C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to Avoid inheritence ?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Rohit Dhamija
Guest





PostPosted: Mon Aug 23, 2004 10:35 am    Post subject: How to Avoid inheritence ? Reply with quote



Dear all,

I have a small query, an application whose a structure is like

class vehicle{
...
};
class car : public vehicle{
...
};
class maruti : public car {
....
};

I want to add functionality such that user could not derive another
class fom maruti class ,i.e compiler should generate error if user
tries to inherit maruti

Although, the user should be able to make objects of the maruti class.

Is there any way to accomplish the same ? Please comment!

Thanks & Regards,
Rohit

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Stefan Heinzmann
Guest





PostPosted: Mon Aug 23, 2004 10:05 pm    Post subject: Re: How to Avoid inheritence ? Reply with quote



Rohit Dhamija wrote:

Quote:
Dear all,

I have a small query, an application whose a structure is like

class vehicle{
..
};
class car : public vehicle{
..
};
class maruti : public car {
...
};

I want to add functionality such that user could not derive another
class fom maruti class ,i.e compiler should generate error if user
tries to inherit maruti

Although, the user should be able to make objects of the maruti class.

Is there any way to accomplish the same ? Please comment!

Yes, but you may not like it ;-)

One idea would be to make the constructors private and offer a factory
method instead to create objects of the maruti class:

class maruti : public car {
private:
maruti(...);
public:
maruti* make(); // factory function
....
};

Another idea is to use the rules of virtual inheritance in a clever way:

class maruti_derive_blocker {
friend class maruti;
maruti_derive_blocker() {}
};

class maruti : public car, virtual maruti_derive_blocker {
....
};

Both have their disadvantages, a simple lightweight method of
accomplishing your goal is missing in C++, unfortunately.

--
Cheers
Stefan

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Mon Aug 23, 2004 10:22 pm    Post subject: Re: How to Avoid inheritence ? Reply with quote



Rohit Dhamija wrote:
Quote:
I have a small query, an application whose a structure is like

class vehicle{
..
};
class car : public vehicle{
..
};
class maruti : public car {
...
};

I want to add functionality such that user could not derive another
class fom maruti class ,i.e compiler should generate error if user
tries to inherit maruti

Although, the user should be able to make objects of the maruti class.

Is there any way to accomplish the same ? Please comment!

Private constructor along with the "factory method".

Victor

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Gerd Orfey
Guest





PostPosted: Mon Aug 23, 2004 10:27 pm    Post subject: Re: How to Avoid inheritence ? Reply with quote

Rohit Dhamija wrote:
Quote:
Dear all,

I have a small query, an application whose a structure is like

class vehicle{
..
};
class car : public vehicle{
..
};
class maruti : public car {
...
};

I want to add functionality such that user could not derive another
class fom maruti class ,i.e compiler should generate error if user
tries to inherit maruti

Although, the user should be able to make objects of the maruti class.


See Stroustrups Style & Technique FAQ:
http://www.research.att.com/~bs/bs_faq2.html#no-derivation

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Mike Capp
Guest





PostPosted: Mon Aug 23, 2004 10:30 pm    Post subject: Re: How to Avoid inheritence ? Reply with quote

[email]rohit_dhamija (AT) rediffmail (DOT) com[/email] (Rohit Dhamija) wrote in message news:<e77b983b.0408230053.220debb9 (AT) posting (DOT) google.com>...

Quote:
I want to add functionality such that user could not derive another
class fom maruti class ,i.e compiler should generate error if user
tries to inherit maruti

Although, the user should be able to make objects of the maruti class.

Is there any way to accomplish the same ? Please comment!

Yes, there's an old trick which (ab)uses virtual inheritance to
achieve this. See, for example, Bjarne Stroustrup's description at
http://www.research.att.com/~bs/bs_faq2.html#no-derivation

Do think very, very hard about your reasons for wanting to do this,
though. There may be situations where it's appropriate, but I don't
recall ever seeing one.

