我不明白为什么下面的代码会调用基类的(= A)实现
A::operator=(A&)
每当作为参数传入的引用属于派生类类型时,(由输出行15和17表示)。由于所有指针都指向派生类的对象,因此我希望所有调用都具有相同的行为(=从派生类B中调用运算符)。
执行下面列出的代码时的输出:
- A :: Ctor objID = 1280 text = BObject1
- B :: Ctor objID = 1280 text = BObject1
- A :: Ctor objID = 3279 text = BObject2
- B :: Ctor objID = 3279 text = BObject2
- --- ptrA-> print():
- B :: print()objID = 1280 text = BObject1
- B :: print()objID = 3279 text = BObject2
- --- * ptrAObj1 = * ptrAObj2:
- B :: operator =(A&)objID = 1280 text = BObject1
- --- ptrAObj1-> operator =(* ptrAObj2):
- B :: operator =(A&)objID = 1280 text = BObject2
- --- * ptrBObj1 = * ptrAObj2:
- B :: operator =(A&)objID = 1280 text = BObject2
- --- * ptrBObj1 = * ptrBObj2:
- A :: operator =(A&)objID = 1280 text = BObject2
- --- ptrBObj1-> operator =(* ptrBObj2):
- A :: operator =(A&)objID = 1280 text = BObject2
- --- ptrAObj1-> operator =(* ptrBObj2):
- B :: operator =(A&)objID = 1280 text = BObject2
- ---- TearDown()
- B :: Dtor objID = 1280 text = BObject2
- A :: Dtor objID = 1280 text = BObject2
- B :: Dtor objID = 3279 text = BObject2
- A :: Dtor objID = 3279 text = BObject2
醇>
代码:
#include "gtest/gtest.h"
#include <stdio.h>
#include <string>
#include <ctime>
class A
{
public:
A(const std::string& sLabel=""):
m_sText(sLabel)
{
std::srand(std::time(nullptr)); // use current time as seed for random generator
int random_no = (std::rand()+(m_iObjectCounter*1999)) % 9999;
m_iObjectID = random_no;
m_iObjectCounter++;
std::cout << "A::Ctor objID=" << m_iObjectID << " text=" << m_sText << std::endl;
}
virtual ~A()
{
std::cout << "A::Dtor objID=" << m_iObjectID << " text=" << m_sText << std::endl;
}
virtual void print() const
{
std::cout << "A::print() objID=" << m_iObjectID << " text=" << m_sText << std::endl;
}
virtual int operator=(A& rIn)
{
std::cout << "A::operator=(A&) objID=" << m_iObjectID << " text=" << m_sText << std::endl;
m_sText = rIn.m_sText;
return EXIT_SUCCESS;
}
std::string m_sText;
int m_iObjectID;
static int m_iObjectCounter;
};
int A::m_iObjectCounter=0;
class B : public A
{
public:
B(const std::string& sLabel = "") :
A(sLabel)
{
std::cout << "B::Ctor objID=" << m_iObjectID << " text=" << m_sText << std::endl;
}
virtual ~B()
{
std::cout << "B::Dtor objID=" << m_iObjectID << " text=" << m_sText << std::endl;
}
virtual void print() const
{
std::cout << "B::print() objID=" << m_iObjectID << " text=" << m_sText << std::endl;
}
virtual int operator=(A& rIn)
{
std::cout << "B::operator=(A&) objID=" << m_iObjectID << " text=" << m_sText << std::endl;
m_sText = rIn.m_sText;
return EXIT_SUCCESS;
}
};
class TestAssignmentOperator : public ::testing::Test {
public:
TestAssignmentOperator() :
ptrAtoBObject1(NULL),
ptrAtoBObject2(NULL)
{
ptrAtoBObject1 = new B("BObject1");
ptrAtoBObject2 = new B("BObject2");
}
void SetUp(){}
void TearDown() {
std::cout << " ---- TearDown()" << std::endl;
delete ptrAtoBObject1;
delete ptrAtoBObject2;
}
~TestAssignmentOperator(){}
A* ptrAtoBObject1;
A* ptrAtoBObject2;
};
TEST_F(TestAssignmentOperator, Test1)
{
std::cout << " --- ptrA->print():" << std::endl;
ptrAtoBObject1->print();
ptrAtoBObject2->print();
int iError = EXIT_FAILURE;
std::cout << " --- *ptrAObj1 = *ptrAObj2:" << std::endl;
iError = *ptrAtoBObject1 = *ptrAtoBObject2;
std::cout << " --- ptrAObj1->operator=(*ptrAObj2):" << std::endl;
iError = ptrAtoBObject1->operator=(*ptrAtoBObject2);
std::cout << " --- *ptrBObj1 = *ptrAObj2:" << std::endl;
B* ptrBToObject1 = dynamic_cast<B*>(ptrAtoBObject1);
iError = *ptrBToObject1 = *ptrAtoBObject2;
std::cout << " --- *ptrBObj1 = *ptrBObj2:" << std::endl;
B* ptrBtoBObject2 = dynamic_cast<B*>(ptrAtoBObject2);
*ptrBToObject1 = *ptrBtoBObject2;
std::cout << " --- ptrBObj1->operator=(*ptrBObj2):" << std::endl;
ptrBToObject1->operator=(*ptrBtoBObject2);
std::cout << " --- ptrAObj1->operator=(*ptrBObj2):" << std::endl;
ptrAtoBObject1->operator=(*ptrBtoBObject2);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
答案 0 :(得分:8)
我认为这是因为B类中的virtual int operator=(A& rIn)
不是真正的默认operator=
所以编译器正在为您创建B& operator=(const B& other)
并使用该版本。
要添加到此,仅覆盖虚拟运算符=是不够的,还必须定义常规运算符=。
当然最好使用像override
关键字这样的C ++ 11功能来保证你实际编写了你想要的函数签名,而不是创建一个新的虚函数。
答案 1 :(得分:2)
在这两种情况下,您都会调用默认B::operator=(const B&)
。其默认实现调用A::operator=(A&)