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,就尽量定义一个默认构造函数。

  1. 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!
  2. 友元函数 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

  3. 不同编译器对 临时匿名变量(临时匿名 类对象)的对待不同,大部分会认为是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 &para1,const MinMax &para2);
    friend MinMax operator+(const MinMax &para1,int para2);
    friend MinMax operator+(int para1,const MinMax &para2);

private:
    int m_nMin;
    int m_nMax;
};
MinMax operator+(const MinMax &para1,const MinMax &para2)
{
    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}

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

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

  3. If you don’t want a class to be copyable, use a private copy constructor and assignment operator prototype in the class header.仅声明不用实现。

  4. 要想搞懂类的各种继承关系,就看这里!http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/
  5. a function that is defined as private in the base class can be redefined as public in the derived class, or vice-versa!
  6. you can only change the access specifiers of base members the derived class would normally be able to access. 也就是说子类要改变父类的成员属性,只能改变子类能访问的,一旦能访问就有了所有权,可以把条件由宽松改为限制,比如protected 改为private,或者由限制改为宽松,比如把protected 改为 public.
  7. A 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.

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值