gpt4 book ai didi

c++ - 具有虚拟赋值运算符的多态性

转载 作者:太空狗 更新时间:2023-10-29 19:51:20 25 4
gpt4 key购买 nike

我不明白为什么下面的代码调用基类(=A)的实现

A::operator=(A&)

(由输出行 15 和 17 指示)只要作为参数传入的引用属于派生类类型。由于所有指针都指向派生类的对象,因此我希望所有调用都具有相同的行为(=从派生类 B 调用运算符)。

执行下面列出的代码时的输出:

  1. A::Ctor objID=1280 text=BObject1
  2. B::Ctor objID=1280 text=BObject1
  3. A::Ctor objID=3279 text=BObject2
  4. B::Ctor objID=3279 text=BObject2
  5. --- ptrA->print():
  6. B::print() objID=1280 text=BObject1
  7. B::print() objID=3279 text=BObject2
  8. --- *ptrAObj1 = *ptrAObj2:
  9. B::operator=(A&) objID=1280 text=BObject1
  10. --- ptrAObj1->operator=(*ptrAObj2):
  11. B::operator=(A&) objID=1280 text=BObject2
  12. --- *ptrBObj1 = *ptrAObj2:
  13. B::operator=(A&) objID=1280 text=BObject2
  14. --- *ptrBObj1 = *ptrBObj2:
  15. A::operator=(A&) objID=1280 text=BObject2
  16. --- ptrBObj1->operator=(*ptrBObj2):
  17. A::operator=(A&) objID=1280 text=BObject2
  18. --- ptrAObj1->operator=(*ptrBObj2):
  19. B::operator=(A&) objID=1280 text=BObject2
  20. ---- TearDown()
  21. B::Dtor objID=1280 text=BObject2
  22. A::Dtor objID=1280 text=BObject2
  23. B::Dtor objID=3279 text=BObject2
  24. 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();
}

最佳答案

我相信这是因为 B 类中的 virtual int operator=(A& rIn) 不是真正的默认 operator= 所以编译器正在创建一个 B& operator=(const B& other) 为你使用那个。

除此之外,仅覆盖虚拟 operator= 是不够的,您还必须定义常规 operator=。

当然,最好使用 C++11 特性,例如 override 关键字来保证您实际上编写了您想要的函数签名,而不是创建一个新的虚函数。

关于c++ - 具有虚拟赋值运算符的多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48213630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com