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:
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;
}
Comment