C++: Copying protected members of base class to derived class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Edan
    New Member
    • Dec 2007
    • 5

    C++: Copying protected members of base class to derived class

    I have a base class with protected members (`Base`). The function `MakeBase()` is a member function of another class, that returns a `Base` object initialized with private members of this class.
    Now the thing is, I want to extend the functionality of `Base` class. I do that by inheriting `Base` class by `Derived`, but then I can't use `MakeBase()` function. It all makes sense to me, but I don't seem to find an appropriate solution.
    Since I don't want to give any access to the private / protected members of these classes, I don't see how I can implement copy constructors
    So I'm basically asking for suggestions. Maybe this is just bad design and I need to think differently (the `MakeBase()` thing).

    This code gives the basic idea:

    Code:
    #include <iostream>
    using namespace std;
    
    
    class Base
    {
      protected:
        int i;
        int n;
    
      public:
        Base(int _i, int _n)
        {
          i = _i;
          n = _n;
        }
        void Print()
        {
          cout << "i = " << i << endl;
          cout << "n = " << n << endl;
        }
    };
    
    
    Base MakeBase()
    {
      /* This is originally a member function of a class,
         and the values are protected members of this class */
      return Base(1,2);
    }
    
    
    class Derived : public Base
    {
      public:
        /* ... or any other function that adds
           functionality to the base class */
        void Increase()
        {
          i++;
          n++;
        }
    };
    
    
    int main()
    {
    
      cout << "Base class:\n========" << endl;
      Base base_class = MakeBase();
      base_class.Print();
    
      cout << "----" << endl;
    
      cout << "Derived class:\n========" << endl;
      Derived derived_class = MakeBase(); /* This obviously isn't valid, but it illustrates what I want */
      derived_class.Print();
    
      return 0;
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Create an operator= in the base class that takes a type of Base &

    Create an operator= in the derived class that takes a type of Base &, with-in this operator call the Base class operator= using the passed parameter.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You are trying to implement the Factory design pattern. In this case, an Abstract Factory.

      You might want to Google on this or get a design patterns book.

      Comment

      • Edan
        New Member
        • Dec 2007
        • 5

        #4
        I see. I've changed a couple of things to get a solution, and it's fine, but I will read about design patterns anyway.

        Thanks,
        Edan

        Comment

        Working...