473,385 Members | 1,275 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.

How can I set up my class so it won't be inherited from?

http://www.parashift.com/c++-faq-lit...aq-23.11offers
a solution of the problem:

class Fred;

class FredBase {
private:
friend class Fred;
FredBase() { }
};

class Fred : private virtual FredBase {
public:
...
};
The only thing I want to know is why did he make FredBase a virtual
base. It works even if inheritance is non-virtual. Why virtual
inheritance is better??

Jun 8 '07 #1
5 1370
Nikolay Kurtov wrote:
http://www.parashift.com/c++-faq-lit...aq-23.11offers
a solution of the problem:

class Fred;

class FredBase {
private:
friend class Fred;
FredBase() { }
};

class Fred : private virtual FredBase {
public:
...
};
The only thing I want to know is why did he make FredBase a virtual
base. It works even if inheritance is non-virtual. Why virtual
inheritance is better??
How does it work if the inheritance non-virtual? What error does
your compiler report here:

class Fred;

class FredBase {
private:
friend class Fred;
FredBase() { }
};

class Fred : private /*virtual*/ FredBase {
};

class Foo : public Fred {};

class Bar : private Fred {};

int main() {
Foo foo;
Bar bar;
}

I've derived TWO classes from Fred and my compiler had no problem
letting me.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '07 #2
I've derived TWO classes from Fred and my compiler had no problem
letting me.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -
Sorry, it really produces an error.
Now I really confused and cannot understand why it is possible to
derive.
Isn't it true, that a usual inheritance runs first constructor of the
base class and then of the derived one?

Jun 8 '07 #3
Sorry, it really produces an error.
I meant without "virtual" it IS possible to derive from Fred.

Jun 8 '07 #4
Nikolay Kurtov wrote:
>I've derived TWO classes from Fred and my compiler had no problem
letting me.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide
quoted text -

- Show quoted text -

Sorry, it really produces an error.
Who "it"? What error?
Now I really confused and cannot understand why it is possible to
derive.
It's possible to derive from _my_ Fred (that doesn't have 'FredBase'
as virtual base class) because NOTHING prevents you from that.
Isn't it true, that a usual inheritance runs first constructor of the
base class and then of the derived one?
The virtual base class subobject is supposed to be _initialised_ in
the most derived class' constructor. If you derive from [the original]
'Fred', say a class 'Foo', then 'Foo::Foo' is responsible for
initialising the 'FredBase' (even if it happens implicitly), and since
'FredBase's constructor is private, it's not accessible to 'Foo' (or
any other class derived from 'Fred' for that matter). That's why you
cannot derive from 'Fred' given in the FAQ. The inability to initialise
the virtual base class in any derived from 'Fred' class makes 'Fred'
non-derivable.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '07 #5
OK, thanks a lot.

Jun 8 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pavils Jurjans | last post by:
Hallo, Is there some decent way how to get the object class name in a string format? Currently I use this: function getClassName(obj) { if (typeof obj != "object" || obj === null) return...
4
by: Steve Amey | last post by:
Hi all I am creating a basic control to perform some tasks, and I want to declare some events to be raised so they can be handled from the form that the control is on. I can create my own Event...
2
by: owingruters | last post by:
Hi all, I'm trying to hide a member from a base class, like so : public class class1 { public class1() { } public int i; }
8
by: TS | last post by:
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give me the code to do it? something like this?...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
6
by: **Developer** | last post by:
usually I'd do: Drawing.Image.FromFile( I noticed I once did without thinking: Drawing.Bitmap.FromFile( I assumed this worked because Bitmap inherits from Image, but for fun I thought I'd...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
12
by: Janaka Perera | last post by:
Hi All, We have done a object oriented design for a system which will create a class multiply inherited by around 1000 small and medium sized classes. I would be greatful if you can help me...
12
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a class which "BiologySequence" which looks about like this. public class BiologySequence { private string _Sequence; public string Sequence {
36
by: puzzlecracker | last post by:
Would someone explain why this declaration is illegal: class Sample<T> where T : Stream, class
1
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...
0
isladogs
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...
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: 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
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
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?
1
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 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.