 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Saumya Guest
|
Posted: Tue Feb 10, 2004 1:08 pm Post subject: How to prevent a class from being inheritable, in C++ |
|
|
Hi,
I didn't know how else to express myself in the subject line, but what
I want to know is this: I have a class A, so what do I have to do
to ensure that no one can derive from it?
Saumya
|
|
| Back to top |
|
 |
Hendrik Belitz Guest
|
Posted: Tue Feb 10, 2004 1:10 pm Post subject: Re: How to prevent a class from being inheritable, in C++ |
|
|
Saumya wrote:
| Quote: | Hi,
I didn't know how else to express myself in the subject line, but what
I want to know is this: <b>I have a class A, so what do I have to do
to ensure that no one can derive from it?
Saumya
|
Why do you want to do this? Explaining what you want to achieve with this
behaviour would help to find a meaningful solution.
--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich
|
|
| Back to top |
|
 |
Christian Jaeger Guest
|
|
| Back to top |
|
 |
Philipp Bachmann Guest
|
Posted: Tue Feb 10, 2004 1:50 pm Post subject: Re: How to prevent a class from being inheritable, in C++ |
|
|
As Christian already posted, there are at least three ways to do so. But: There's no way in C++ as simple as
"final" in Java:
The first approach needs additional static member functions, the second comment-only approach only
works iff the user cooperates, and the third one needs a "friend" declaration in a virtual base class, which
isn't nice either. And in my opinion "final"ization imposes a large limitation to the usage of you class.
Therefore think twice, please, whether you can't change your design such not to need to make
your class behave like "final" any more.
Cheers,
Philipp.
"Saumya" <saumyasarpal (AT) yahoo (DOT) com> wrote
| Quote: | Hi,
I didn't know how else to express myself in the subject line, but what
I want to know is this: <b>I have a class A, so what do I have to do
to ensure that no one can derive from it?
Saumya
|
|
|
| Back to top |
|
 |
Karl Heinz Buchegger Guest
|
Posted: Tue Feb 10, 2004 4:18 pm Post subject: Re: How to prevent a class from being inheritable, in C++ |
|
|
"Philipp Bachmann
| Quote: |
As Christian already posted, there are at least three ways to do so. But: There's no way in C++ as simple as
"final" in Java:
The first approach needs additional static member functions, the second comment-only approach only
works iff the user cooperates, and the third one needs a "friend" declaration in a virtual base class, which
isn't nice either. And in my opinion "final"ization imposes a large limitation to the usage of you class.
|
Hmm. So it seems I am not alone.
All always wondered: Why would one want to impose such a 'finalization' to a class. What
is the purpose of doing so?
--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
| Back to top |
|
 |
Jesper Madsen Guest
|
Posted: Tue Feb 10, 2004 10:35 pm Post subject: Re: How to prevent a class from being inheritable, in C++ |
|
|
I think the only good reason is to force users of the class to aggregate
instead of inherit.
Consider a socket implementation, normally a socket class gives no meaning
to inherit from, it is like inheriting from std::iostream..
Sure there maybe where it could make sence, but for common use it doesn't..
Jesper
"Karl Heinz Buchegger" <kbuchegg (AT) gascad (DOT) at> wrote
| Quote: | "Philipp Bachmann
As Christian already posted, there are at least three ways to do so.
But: There's no way in C++ as simple as
"final" in Java:
The first approach needs additional static member functions, the second
comment-only approach only
works iff the user cooperates, and the third one needs a "friend"
declaration in a virtual base class, which
isn't nice either. And in my opinion "final"ization imposes a large
limitation to the usage of you class.
Hmm. So it seems I am not alone.
All always wondered: Why would one want to impose such a 'finalization' to
a class. What
is the purpose of doing so?
--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
|
| Back to top |
|
 |
Saumya Guest
|
Posted: Thu Feb 26, 2004 7:55 am Post subject: Re: How to prevent a class from being inheritable, in C++ |
|
|
Not a very technical reason pal, but it just struck me while I was
learning Java and encountered the "final" keyword there.
Also, it struck me that what if I was asked this question in some
technical interview
|
|
| Back to top |
|
 |
