|
-
August 10th, 2002, 03:50 AM
#1
Inheritance
How to make a class non-inheritable but still create objects of that
class ?
-
August 10th, 2002, 07:46 AM
#2
this may help u
hi !!!
i don't know whether is there any good mechanism in c++ to achieve this (as final in java) but you can get the same thing done by this way
first make the constructor as private now this won't allow any other class to inherit that class and if u want other classes to create the objects provide one static method for that
here is the example of the same
#include <iostream.h>
class Base
{
private:
Base()
{
}
public :
static Base ConstructorBase()
{
Base a1;
return a1;
}
void fun()
{
cout<<"in base fun";
}
};
class Der ublic Base
{
public :
// error
/*
Der()
{
}
*/
};
void main()
{
Base *aObj=& Base::ConstructorBase();
aObj->fun();
delete aObj;
}
jayostu
-
August 10th, 2002, 07:18 PM
#3
How about
Code:
class
{
public:
...
} object1, object2, object3;
You cannot inherit from it if you don't give it a name. The only problem with this is it only has a default constructor. The following does not work: you can still inherit from a typedef. Something I only just found out.
Code:
typedef class
{
public:
...
} Sterile;
Last edited by cup; August 11th, 2002 at 05:08 AM.
Succinct is verbose for terse
-
August 11th, 2002, 12:46 PM
#4
Here is a simple technique which i have already post on codeguru in VC programming forum.
Code:
class temp
{
private:
temp() { };
friend class MyClass;
};
class MyClass : virtual public temp
{
// do whatever you want
};
Now MyClass become a final class, i.e. you can make object of this but cant inherit class from it.
Hope it helps.
-
August 12th, 2002, 12:20 AM
#5
finally
hi all !!!
i think all of us want to tell one thing that is make the constructor private. later things are as per ur choice whether should make a static function or again a new class
bye
jayostu
-
August 12th, 2002, 05:05 AM
#6
Why bother? You should only inherit publicly from a class that has been designed for public inheritance - that is, it has a virtual destructor and (probably) one or more virtual functions. Yes, you can prevent it by making the constructor private, but that ties you to heap-based allocation of your objects. Look at the STL - none of the container classes are designed to be publicly inherited from, so you just don't do it. Too much is made of public inheritance in OO programming - it's just one aspect, and not a major one at that. Personally, I wouldn't take any special steps to prevent public inheritance from a class. Special steps are taken to allow public inheritance. If someone does decide to inherit from a class that's not designed for it, then they will suffer the problems of doing something that they shouldn't. Why should I limit my (and others) options just to molly-coddle the occasional idiot who should know better?
Rant over.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
August 12th, 2002, 04:07 PM
#7
just to molly-coddle the occasional idiot
Wow, I can actually feel my vocabulary expanding! I can't wait to use this one!
Although I mostly agree with you, Graham, but it is not obvious to most programmers (read "occasional idiot") that a non-virtual destructor means that the class should not be inherited from. It makes sense after some thought, but it is easy to miss, and you really do need to actively look for it. I didn't make this connection until I started using STL, when the very first thing I did was inherit from a container. Not to worry: my use of STL has since improved.
Also, the class in question may look like a class that would be nice to inherit from. It may be tempting. It's better to have rules enforced by the compiler. It makes the "occasional idiot" walk down the hall and ask what's going on.
That aside, I agree that it's probably not worth limiting the use of the class in order to prevent inheritance unless that inheritance would cause a specific problem.
I would like to see compilers warn against inheriting from a class with a non-virtual destructor. This sounds like something easy enough to implement.
Jeff
-
August 13th, 2002, 04:27 AM
#8
Jeff - if you want to expand your vocabulary a bit more, then "pillock" is my favourite alternative for "idiot". American friends of mine became quite enamoured of that word after I first used it (I think I was referring to D. Quayle at the time).
I quite like the idea of a warning when publicly inheriting from a class with a non-virtual destructor (no problem if privately inheriting). After all, VC++ gives us that pointless "use of 'this' in an initializer list" warning, so why not something useful?
I still think, though, that it comes down to changing people's attitudes to what OO actually means. Inheritance is not the first tool of OO that you should reach for. And, if you do think "oh, that's nice class, I wonder if I can inherit from it?", I would say: resist and consider long and hard whether that makes sense from a design point of view. Presumably, if the class exists, then it's a concrete class - read Scott Meyers on the subject of inheriting from concrete classes. It's more likely that the correct design decision is to extract the common code (the reason you wanted to inherit in the first place) into an abstract class that both classes then inherit from. I don't have time to repeat Meyers's arguments for making all non-leaf classes abstract, but they are pretty compelling.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|