 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rohit Dhamija Guest
|
Posted: Mon Aug 23, 2004 10:35 am Post subject: How to Avoid inheritence ? |
|
|
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
|
Posted: Mon Aug 23, 2004 10:05 pm Post subject: Re: How to Avoid inheritence ? |
|
|
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
|
Posted: Mon Aug 23, 2004 10:22 pm Post subject: Re: How to Avoid inheritence ? |
|
|
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
|
Posted: Mon Aug 23, 2004 10:27 pm Post subject: Re: How to Avoid inheritence ? |
|
|
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
|
Posted: Mon Aug 23, 2004 10:30 pm Post subject: Re: How to Avoid inheritence ? |
|
|
[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
|
Posted: Mon Aug 23, 2004 10:37 pm Post subject: Re: How to Avoid inheritence ? |
|
|
[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
|
Posted: Mon Aug 23, 2004 10:41 pm Post subject: Re: How to Avoid inheritence ? |
|
|
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
|
Posted: Mon Aug 23, 2004 10:42 pm Post subject: Re: How to Avoid inheritence ? |
|
|
[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
|
Posted: Mon Aug 23, 2004 11:04 pm Post subject: Re: How to Avoid inheritence ? |
|
|
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
|
Posted: Tue Aug 24, 2004 6:33 am Post subject: Re: How to Avoid inheritence ? |
|
|
[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 |
|
 |
|
|
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
|
|