C++ 学习 笔记(菜鸟级,自我备忘)

1. class  内部定义的函数默认为 inline,参考 C++ 对class 讲解章节。 

2. class  成员函数后面加上 const ,说明不会修改class 的对象的数据成员。

 const 成员函数的来龙去脉:
首先是像定义普通类型的const 变量一样:
const int a = 5; //表示这个a定义后就不想改变了,那么,定义一个const类对象呢?
const classType A;//怎么保证这个对象A 里面的数据成员不会变呢? 
只有设计出一个const成员函数,这个成员函数保证不修改对象的的数据成员。而且,const对象只能调用这个const成员函数,千万不能调用别的了哦~ 不然报错哦~

而函数前加的const 仅仅表示函数返回值是个const,满足不了我们设计要求,所以就在函数参数列表后面加个const 表示这是个const 成员函数,保证不修改数据成员,甚至在编译阶段发现代码企图修改数据成员,编译器就果断报错! 

3. 如果我们一个constructor都不定义,编译器就会帮我定义一个默认constructor,如果我们定义了一个带参数的constructor,那么编译器就不管我们了,假如没定义一个默认constructor,那么如果声明一个没有任何参数的类对象时就会报错,因为没有默认constructor,编译器没没帮忙。所以定义了有参数的constructor,就尽量定义一个默认构造函数。


4.  static member functions can only access static member variables. They can not access non-static member variables. This is because non-static member variables must belong to a class object, and static member functions have no class object to work with!

5. 友元函数  If you want two classes to be friends of each other, both must declare the other as a friend. Finally, if class A is a friend of B, and B is a friend of C, that does not mean A is a friend of C.

6 用"=" 在对象之间赋值,那么编译器会拷贝对象的每一个成员。The compiler supplies every class with a default implementation of operator=, which copies each member variable.

6.a 6.a  省略变量名就创建一个临时匿名变量。

Cents cents(5); // normal variable
Cents(7); // anonymous variable


7. 不同编译器对 临时匿名变量(临时匿名  类对象)的对待不同,大部分会认为是const 类型的。

class MinMax
{
public:
	MinMax(int min,int max){ m_nMin = min,m_nMax = max; }
	int getMin(void) { return m_nMin; }
	int getMax(void) { return m_nMax; }
	friend MinMax operator + (const MinMax ¶1,const MinMax ¶2);
	friend MinMax operator+(const MinMax ¶1,int para2);
	friend MinMax operator+(int para1,const MinMax ¶2);

private:
	int m_nMin;
	int m_nMax;
};
MinMax operator+(const MinMax ¶1,const MinMax ¶2)
{
	MinMax temp(0,0);//下面有更高级的写法
	temp.m_nMin = para1.m_nMin < para2.m_nMin?para1.m_nMin:para2.m_nMin;
	temp.m_nMax = para1.m_nMax > para2.m_nMax?para1.m_nMax:para2.m_nMax;
	return temp;
}
MinMax operator+(const MinMax &cM1,int n2)
{
	int min = cM1.m_nMin < n2 ? cM1.m_nMin : n2;
	int max = cM1.m_nMax > n2 ? cM1.m_nMax : n2;
	return MinMax(min,max);//一个临时匿名对象被返回
}
MinMax operator+(int n1,const MinMax &cM2)
{
	return MinMax(cM2+n1);
}
当在main函数中定义了类对象

MinMax v1(2,3);
MinMax v2 = (v1 + 6) + 18;
其中v1 + 6就会生成一个临时匿名对象,然后再加上18,此时编译器认为这个匿名对象const类型的,那么调用重载函数
MinMax operator+(const MinMax &cM1,int n2)
时,如果第一个参数不加const就会编译不过,所以参数前面加了const 

8. 通过成员函数重载操作符需要注意的事项

Overloading operators using a member function is very similar to overloading operators using a friend function. When overloading an operator using a member function:

  • The leftmost operand of the overloaded operator must be an object of the class type.
  • The leftmost operand becomes the implicit *this parameter. All other operands become function parameters.

Most operators can actually be overloaded either way, however there are a few exception cases:

  • If the leftmost operand is not a member of the class type, such as when overloading operator+(int, YourClass), or operator<<(ostream&, YourClass), the operator must be overloaded as a friend.
  • The assignment (=), subscript ([]), call (()), and member selection (->) operators must be overloaded as member functions.
9. for(int i = 0; i < n; i++){ } 这样的写法是不合理的,最后那个i自增应该用前辍自增++i;   因为这里的目的就是让i直接加一,而postfix ++ 后辍加加目的是先使用i然后再让让其自增,显然不符合我们的目的,所以标准写法为for(int i = 0; i < n; ++i){ }

10. 重载类型转换操作符“type”的注意事项:Casting operators do not have a return type. C++ assumes you will be returning the correct type.

operator type  (){ return theTypeValue}


11. C++ 编译器默认提供一个default assignment operator ,只是对成员进行浅拷贝。


12. 如果我们写 overloaded assignment operators, the first thing we do is check for self assignment. 检查*this == para


13. If you don’t want a class to be copyable, use a private copy constructor and assignment operator prototype in the class header.仅声明不用实现。
14. 要想搞懂类的各种继承关系,就看这里!http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/
15. a function that is defined as private in the base class can be redefined as public in the derived class, or vice-versa!

16. you can only change the access specifiers of base members the derived class would normally be able to access. 也就是说子类要改变父类的成员属性,只能改变子类能访问的,一旦能访问就有了所有权,可以把条件由宽松改为限制,比如protected 改为private,或者由限制改为宽松,比如把protected 改为 public.

17. virtual function is a special type of function that resolves to the most-derived version of the function with the same signature. This capability is known as polymorphism.

To make a function virtual, simply place the “virtual” keyword before the function declaration.

Note that virtual functions and virtual base classes are two entirely different concepts, even though they share the same keyword.

关于 virtual base classes ,想想那个 diamond inheritance issue. 

18. whenever you are dealing with inheritance, you should make your destructors virtual.

19. static 成员函数不能访问非 static 数据成员,因为没有this 指针,不知道要访问哪个对象的数据成员。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值