How to create class which can't be inherited

K

KP

How can we create class which other class can't be inherited or we can say
how to protect class from inheritance ? ( e.g. final class in java, if a
class is final in java, then this class can't be inherited)

Ketan
 
K

KP

No, this is not ajay. I don't know if this question has been already asked
in this group. Do you know any solution of this?
 
G

Gavin Deane

Alex said:
I tried writing a class that can't be inherited but can be used but
couldn't get it to work; perhaps I'm not understanding this properly.

I think that FAQ has been around for some time, and I've seen it
referred to before. I would expect any mistakes in it to have been
caught by now. I can't say for definite from my own experience, having
never needed to write a "final" class.

Post a minimal complete program
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
that uses the techniques in the FAQ but doesn't work as you'd like and
you should get some help.

Gavin Deane
 
A

Alex Buell

Post a minimal complete program
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
that uses the techniques in the FAQ but doesn't work as you'd like and
you should get some help.

I think I've got the idea:

#include <iostream>

class NotInheritable
{
public:
static NotInheritable create() { return NotInheritable(); };

private:
NotInheritable() { std::cout << "NotInheritable::NotInheritable
()" << std::endl; };
};


class TryToInherit : NotInheritable
{
public:
TryToInherit() { std::cout << "TryToInherit::TryToInherit()" <<
std::endl; };
};

int main()
{
NotInheritable ni = NotInheritable::create();
return 0;
}
 
A

Axter

Here's example code for an alternative method:

class FinalClass
{
public:
static FinalClass * Create_FinalClass();
private:
FinalClass(const FinalClass&);
FinalClass& operator=(const FinalClass&);
FinalClass(){}
};

FinalClass* FinalClass::Create_FinalClass()
{
return new FinalClass();
}

int main(int argc, char* argv[])
{
FinalClass *f = FinalClass::Create_FinalClass();
delete f;
//Or safer method, use auto pointer
std::auto_ptr<FinalClass> f_safe(FinalClass::Create_FinalClass());

return 0;
}
 
B

Bo Persson

KP said:
No, this is not ajay. I don't know if this question has been already
asked in this group. Do you know any solution of this?

The question is WHY do you want this?

In Java there is a reason to make the class final, beacuse it allows
the code to be optimized when we know that the virtual functions
cannot be overridden by a subclass.

In C++ you can avoid this by not making the functions virtual in the
first place.


If the object is just that inhereting from your class won't work, I
recommend solution #2 from the FAQ:
State in the comments/documentation that inhereting is not allowed -
"Me and my baseball bat will visit any violators!".


Bo Persson
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top