8 c++ inheritance constructor namespaces multiple-inheritance
如果它们位于不同的名称空间中,是否可以继承具有相同名称的两个基类?
顺便说一句,我此时并没有计划这样做,但我很好奇:
class SuperShape : Physics::Shape, Graphics::Shape
{
// constructor
SuperShape( int x, int y, float color) : ???( x, y ), ???( color );
}
Run Code Online (Sandbox Code Playgroud)
小智 10
当然,为什么不呢?没有什么能阻止你这样做.这是一个工作示例:
#include <iostream>
#include <typeinfo>
#include <string>
namespace NS1 {
class MyClass {
public:
MyClass (const std::string &) {}
};
}
namespace NS2 {
class MyClass {
public:
MyClass (int) {}
};
}
class MyClass :
public NS1::MyClass,
public NS2::MyClass
{
public:
MyClass () :
NS1::MyClass (std::string ("Hello")),
NS2::MyClass (1986)
{}
};
int main ()
{
MyClass clazz;
std::cout << typeid (NS1::MyClass).name () << std::endl;
std::cout << typeid (NS2::MyClass).name () << std::endl;
std::cout << typeid (clazz).name () << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
嗯,简单地说:
SuperShape( int x, int y, float color)
: Physics::Shape( x, y ), Graphics::Shape( color )
{
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4816 次 |
| 最近记录: |