C++:多态复制构造函数可以工作吗?
考虑一下:
class A
{
public:
A( int val ) : m_ValA( val ) {}
A( const A& rhs ) {}
int m_ValA;
};
class B : public A
{
public:
B( int val4A, int val4B ) : A( val4A ), m_ValB( val4B ) {}
B( const B& rhs ) : A( rhs ), m_ValB( rhs.m_ValB ) {}
int m_ValB;
};
int main()
{
A* b1 = new B( 1, 2 );
A* b2 = new A( *b1 ); // ERROR...but what if it could work?
return 0;
}
如果“new A( b1 )”能够解析创建新的 B 副本并返回 A,C++ 会被破坏吗?
这还有用吗?
Consider:
class A
{
public:
A( int val ) : m_ValA( val ) {}
A( const A& rhs ) {}
int m_ValA;
};
class B : public A
{
public:
B( int val4A, int val4B ) : A( val4A ), m_ValB( val4B ) {}
B( const B& rhs ) : A( rhs ), m_ValB( rhs.m_ValB ) {}
int m_ValB;
};
int main()
{
A* b1 = new B( 1, 2 );
A* b2 = new A( *b1 ); // ERROR...but what if it could work?
return 0;
}
Would C++ be broken if "new A( b1 )" was able to resolve to creating a new B copy and returning an A?
Would this even be useful?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您是否需要此功能,或者这只是一个思想实验?
如果您需要这样做,常见的习惯用法是使用
Clone
方法:Do you need this functionality, or is this just a thought experiment?
If you need to do this, the common idiom is to have a
Clone
method:您真正要寻找的是虚拟复制构造函数, eduffy 发布的是标准方法。
还有使用模板来完成此操作的巧妙方法。 (免责声明:自我推销)
What you're really looking for is called a virtual copy constructor, and what eduffy posted is the standard way of doing it.
There are also clever ways of doing it with templates. (disclaimer: self-promotion)
只是对 eduffy 的答案做了一点补充:
代替
};
你可以这样声明:
};
它仍然被认为是虚拟
A::Clone();
的有效重写,并且如果直接通过B *
调用会更好Just a small addition to the answer by eduffy:
Instead of
};
you can declare it like this:
};
It's still considered a valid overriding of the virtual
A::Clone();
and is better if called directly throughB *
假设您有适当的重载,则该表达式
已经具有含义。
如果它被赋予了不同的含义,您就必须提供另一种方式来获得其他含义。 鉴于已经有一种方法可以获得您想要的含义,这有点毫无意义:
并猜测哪个读起来更清晰。
The expression
already has a meaning, supposing you had the appropriate overload.
If it was given a different meaning, you'd have to provide another way to get the other meaning. This is kind of pointless given that there is already a way to get the meaning you want:
And guess which is clearer to read.
是的。 没有。
有多种方法可以实现克隆(例如,或多或少标准的clone()方法、对象工厂的参数化变体、有或没有可配置的依赖注入),而无需改变现有程序的含义,或使其无法创建当派生类在编译单元内已知时,基类的实例。
构造函数和析构函数足够复杂,初学者很难理解。 向它们注入更多的复杂性是不明智的。
Yes. No.
There are a number of ways of implementing cloning (e.g. the more or less standard clone() method, parameterized variations of object factories, with or without configurable dependency injection) without changing the meaning of existing programs, or making it impossible to create instances of base classes when a derived class is known within a compilation unit.
Constructors and destructors are sufficiently complex to understand for beginners as is. Injecting even more complexity into them would be unwise.
如上所述,有多种方法可以实现这一点。
要回答您的问题,如果
new A( *b1 )
返回 B 的新实例,那么这将不起作用。As pointed out above there are a number of ways of implementing this.
To answer your question if
new A( *b1 )
returned an new instance of B then this would not work.