c++ - 通过重载 operator+ C++ 添加两个不同的对象

标签 c++

我一直在尝试弄清楚如何将对象 A 的私有(private)成员添加到对象 B 的私有(private)成员。

Cat 和 Dog 类都继承自基类 Animal。我有一个第三类“MyClass”,我想继承 Cat 和 Dog 类的私有(private)成员。所以在 MyClass 中,我有一个友元函数来重载 + 运算符。友元函数定义如下:

MyClass operator+(const Dog &dObj, const Cat &cObj);

我想在上面的函数中访问 dObj.age 和 cObj.age,在 main 中通过这个语句调用:

mObj = dObj + cObj;

这是对类对象的完整引用的完整源代码:

   #include <iostream>
   #include <vld.h>

   using namespace std;

   class Animal
   {
   public :
    Animal() {};
    virtual void eat()  = 0 {};
    virtual void walk() = 0 {};
   };

   class Dog : public Animal
   {
   public :
    Dog(const char * name, const char * gender, int age);
    Dog() : name(NULL), gender(NULL), age(0) {};
    virtual ~Dog();
    void eat();
    void bark();
    void walk();

   private :
    char * name;
    char * gender;
    int age;
   };

   class Cat : public Animal
   {
   public :
    Cat(const char * name, const char * gender, int age);
    Cat() : name(NULL), gender(NULL), age(0) {};
    virtual ~Cat();
    void eat();
    void meow();
    void walk();
   private :
    char * name;
    char * gender;
    int age;
   };

   class MyClass : private Cat, private Dog
   {
   public :
    MyClass() : action(NULL) {};
    void setInstance(Animal &newInstance);
    void doSomething();

    friend MyClass operator+(const Dog &dObj, const Cat &cObj);

   private :
    Animal * action;
   };

   Cat::Cat(const char * name, const char * gender, int age) : 
     name(new char[strlen(name)+1]), gender(new char[strlen(gender)+1]), age(age)
   {
    if (name)
    {
     size_t length = strlen(name) +1;
     strcpy_s(this->name, length, name);
    }
    else name = NULL;
    if (gender)
    {
     size_t length = strlen(gender) +1;
     strcpy_s(this->gender, length, gender);
    }
    else gender = NULL;
    if (age)
    {
     this->age = age;
    }
   }
   Cat::~Cat()
   {
    delete name;
    delete gender;
    age = 0;
   }
   void Cat::walk()
   {
    cout << name << " is walking now.. " << endl;
   }
   void Cat::eat()
   {
    cout << name << " is eating now.. " << endl;
   }
   void Cat::meow()
   {
    cout << name << " says meow.. " << endl;
   }
   Dog::Dog(const char * name, const char * gender, int age) : 
     name(new char[strlen(name)+1]), gender(new char[strlen(gender)+1]), age(age)
   {
    if (name)
    {
     size_t length = strlen(name) +1;
     strcpy_s(this->name, length, name);
    }
    else name = NULL;
    if (gender)
    {
     size_t length = strlen(gender) +1;
     strcpy_s(this->gender, length, gender);
    }
    else gender = NULL;
    if (age)
    {
     this->age = age;
    }
   }
   Dog::~Dog()
   {
    delete name;
    delete gender;
    age = 0;
   }
   void Dog::eat()
   {
    cout << name << " is eating now.. " << endl;
   }
   void Dog::bark()
   {
    cout << name << " says woof.. " << endl;
   }
   void Dog::walk()
   {
    cout << name << " is walking now.." << endl;
   }


   void MyClass::setInstance(Animal &newInstance)
   {
    action = &newInstance;
   }
   void MyClass::doSomething()
   {
    action->walk();
    action->eat();
   }


   MyClass operator+(const Dog &dObj, const Cat &cObj)
   {
    MyClass A;
    //dObj.age;
    //cObj.age;
    return A; 
   }

   int main()
   {
    MyClass mObj;
    Dog dObj("B", "Male", 4);
    Cat cObj("C", "Female", 5);

    mObj.setInstance(dObj); // set the instance specific to the object.
    mObj.doSomething();  // something happens based on which object is passed in
    dObj.bark();

    mObj.setInstance(cObj);
    mObj.doSomething();
    cObj.meow();

    mObj = dObj + cObj;

    return 0;
   }

最佳答案

如果你想访问 Dog 的私有(private)成员,那么你的运算符(operator)必须是 Dog 的友元,而不仅仅是 Dog 的某个派生类的友元。

关于c++ - 通过重载 operator+ C++ 添加两个不同的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2603576/

相关文章:

c++ - 内置函数查找距离

c++ - 使用 __LINE__ 为不同的变量名创建宏

c++ - 为什么我的字符串值存储结构中的所有char数组?

c++ - 将c++代码从Linux移植到Windows时需要注意哪些要点?

c++ - 所有与模板化类型交互的代码都必须使用模板吗?

c++ - 从 C++ 中的 vector 获取指针 vector

C++:链接器错误: undefined reference 仅对单独文件中定义的一个特定类成员

c++ - 不正确的浮点舍入

c# - 从 C++ 调用 C# 函数是否创建编译代码

c++ - C++0x forward_list在不同场景下的表现如何?