Add( const int * value );
两个函数不一样
2.vector加入对象时,调用copy assignment,且形参为const,然后调用 operator=()赋值函数(形参可以是const,也可以不是)
3.纯虚函数必须在子类中重新声明,但它还是可以在基类中有自己的实现
4.char *p = "hello"; (int)p输出与printf("%p");是一致的
打印成员函数地址时,必须用printf("%p"),不能用std::cout,因为iostream识别不了CXX::memfun的类型,把它转成了一个bool
5.静态变量,静态函数只有是public时才能独立于对象之外直接存取。
6.在虚函数内部调用另一个虚函数时,可以直接用CXX::memFunction();,可以提高效率
7.class CA
{
private:
const static int m_ca;
//也可以直接 const static int m_ca = 10;
public:
static void Display() //静态函数不能加类型限定符,如const, volatile, virtual
{
std::cout << "CA: " << m_ca << std::endl;
}
};
8.虚函数可以返回不同类型,但仅限于有继承成关系的类的指针或引用
9.
placement new上分配的内存必须直接调用析构函数,系统不会自动调用析构函数
placement new获得的指针如果不是operator new上获得的,就不能delete,否则出错
使用placement new可以使new 分配的对象数组逐个设置初始值
10.nothrow 须包含头文件<new.h>
11.之所以对一般函数,通过类得指针调用的函数只会是本类的函数,而不会是基类或其他函数,其原因就在于这种函数就静态绑定的
与此对应,虚函数就是动态绑定
12.函数的默认参数也是静态绑定,定义一个指针是静态绑定,指向一个对象是动态绑定
13.私有继承的子类不能转为基类
14.取类成员函数地址
// build_independ.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
class CT
{
public:
CT()
{}
void Display()
{
std::cout << "CT::Display()" << std::endl;
}
public:
static void Display2(int value)
{
std::cout << "CT::Display2() " << value << std::endl;
}
virtual void Display3(const char* ptr)
{
m_value = 3;
std::cout << "CT::Display3() " << ptr << std::endl;
}
private:
int m_value;
};
class CD : public CT
{
public:
virtual void Display3(const char* ptr)
{
std::cout << "CD::Display3() " << ptr << std::endl;
}
};
void (CT::*fun)();
void (*fun2)(int);
void (CT::*fun3)(const char*);
int _tmain(int argc, _TCHAR* argv[])
{
CT t;
fun = CT::Display;
(t.*fun)();
fun2 = CT::Display2;
fun2(12);
CD d;
fun3 = CT::Display3;
(d.*fun3)("hello");
printf("%pn", CT::Display);
printf("%p\n", CT::Display2);
printf("%p\n", CT::Display3);
}
note:
C++ reminds us of that by disallowing the conversion from pointer to member of Drived Class to pointer to member of BaseClass.(member, 类的成员变量)
类的方法没有“虚拟”属性
there is a predefined conversion from a pointer to a member function of a base class to a pointer to a member function of a derived class, not the reverse
This makes sense if you consider that a base class member function will attempt to access only base class members through its this pointer whereas a derived class function may attempt to access members that are not present in the base class.
普通函数指针:
Note that both the argument and return types contribute to the type of a function or function pointer.
char *(*fp4)(int,int);
char *(*fp5)(short,short) = 0;
fp4 = fp5; // error! type mismatch
15.虚拟虚拟构造函数 :
能够根据输入给它的数据的不同而建立不同类型的对象。虚拟构造函数在很多场合下都有用处,从磁盘(或者通过网络连接,或者从磁带机上)读取对象信息只是其中的一个应用。
虚拟非成员函数:
void fun( CXX &c)
{
c.virtualfunction();
}
就是参数为一个类对象的引用,然后在函数里面调用这个参数的虚拟函数.
16.定义一个函数指针,然后为它赋值,这样做可以动态设置回调函数指针,但不会被编译为inline函数
17.限制类对象必须在堆上创建,把析构函数定义为private就行了,如果把构造函数定义为private,则所有的构造函数都要定义为private,太麻烦了