473,698 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

5 New Member
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 1972
Banfa
9,065 Recognized Expert Moderator Expert
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 Recognized Expert Moderator Expert
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 New Member
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
6429
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 std::endl;
2
2304
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 in Rectangle, the compiler tells me Point::x is protected. I would have expected Rectangle to see the protected members of any Point. Compiling the following code give me this error: g++ -o rectangle main.cc main.cc: In member function `size_t...
6
1779
by: bazley | last post by:
Why doesn't this work in g++? class Base { public: Base() {} ~Base() {} protected: int foo; }
13
7720
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. They don't actually protect any encapsulation or anything, and for all the actual protection they offer, they might as well be public. For example: class B { protected:
14
2470
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 void (safe_bool_base::*bool_type)() const; void this_type_does_not_support_comparisons() const {} safe_bool_base() {}
6
4132
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 functions of another (same type) - but not via a base class pointer.... I've checked the FAQs, Meyers etc but nothing obvious I can find explains it.
2
1937
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 its base class only through a derived object. The derived class has no special access to the protected members of base type objects." He gives the following example: =========================================================
15
3079
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: 'base::~base' : cannot access protected member declared in
10
4103
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 allowed to access the protected data members of the base object. This surprises me. Can someone explain why this is? I suspect there is a good reason and I am just having a slow day to not come up with it myself. Bob
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.