cheers,
Mike

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Srik
Guest





PostPosted: Mon Aug 23, 2004 10:37 pm    Post subject: Re: How to Avoid inheritence ? Reply with quote

[email]rohit_dhamija (AT) rediffmail (DOT) com[/email] (Rohit Dhamija) wrote in message news:<e77b983b.0408230053.220debb9 (AT) posting (DOT) google.com>...

Quote:
I want to add functionality such that user could not derive another
class
...


see
"How can I set up my class so it won't be inherited from?"
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.8

-srik

Quote:

Thanks & Regards,
Rohit

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
John Pazarzis
Guest





PostPosted: Mon Aug 23, 2004 10:41 pm    Post subject: Re: How to Avoid inheritence ? Reply with quote

Try this:

class Final
{
private:
Final()
{
}
friend class maruti;
};

class vehicle
{

};

class car : public vehicle
{

};

class maruti : public car , virtual public Final
{
};

class derived: public maruti
{
};



void main ()
{

maruti m;
derived d; // The compiler will complain.....
}




[email]rohit_dhamija (AT) rediffmail (DOT) com[/email] (Rohit Dhamija) wrote in message news:<e77b983b.0408230053.220debb9 (AT) posting (DOT) google.com>...
Quote:
Dear all,

I have a small query, an application whose a structure is like

class vehicle{
..
};
class car : public vehicle{
..
};
class maruti : public car {
...
};

I want to add functionality such that user could not derive another
class fom maruti class ,i.e compiler should generate error if user
tries to inherit maruti

Although, the user should be able to make objects of the maruti class.

Is there any way to accomplish the same ? Please comment!

Thanks & Regards,
Rohit

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
James Hopkin
Guest





PostPosted: Mon Aug 23, 2004 10:42 pm    Post subject: Re: How to Avoid inheritence ? Reply with quote

[email]rohit_dhamija (AT) rediffmail (DOT) com[/email] (Rohit Dhamija) wrote in message news:<e77b983b.0408230053.220debb9 (AT) posting (DOT) google.com>...
Quote:

I want to add functionality such that user could not derive another
class fom maruti class ,i.e compiler should generate error if user
tries to inherit maruti


<http://www.research.att.com/~bs/bs_faq2.html#no-derivation>

A fantastic FAQ, by the way. Worth reading over and over, whether
you're a beginner or an expert.


James

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bronek Kozicki
Guest





PostPosted: Mon Aug 23, 2004 11:04 pm    Post subject: Re: How to Avoid inheritence ? Reply with quote

Rohit Dhamija wrote:


Quote:
I want to add functionality such that user could not derive another
class fom maruti class ,i.e compiler should generate error if user
tries to inherit maruti

most compilers should understand following:

class _block_maruti_inheritance_
{
_block_maruti_inheritance_() {} // private!
friend class maruti;
};

class vehicle {
// ...
};

class car : public vehicle {
// ...
};

class maruti : public car,
virtual private _block_maruti_inheritance_ {
// ...
};

class D : public maruti {};

int main()
{
maruti m;
D d; //!
}


error is thrown at the point where constructor of class D is defined -
in above example it's generated by compiler at line //! but I think
that's not a big problem.


B.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Anand Hariharan
Guest





PostPosted: Tue Aug 24, 2004 6:33 am    Post subject: Re: How to Avoid inheritence ? Reply with quote

[email]rohit_dhamija (AT) rediffmail (DOT) com[/email] (Rohit Dhamija) wrote in message news:<e77b983b.0408230053.220debb9 (AT) posting (DOT) google.com>...
[...]
Quote:

I want to add functionality such that user could not derive another
class fom maruti class ,i.e compiler should generate error if user
tries to inherit maruti

Although, the user should be able to make objects of the maruti class.

Is there any way to accomplish the same ? Please comment!


Read Stroustrup's FAQ:
http://www.research.att.com/~bs/bs_faq2.html#no-derivation

HTH,
- Anand

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.