CS225: Data Structure

Labs

Lab1

纯类外增加变量法 (Pure Outline Variable Adding Method, POVAM)

#include <iostream>
using namespace std;

class CA
{
public:
    int m;
};

int32_t main(){
    int CA::*pt = &CA::m;

    CA a;
    a.*pt = 5;

    cout << a.m <<endl;
    return 0
}

这串代码的用处是,在类CA中加入一个指针变量pt 使之等于CA类中m的地址(即:在CA类中创造pt,它指向m),这样可以直接实现改变pt指向对象的值的目标(通过*pt=~语句)。

参数引用化 (Parameter Inferencification, PIF)

所谓“引用”,是在C++中才引入的一个概念,在C语言中不存在相似的表达方式。
在这里插入图片描述

观察上述左右两段代码,区分不同。将参数引用化后,传入的变量仍然可以是变量本身,而非变量指针。更确切地说,传入的只能是变量本身,而非变量指针,这是C++11的规定。

C++11中的引申规定

ISO C++11 does not allow conversion from string literal to 'char *'

出现上述错误时,请在变量前加上const。

LeetCode

int main(int argc, const char *argv[])
{
    try
    {
        int i;
        cin >> i;
        if(i == 0)
            throw runtime_error("出现运行期错误");
        else if(i == 1)
            throw invalid_argument("非法参数");
        else if(i == 2)
            throw logic_error("逻辑错误");
        else
            throw out_of_range("越界错误");
    }
    catch(...) //能捕获所有的异常
    {
    
    }
    catch(exception &e)
    {
        cout << "异常信息:" << e.what() << endl;
    }
    catch(runtime_error &e)
    {
        cout << "runtime_error :" << e.what() << endl;
    }
    catch(invalid_argument &e)
    {
        cout << "invalid_argument:" << e.what() << endl;
    }
    cout << "继续运行" << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值