转自:http://bbs.chinaunix.net/thread-1750709-1-1.html
c++子类能从父类中继承父类的静态成员函数吗
子类不能从父类继承的有: 1. 构造函数 2. 拷贝构造函数 3. 析构函数 子类能从父类继承的有: 1、静态成员变量 2、静态成员函数 3、友元函数 4、赋值操作符=重载函数 关于子类不能继承的三点,这个我大概可以肯定,之前对此也有所了解。但是对其能继承的4点我就不太确认,在网上看了看,也没有找到明确的答案。我贴一下测试代码,欢迎大家指教。 /******测试代码******* *子类不能从父类继承的有: * 1. 构造函数 * 2. 拷贝构造函数 * 3. 析构函数 *子类能从父类继承的有: * 1、静态成员变量 * 2、静态成员函数 * 3、友元函数 * 4、赋值操作符=重载函数 * * 该测试结果不一定准确,仅供参考 */ #include<iostream> using namespace std; class base { int mx; public: static int xxx; //静态成员变量 static void fun(int x) //静态成员函数 { xxx=x; } base(int x):mx(x){} friend std: ![]() ![]() ![]() base& operator =(base &s) //赋值操作符=重载函数 { this->mx = s.mx; return *this; } }; int base: ![]() class test:public base { public: test(int x):base(x){} }; std: ![]() ![]() { os<<s.mx; return os; } int main() { test x(321); test y(123); cout<<test: ![]() test::fun(999); cout<<test: ![]() cout<<x<<endl; cout<<y<<endl; y = x; cout<<y<<endl; system("pause" ![]() return 0; } |