473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

5
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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. class Base
  6. {
  7.   protected:
  8.     int i;
  9.     int n;
  10.  
  11.   public:
  12.     Base(int _i, int _n)
  13.     {
  14.       i = _i;
  15.       n = _n;
  16.     }
  17.     void Print()
  18.     {
  19.       cout << "i = " << i << endl;
  20.       cout << "n = " << n << endl;
  21.     }
  22. };
  23.  
  24.  
  25. Base MakeBase()
  26. {
  27.   /* This is originally a member function of a class,
  28.      and the values are protected members of this class */
  29.   return Base(1,2);
  30. }
  31.  
  32.  
  33. class Derived : public Base
  34. {
  35.   public:
  36.     /* ... or any other function that adds
  37.        functionality to the base class */
  38.     void Increase()
  39.     {
  40.       i++;
  41.       n++;
  42.     }
  43. };
  44.  
  45.  
  46. int main()
  47. {
  48.  
  49.   cout << "Base class:\n========" << endl;
  50.   Base base_class = MakeBase();
  51.   base_class.Print();
  52.  
  53.   cout << "----" << endl;
  54.  
  55.   cout << "Derived class:\n========" << endl;
  56.   Derived derived_class = MakeBase(); /* This obviously isn't valid, but it illustrates what I want */
  57.   derived_class.Print();
  58.  
  59.   return 0;
  60. }
  61.  
Sep 19 '08 #1
3 1961
Banfa
9,065 Expert Mod 8TB
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.
Sep 19 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 19 '08 #3
Edan
5
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
Sep 19 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Christian Meier | last post by:
Hi dear programmers I looked for the difference between private and protected inheritance, but couldn't find anything. Here is my sample code: #include <iostream> using std::cout; using...
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
6
by: bazley | last post by:
Why doesn't this work in g++? class Base { public: Base() {} ~Base() {} protected: int foo; }
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
14
by: mlimber | last post by:
In an article on the safe bool idiom (http://www.artima.com/cppsource/safeboolP.html), Bjorn Karlsson gives the following code (slightly modified): class safe_bool_base { protected: typedef...
6
by: Rick | last post by:
Hi, Can anyone explain to me why the below fails to compile - seeing otherA->f(); as a call to a inaccessible function, while otherB->f(); is ok? It seems you can happily access protected...
2
by: t | last post by:
Lippman's C++ Primer, 4th ed., p562, dicussion of protected members seems to be wrong, unless I am misinterpreting things. He says: "A derived class object may access the protected members of...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.