标签:std 其他 不能 通过 定义 value 类构造 成员 移动
一个类只继承其直接基类的构造函数,默认/拷贝/移动构造函数是不能被继承的;
继承Father的构造函数
using Father::Father;
//继承Father的构造函数 using让某个名字在当前的作用域可见;
//编译器遇到这条代码的时候,会把基类的每个构造函数都生成一个与之对应的派生类构造函数
如果基类A的构造函数有默认参数的话,那么编译器遇到这种using A::A的时候,就会帮咱们在派生类B中构造出多个构造函数来;
//示例:
Son(int i, int j, int k = 5) :Father(i, j, k){};
Son(int i, int j, int k):Father(i,j,k){};
Son(int i, int j):Father(i,j){};
如果基类含多个构造函数,则多数情况下,派生类会继承所有这些构造函数,但如下情况例外:
#include <iostream>
using namespace std;
class Father
{
public:
Father(int i, int j, int k)
{
}
};
class Son : public Father
{
public:
using Father::Father; //继承Father的构造函数 using让某个名字在当前的作用域可见;
//编译器遇到这条代码的时候,会把基类的每个构造函数都生成一个与之对应的派生类构造函数
};
int main()
{
Son son(3, 4, 5);
}
从多个父类产生出子类,多重继承;
#include <iostream>
using namespace std;
class Base
{
public:
Base(int i) :m_value_base(i) {};
public:
int m_value_base;
virtual ~Base()
{
}
void my_info()
{
cout << m_value_base << endl;
}
};
class Father_A: public Base
{
public:
Father_A(int i) :Base(i), m_value_a(i) //每个子类的构造函数,负责解决自己父类的初始化问题
{
};
virtual ~Father_A()
{
}
void my_info()
{
cout << m_value_a << endl;
}
public:
int m_value_a;
};
class Father_B
{
public:
Father_B(int i):m_value_b(i) //每个子类的构造函数,负责解决自己父类的初始化问题
{
};
virtual ~Father_B()
{
}
void my_info()
{
cout << m_value_b << endl;
}
public:
int m_value_b;
};
//class默认是私有继承
//struct默认是共有继承
class Son :public Father_A, public Father_B
{
public:
Son(int i, int j, int k) :Father_A(i), Father_B(j), m_value_c(k)
{
}
virtual ~Son()
{
}
void my_info_c()
{
cout << m_value_c << endl;
Father_A::my_info(); //调用父类Father_A的同名函数
Father_B::my_info(); //调用父类Father_B的同名函数
}
public:
int m_value_c;
};
int main()
{
Son son(10, 20, 50);
//多重继承的二义性问题
//1. 通过作用域来解决
son.Father_A::my_info(); //增加作用域,明确告诉系统调用的是父类A还是父类B的成员函数
//派生类会包含每个基类的子对象
}
静态成员属于类,不属于对象;
为了能够使用,需要定义静态成员变量;如果代码中不用该静态变量,可以不定义;如果用到该静态变量,可以定义;否则,链接出错;
#include <iostream>
using namespace std;
class Base
{
public:
Base(int i) :m_value_base(i) {};
public:
virtual ~Base()
{
}
void my_info()
{
cout << m_value_base << endl;
}
public:
int m_value_base;
static int m_static;
};
int Base::m_static = 5;
class Father_A: public Base
{
public:
Father_A(int i) :Base(i), m_value_a(i) //每个子类的构造函数,负责解决自己父类的初始化问题
{
};
virtual ~Father_A()
{
}
void my_info()
{
cout << m_value_a << endl;
}
public:
int m_value_a;
};
class Father_B
{
public:
Father_B(int i):m_value_b(i) //每个子类的构造函数,负责解决自己父类的初始化问题
{
};
virtual ~Father_B()
{
}
void my_info()
{
cout << m_value_b << endl;
}
public:
int m_value_b;
};
//class默认是私有继承
//struct默认是共有继承
class Son :public Father_A, public Father_B
{
public:
Son(int i, int j, int k) :Father_A(i), Father_B(j), m_value_c(k)
{
}
virtual ~Son()
{
}
void my_info_c()
{
cout << m_value_c << endl;
Father_A::my_info(); //调用父类Father_A的同名函数
Father_B::my_info(); //调用父类Father_B的同名函数
}
public:
int m_value_c;
};
int main()
{
//派生类会包含每个基类的子对象
Base::m_static = 1;
Father_A::m_static = 2;
Son::m_static = 3;
Son son(10, 20, 30);
son.m_static = 15;
}
显示的初始化基类和隐式的初始化基类
#include <iostream>
using namespace std;
class Base
{
public:
Base(int i) :m_value_base(i)
{
cout << "Base构造函数执行" << endl;
};
public:
virtual ~Base()
{
cout << "Base析构函数执行" << endl;
}
public:
int m_value_base;
};
class Father_A: public Base
{
public:
Father_A(int i) :Base(i), m_value_a(i) //每个子类的构造函数,负责解决自己父类的初始化问题
{
cout << "Father_A构造函数执行" << endl;
};
virtual ~Father_A()
{
cout << "Father_A析构函数执行" << endl;
}
public:
int m_value_a;
};
class Father_B
{
public:
Father_B(int i):m_value_b(i) //每个子类的构造函数,负责解决自己父类的初始化问题
{
cout << "Father_B构造函数执行" << endl;
};
virtual ~Father_B()
{
cout << "Father_B析构函数执行" << endl;
}
public:
int m_value_b;
};
//class默认是私有继承
//struct默认是共有继承
class Son :public Father_A, public Father_B
{
public:
Son(int i, int j, int k) :Father_A(i), Father_B(j), m_value_c(k)
{
cout << "Son构造函数执行" << endl;
}
virtual ~Son()
{
cout << "Son析构函数执行" << endl;
}
public:
int m_value_c;
};
int main()
{
Son son(10, 20, 30);
}
如果一个类从它的基类中继承了相同的构造函数,这个类必须为该构造函数定义它的自己的版本;
子类要定义同参数的构造函数的自己的版本;
#include <iostream>
using namespace std;
class Father_A
{
public:
Father_A(int tv) {};
};
class Father_B
{
public:
Father_B(int tv) {};
};
class Son :public Father_A, public Father_B
{
using Father_A::Father_A;//继承Father_A的构造函数 Son(int tv):Father_A(tv){}
using Father_B::Father_B;//继承Father_B的构造函数 Son(int tv):Father_B(tv){}
Son(int tv) :Father_A(tv), Father_B(tv) {};
};
int main()
{
}
基类指针可以指向一个派生类对象:编译器隐式执行这种派生类到基类的转换,转换成功的原因是每个派生类对象都包含一个基类对象部分;所以基类的引用或者指针是可以绑定到基类对象这部分;
上述对于多重继承同样成立;
#include <iostream>
using namespace std;
class Base
{
public:
Base(int i) :m_value_base(i)
{
cout << "Base构造函数执行" << endl;
};
public:
virtual ~Base()
{
cout << "Base析构函数执行" << endl;
}
public:
int m_value_base;
};
class Father_A: public Base
{
public:
Father_A(int i) :Base(i), m_value_a(i) //每个子类的构造函数,负责解决自己父类的初始化问题
{
cout << "Father_A构造函数执行" << endl;
};
virtual ~Father_A()
{
cout << "Father_A析构函数执行" << endl;
}
public:
int m_value_a;
};
class Father_B
{
public:
Father_B(int i):m_value_b(i) //每个子类的构造函数,负责解决自己父类的初始化问题
{
cout << "Father_B构造函数执行" << endl;
};
virtual ~Father_B()
{
cout << "Father_B析构函数执行" << endl;
}
public:
int m_value_b;
};
//class默认是私有继承
//struct默认是共有继承
class Son :public Father_A, public Father_B
{
public:
Son(int i, int j, int k) :Father_A(i), Father_B(j), m_value_c(k)
{
cout << "Son构造函数执行" << endl;
}
virtual ~Son()
{
cout << "Son析构函数执行" << endl;
}
public:
int m_value_c;
};
int main()
{
Base* base = new Son(1, 2, 3);
Father_A* fa = new Son(1, 2, 3);
Father_B* fb = new Son(1, 2, 3);
Son son(10 ,20, 30);
Base b(son);
}
派生列表中,同一个基类只能出现一次,但是如下两种情况例外;
派生类可以通过它的两个直接基类分别继承同一个间接基类;
直接继承某个基类,然后通过另一个基类间接继承
虚基类和虚继承
说明
#include <iostream>
using namespace std;
class Base
{
public:
Base(){}
Base(int i) :m_value_base(i)
{
cout << "Base构造函数执行" << endl;
};
public:
virtual ~Base()
{
cout << "Base析构函数执行" << endl;
}
public:
int m_value_base;
};
class Father_A: virtual public Base //virtual可以和public互换位置
{
public:
Father_A(int i) :Base(i), m_value_a(i) //每个子类的构造函数,负责解决自己父类的初始化问题
{
cout << "Father_A构造函数执行" << endl;
};
virtual ~Father_A()
{
cout << "Father_A析构函数执行" << endl;
}
public:
int m_value_a;
};
class Father_C : virtual public Base
{
public:
Father_C(int i) :Base(i), m_value_c(i) //每个子类的构造函数,负责解决自己父类的初始化问题
{
cout << "Father_C构造函数执行" << endl;
};
virtual ~Father_C()
{
cout << "Father_C析构函数执行" << endl;
}
public:
int m_value_c;
};
class Father_B
{
public:
Father_B(int i):m_value_b(i) //每个子类的构造函数,负责解决自己父类的初始化问题
{
cout << "Father_B构造函数执行" << endl;
};
virtual ~Father_B()
{
cout << "Father_B析构函数执行" << endl;
}
public:
int m_value_b;
};
//class默认是私有继承
//struct默认是共有继承
class Son :public Father_A, public Father_B, public Father_C
{
public:
//虚基类的时候,孙子初始化爷爷
Son(int i, int j, int k) :Base(i), Father_A(i), Father_B(j), Father_C(k), m_value_c(k)
{
cout << "Son构造函数执行" << endl;
}
virtual ~Son()
{
cout << "Son析构函数执行" << endl;
}
public:
int m_value_c;
};
int main()
{
Son son(10 ,20, 30);
//son间接继承了Base两次,导致Base构造了两次;
//son.m_value_base = 10; 两个m_value_base, 二义性
}
总结
标签:std 其他 不能 通过 定义 value 类构造 成员 移动
原文地址:https://www.cnblogs.com/Trevo/p/13363941.html