D d1 = d; 这个是调用的是复制构造函数
函数参数是以copy constructor初始化的
Default constructor的错误认为:(P47)
- 任何class如果没有定义default constructor,就会被合成一个出来,其实不是.(要有四种之一,带有default constructor的member class object或base class或带有一个virtual function的class或者带有一个virtual base class 的class)
- 编译器合成出来的default constructor会现实定义”class内每一个data member的默认值”,其实不是.(一些内置类型是不会的)
Default copy也是有上述四种情况才会自动生成的
带有default constructor的member class object
class B{
public:
B(){
cout << "B" << endl;
}
};
class D{
public:
D(){
}
private:
int c;
B b;
};
带有default constructor的base class
int main(){
D d; 输入B,编译器会自动添加B的初始化(在D构造函数中加),但是会初始话c
cout << d.c;
return 0;
}
class B{ 与上面的返回值一样
public:
B(){
cout << "B" << endl;
}
};
class D: public B{
public:
D(){
}
int c;
};
int main(){
D d;
cout << d.c;
return 0;
}
enum n{N,M,K,J,L};
cout << sizeof(n) << endl; //输出4
class B{
public:
virtual void x(){
cout << "call B virtual x()" << endl;
}
};
class D: public B{
public:
void x(){
cout << "call D virtual x()" << endl;
}
};
int main(){
B *b = new D;
(*b).x();
返回的还是D类中的x();
在下列情况下你必须使用member initialization list:
- 当初始化一个reference member时;
- 当初始化一个const member时;
- 当调用一个base class的constructor,而它拥有一组参数时
- 当调用一个member class的constructor,而它拥有一组参数时
class X{
};
class Y: virtual public X{};
class Z: virtual public X{};
class T: public Y, public Z{};
int main(){
cout << "X: " << sizeof(X) << endl; //1
cout << "Y: " << sizeof(Y) << endl; //4
cout << "Z: " << sizeof(Z) << endl; //4
cout << "T: " << sizeof(T) << endl; //8
X x;
Y y;
Z z;
T t;
cout << "x: " << sizeof(x) << endl;
cout << "y: " << sizeof(y) << endl;
cout << "z: " << sizeof(z) << endl;
cout << "t: " << sizeof(t) << endl;
return 0;
}
class X{
};
class Y: virtual public X{};
class Z: virtual public X{};
class T: public Y, virtual public Z{};
int main(){
cout << "X: " << sizeof(X) << endl; //1
cout << "Y: " << sizeof(Y) << endl; //4
cout << "Z: " << sizeof(Z) << endl; //4
cout << "T: " << sizeof(T) << endl; //8
X x;
Y y;
Z z;
T t;
cout << "x: " << sizeof(x) << endl;
cout << "y: " << sizeof(y) << endl;
cout << "z: " << sizeof(z) << endl;
cout << "t: " << sizeof(t) << endl;
return 0;
}
class X{
};
class Y: virtual public X{};
class Z: virtual public X{};
class T: virtual public Y, virtual public Z{};
int main(){
cout << "X: " << sizeof(X) << endl; //1
cout << "Y: " << sizeof(Y) << endl; //4
cout << "Z: " << sizeof(Z) << endl; //4
cout << "T: " << sizeof(T) << endl; //12
X x;
Y y;
Z z;
T t;
cout << "x: " << sizeof(x) << endl;
cout << "y: " << sizeof(y) << endl;
cout << "z: " << sizeof(z) << endl;
cout << "t: " << sizeof(t) << endl;
return 0;
}
Data Member绑定
typedef int length;
class X{
public:
void mumble(length val){ _val = 1.1;} //这里的length是global的
length mumble(){return _val;} //这里的length也是global的
public:
typedef float length;
length _val; //class的
};
int main(){
X x;
x.mumble(1.1);
cout << x.mumble() << endl; //1
cout << x._val << endl; //1.1
return 0;
}
如果想要typedef int length是class内的就要在类的开始定义就好,但是类内成员变量就不会有这问题
class X{
public:
static int l;
float x;
};
class Y : public X{
};
int X::l = 10;
int main(){
cout << X::l << endl; //10
cout << Y::l << endl; //10
X::l = 11;
cout << Y::l << endl; //11 因为X,Y是共享存储在data segment的l
return 0;
}
P103
class X{
private:
int l;
char a;
char b;
};
int main(){
cout << "sizeof(X):" << sizeof(X) << endl; //8
return 0;
}
class X{
public:
int l;
char a;
};
class Y: public X{
public:
char b;
};
int main(){
cout << "sizeof(Y):" << sizeof(Y) << endl; //12 原因X的补充不会与Y相连
return 0; 想想X x; Y y; x = y,如果是相连的话会使x中有个未知的b成员变量在最后
}
class B{
public:
virtual void func();
};
class D: virtual public B{
public:
virtual void fun();
};
int main(){
cout << "sizeof(B):" << sizeof(B) << endl; //4
cout << "sizeof(D):" << sizeof(D) << endl; //12自己也有一个虚table指针,一个指向
return 0; B的指针,还有就是B父类的大小
}
class B{
public:
virtual void func();
};
class D: virtual public B{
};
int main(){
cout << "sizeof(B):" << sizeof(B) << endl; //4
cout << "sizeof(D):" << sizeof(D) << endl; //8 一个指向B的指针,还有就是B的大小
return 0;
}
class B{
public:
virtual void func();
};
class D: public B{
public:
virtual void fun();
};
int main(){
cout << "sizeof(B):" << sizeof(B) << endl; //4
cout << "sizeof(D):" << sizeof(D) << endl; //4 只有一个虚table指针
return 0;
}
class B{
public:
static void func() const; //错误,const 函数不能是static因为const指代
}; 对象中的成员变量不能改变,但是static是类层次的
编译器会将member function 转换成为一个独一无二的nonmember function
class B{
public:
virtual B* func() const;
};
class D: virtual public B{
public:
virtual D* func() const; //虽然D*返回的类型与B*不一样,但是有继承
}; 关系,所以这个虚拟继承是符合的
class B1{
public:
virtual B1* func() const{}
};
class B2{
public:
virtual B2* func() const{}
};
class D: public B1, public B2{
};
int main(){
cout << "sizeof(B1):" << sizeof(B1) << endl; //4
cout << "sizeof(D):" << sizeof(D) << endl; //8因为D会有两个vptr,分别指向两个子类
return 0;
}
单一继承:
多重继承:
虚拟继承: