虚拟赋值运算符

时间:2018-01-11 18:25:06

标签: c++

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

A::operator=(A&)
每当作为参数传入的引用属于派生类类型时,

(由输出行15和17表示)。由于所有指针都指向派生类的对象,因此我希望所有调用都具有相同的行为(=从派生类B中调用运算符)。

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

  
      
  1. A :: Ctor objID = 1280 text = BObject1
  2.   
  3. B :: Ctor objID = 1280 text = BObject1
  4.   
  5. A :: Ctor objID = 3279 text = BObject2
  6.   
  7. B :: Ctor objID = 3279 text = BObject2
  8.   
  9. --- ptrA-> print():
  10.   
  11. B :: print()objID = 1280 text = BObject1
  12.   
  13. B :: print()objID = 3279 text = BObject2
  14.   
  15. --- * ptrAObj1 = * ptrAObj2:
  16.   
  17. B :: operator =(A&)objID = 1280 text = BObject1
  18.   
  19. --- ptrAObj1-> operator =(* ptrAObj2):
  20.   
  21. B :: operator =(A&)objID = 1280 text = BObject2
  22.   
  23. --- * ptrBObj1 = * ptrAObj2:
  24.   
  25. B :: operator =(A&)objID = 1280 text = BObject2
  26.   
  27. --- * ptrBObj1 = * ptrBObj2:
  28.   
  29. A :: operator =(A&)objID = 1280 text = BObject2
  30.   
  31. --- ptrBObj1-> operator =(* ptrBObj2):
  32.   
  33. A :: operator =(A&)objID = 1280 text = BObject2
  34.   
  35. --- ptrAObj1-> operator =(* ptrBObj2):
  36.   
  37. B :: operator =(A&)objID = 1280 text = BObject2
  38.   
  39. ---- TearDown()
  40.   
  41. B :: Dtor objID = 1280 text = BObject2
  42.   
  43. A :: Dtor objID = 1280 text = BObject2
  44.   
  45. B :: Dtor objID = 3279 text = BObject2
  46.   
  47. A :: Dtor objID = 3279 text = BObject2
  48.   

代码:

#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();
}

2 个答案:

答案 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&)