Hello
If there a derived class privately inheriting from a base class, there
is no subtyping relationship between them. So a funtion that expects a
base class pointer should not accept a derived class pointer.
However, if this same function is called from within a derived class
function, passing 'this', then the function accepts it. Why is that. I
enclose some test code:
#include <iostream>
using std::cout;
class Base {
friend class FriendClass;
private:
virtual void print() const { cout << "In Base\n"; }
};
class FriendClass {
public:
void print(Base * pb) { pb->print(); }
};
class Derived : private Base { // note private
void print() const { cout << "In Derived 2\n"; }
public:
void printEx(FriendClass & rf, Derived * pd) {
rf.print(pd); // why does this work ?
rf.print(this); // why does this work ?
}
};
int main()
{
FriendClass of;
Derived od;
//Base * pb = new Derived; // will not work, base inaccessible
//of.print(&od); // does not work
od.printEx(of, &od); // works
return 0;
}
Here, if print is called from main, being passed &od, it does not
compile. If print is called from printEx, being passed this, it
compiles. Why ?
I used g++ 3.2.
Regards & Thanks
--
Ragnar 2 2213
On 7 Nov 2003 01:31:40 -0800, Ragnar <at*******@yahoo.com> wrote: Hello
If there a derived class privately inheriting from a base class, there is no subtyping relationship between them. So a funtion that expects a base class pointer should not accept a derived class pointer.
However, if this same function is called from within a derived class function, passing 'this', then the function accepts it. Why is that. I enclose some test code:
#include <iostream>
using std::cout;
class Base { friend class FriendClass; private: virtual void print() const { cout << "In Base\n"; } };
class FriendClass { public: void print(Base * pb) { pb->print(); } };
class Derived : private Base { // note private void print() const { cout << "In Derived 2\n"; } public: void printEx(FriendClass & rf, Derived * pd) { rf.print(pd); // why does this work ? rf.print(this); // why does this work ?
conversion Derived* -> Base* is private in Derived, will not be inherited
but exist in Derived.
Function print in FriendClass calls print through the pointer to Base,
that is Derived::print will be called.
} };
int main() { FriendClass of; Derived od; //Base * pb = new Derived; // will not work, base inaccessible //of.print(&od); // does not work od.printEx(of, &od); // works return 0; }
Here, if print is called from main, being passed &od, it does not compile. If print is called from printEx, being passed this, it compiles. Why ?
I used g++ 3.2.
Regards & Thanks
-- Ragnar
--
grzegorz at*******@yahoo.com (Ragnar) wrote in message news:<9c**************************@posting.google. com>... Hello
If there a derived class privately inheriting from a base class, there is no subtyping relationship between them. So a funtion that expects a base class pointer should not accept a derived class pointer.
However, if this same function is called from within a derived class function, passing 'this', then the function accepts it. Why is that. I enclose some test code:
see in code #include <iostream>
using std::cout;
class Base { friend class FriendClass; private: virtual void print() const { cout << "In Base\n"; } };
class FriendClass { public: void print(Base * pb) { pb->print(); } };
class Derived : private Base { // note private void print() const { cout << "In Derived 2\n"; } public: void printEx(FriendClass & rf, Derived * pd) { rf.print(pd); // why does this work ?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pd is Derived* so it calls the Derived::print
rf.print(this); // why does this work ?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is Derived* so it calls the Derived::print
} };
int main() { FriendClass of; Derived od; //Base * pb = new Derived; // will not work, base inaccessible //of.print(&od); // does not work od.printEx(of, &od); // works
return 0; }
Here, if print is called from main, being passed &od, it does not compile. If print is called from printEx, being passed this, it compiles. Why ?
the output of the program is:
In Derived 2
In Derived 2 I used g++ 3.2.
Regards & Thanks
It does what is supposed to do. Check again your code.
/dan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mike Meng |
last post by:
hi all,
I'm a newbie Python programmer with a C++ brain inside. I have a
lightweight framework in which I design a base class and expect user to
extend. In other part of the framework, I heavily...
|
by: Dave Theese |
last post by:
Hello all,
The example below demonstrates proper conformance to the C++ standard.
However, I'm having a hard time getting my brain around which language rules
make this proper...
The error...
|
by: Tony Johansson |
last post by:
Hello!
Private inheritance is sometimes called implementation inheritance.
If you use this private inheritance how is with the usage of overriding
then.
Is overriding used less often when...
|
by: alf |
last post by:
is there any way to tell the class the base class during runtime?
a.
|
by: Lars |
last post by:
Hi,
I have a base class holding a generic list that needs
to be accessed by both the base class and its subclasses.
What is the best solution to this?
I am fairly new to generics, but I am...
|
by: karthikbalaguru |
last post by:
Hi,
Could someone here tell me some links/pdfs/tutorials to know about the
difference between Private Inheritance and Public Inheritance ?
I am unable to get info w.r.t it.
Thx in advans,...
|
by: puzzlecracker |
last post by:
The statement is taken from FAQ . What about non-virtual
functions? Can they be overriden? I still don't see a good
justification to prefer private inheritance over composition. In
fact, I have...
|
by: snewman18 |
last post by:
In learning about design patterns, I've seen discussion about using
inheritance when an object's relationship to another object is 'is-a'
and composition when the relationship is 'has-a'.
Since...
|
by: Jay Dee |
last post by:
Hi all.
I have bean studying the use of Interfaces, and from what I understand
if you create a class that inherits from an interface, that class must
poses a member that represents each member...
|
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: 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: 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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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: 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...
|
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...
| |