的VisualStudio的2013编译器处理下面的代码就好了,但铿锵5.0和6.2给了我一个链接错误:重载抽象操作时锵链接错误=
#include <memory>
using namespace::std;
class IBase
{
public:
virtual IBase& operator=(const IBase& other) = 0;
};
class Base : virtual public IBase
{
public:
Base& operator=(const IBase& other) override
{
const Base& b = dynamic_cast<const Base&>(other);
return *this = b;
}
virtual Base& operator=(const Base& other)
{
return *this;
}
};
class IDerived : virtual public IBase
{
};
class Derived : public IDerived, public Base
{
public:
using Base::operator=;
};
int main(int argc, const char * argv[]) {
shared_ptr<Derived> d1 = make_shared<Derived>();
shared_ptr<Derived> d2 = make_shared<Derived>();
*d2 = *d1;
}
下面是生成日志输出:
Ld /Users/Jennifer/Library/Developer/Xcode/DerivedData/Operator-bjjgcoxcziyegjgmazknrandutqz/Build/Products/Debug/Oper normal x86_64
cd /Users/Jennifer/Documents/Operator
export MACOSX_DEPLOYMENT_TARGET=10.9
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/Jennifer/Library/Developer/Xcode/DerivedData/Operator-bjjgcoxcziyegjgmazknrandutqz/Build/Products/Debug -F/Users/Jennifer/Library/Developer/Xcode/DerivedData/Operator-bjjgcoxcziyegjgmazknrandutqz/Build/Products/Debug -filelist /Users/Jennifer/Library/Developer/Xcode/DerivedData/Operator-bjjgcoxcziyegjgmazknrandutqz/Build/Intermediates/Operator.build/Debug/Oper.build/Objects-normal/x86_64/Oper.LinkFileList -mmacosx-version-min=10.9 -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/Jennifer/Library/Developer/Xcode/DerivedData/Operator-bjjgcoxcziyegjgmazknrandutqz/Build/Intermediates/Operator.build/Debug/Oper.build/Objects-normal/x86_64/Oper_dependency_info.dat -o /Users/Jennifer/Library/Developer/Xcode/DerivedData/Operator-bjjgcoxcziyegjgmazknrandutqz/Build/Products/Debug/Oper
Undefined symbols for architecture x86_64:
"IBase::operator=(IBase const&)", referenced from:
IDerived::operator=(IDerived const&) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
IBase::operator=(IBase const&)
在选自Base
其中Derived
继承所定义,并且是Derived
using Base::operator=
所以应被用于定义,未被默认赋值运算符覆盖。
我发现的一个解决方案是删除IBase::operator=
方法,但这并不理想,因为它是任何继承类需要实现的方法。
有谁知道有什么区别,以及如何解决它?如果可能,我想保留IBase::operator=
方法。
关于答案中的第一段,可能值得澄清的是,即使使用声明引入了具有正确参数typ e,它仍然不会抑制复制赋值运算符的隐式声明。 – bogdan
@bogdan谢谢,我忘了那部分。 – ecatmur