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

时间:2010-04-08 21:26:50

标签: c++

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

Cat和Dog Class都继承自Animal类。我有一个第三类'MyClass',我想继承Cat和Dog类的私有成员。所以在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;
   }

2 个答案:

答案 0 :(得分:2)

如果您想访问Dog的私人会员,那么您的运营商必须是Dog的朋友,而不仅仅是某些派生类Dog的朋友。

答案 1 :(得分:0)

如果您将friend MyClass operator+(const Dog &dObj, const Cat &cObj);添加到类Cat和类Dog的定义中,则该函数可以访问dObj.agecObj.age

或者,您可以将friend MyClass;添加到类Cat和类Dog的定义中,但是MyClass的所有函数都可以访问所有Cat和Dog的内部。

或者,您可以将age设为受保护而非私有。