473,381 Members | 1,615 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,381 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  5. class Base
  6. {
  7.   protected:
  8.     int i;
  9.     int n;
  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. };
  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. }
  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. };
  46. int main()
  47. {
  49.   cout << "Base class:\n========" << endl;
  50.   Base base_class = MakeBase();
  51.   base_class.Print();
  53.   cout << "----" << endl;
  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();
  59.   return 0;
  60. }
Sep 19 '08 #1
3 1959
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
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
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.

Sep 19 '08 #4

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

Similar topics

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...
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...
by: bazley | last post by:
Why doesn't this work in g++? class Base { public: Base() {} ~Base() {} protected: int foo; }
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. ...
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...
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...
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...
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:...
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...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.