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;
}