How to prevent a class from being inheritable, in C++

S

Saumya

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?</b>

Saumya
 
H

Hendrik Belitz

Saumya said:
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?</b>

Saumya

Why do you want to do this? Explaining what you want to achieve with this
behaviour would help to find a meaningful solution.
 
P

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.

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.
 
K

Karl Heinz Buchegger

"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?
 
J

Jesper Madsen

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
 
S

Saumya

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 ;-)
 
K

Keith H Duggar

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
 
D

Datta Patil

Saumya said:
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"<<endl;

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

}
 
D

Datta Patil

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"<<endl;
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top