Christopher Benson-Manica Guest
|
Posted: Thu Feb 26, 2004 12:53 pm Post subject: Re: How to prevent a class from being inheritable, in C++ |
|
|
Saumya <saumyasarpal (AT) yahoo (DOT) com> spoke thus:
| Quote: | Not a very technical reason pal, but it just struck me while I was
learning Java and encountered the "final" keyword there.
|
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.8
| Quote: | Also, it struck me that what if I was asked this question in some
technical interview
|
Just memorize the above URL? ;)
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
|
|
| Back to top |
|
 |
Razvan Popovici Guest
|
Posted: Thu Feb 26, 2004 10:41 pm Post subject: Re: How to prevent a class from being inheritable, in C++ |
|
|
I am really wondering who needs such a thing ...
Razvan
"Christopher Benson-Manica" <ataru (AT) nospam (DOT) cyberspace.org> schrieb im
Newsbeitrag news:c1kq80$q3k$1 (AT) chessie (DOT) cirr.com...
| Quote: | Saumya <saumyasarpal (AT) yahoo (DOT) com> spoke thus:
Not a very technical reason pal, but it just struck me while I was
learning Java and encountered the "final" keyword there.
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.8
Also, it struck me that what if I was asked this question in some
technical interview ;-)
Just memorize the above URL? ;)
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
|
|
|
| Back to top |
|
 |
Keith H Duggar Guest
|
Posted: Tue Mar 02, 2004 7:15 am Post subject: Re: How to prevent a class from being inheritable, in C++ |
|
|
| Quote: | Hmm. So it seems I am not alone.
All always wondered: Why would one want to impose such a 'finalization' to a class. What
is the purpose of doing so?
|
No you aren't alone. 'finalization' seems almost equivalent to asking:
"How can I prevent someone from reusing my code?"
The idiom seems to fit well in Java, however. I always do feel like
I'm wearing a straight-jacket when I code in Java. So heck, why not
'finalize' the classes to prevent there reuse. After all, you never
know when someone (possibly even yourself) might try to reuse it
without your permission possibly messing up the aesthetic symmetry
your 'pre-planned' class hierarchy. Shame on them.
"I have opinions, strong opinions, but, I don't always agree with
them."
-- G. W. Bush
|
|
| Back to top |
|
 |
Datta Patil Guest
|
Posted: Tue Mar 02, 2004 12:45 pm Post subject: Re: How to prevent a class from being inheritable, in C++ |
|
|
"Saumya" <saumyasarpal (AT) yahoo (DOT) com> wrote
| Quote: | Not a very technical reason pal, but it just struck me while I was
learning Java and encountered the "final" keyword there.
U mean how to simulate final java class concept in c++.....here is |
program.I guess its similar to creating singleton pattern
// Make ctor private so that class cannot be inheriated
//Use static member function to create object of its own which will return
ptr to same class obj
#include <iostream.h>
class X{
private :
int x;
X(); // private ctor
public :
static X* getMePrivateObject(); // this will help me creating handle rather
for normal object
};
X::X(){
cout<<"in constructor"<
x=0;
};
X* X::getMePrivateObject(){
X *pMyP = new X();
return pMyP;
};
/*class Y : public X //cannot do this as X has private ctor
{
public:
Y(){}
void print(){ cout<<"Display me if u can";}
};
*/
void main(void){
X *pMyP = X::getMePrivateObject();
//Rest of ur code
}
|
|
| Back to top |
|
 |
Datta Patil Guest
|
Posted: Tue Mar 02, 2004 1:10 pm Post subject: Re: How to prevent a class from being inheritable, in C++ |
|
|
Check this program....Class X acts like final class in java.
#include <iostream.h>
class X{
private :
int x;
X(); // private ctor
public :
static X* getMePrivateObject(); // this will help me creating handle rather
for normal object
};
X::X(){
cout<<"in constructor"<
x=0;
};
X* X::getMePrivateObject(){
X *pMyP = new X();
return pMyP;
};
/*class Y : public X //cannot do this as X has private ctor
{
public:
Y(){}
void print(){ cout<<"Display me if u can";}
};
*/
void main(void){
X *pMyP = X::getMePrivateObject();
//Rest of ur code
}
Njoy,
Datta Patil
|
|
| 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
|
|