ban of inheritance

Hi!
I have found question: How do I forbid opportunity of inheritance, using virtual inheritance?
I thought that it's impossible in C++.
Like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Unoverloadable;

class Base
{
    private:
        friend Unoverloadable;
        Base(){}
};

class Unoverloadable : public virtual Base
{
    // class definition
};


now since derived classes need access to their base class constructors, and only Unoverloadable can access Base as its constructor is private, nothing can be derived from Unoverloadable.

May I ask why exactly you want to prevent inheritance?
Does anyone know if C++ is getting a final keyword like java in C++ 0x? Or for that matter anything else interesting that's coming to 0x
Last edited on
I know auto is coming (as Stroustrup intended way back in the mists of time when he started all this, apparently). I don't know if I'd call it interesting; I've never really minded having to declare a type when I create an object.
Never heard of auto but I looked it up and to me it just makes things less readable =/
Not necessarily. What's the most readable version:

for(auto it = vec.begin(); it != vec.end(); ++it)

or

for(std::vector<std::vector<MyClass> >::iterator it = vec.begin(); it != vec.end(); ++it)

?
Although I haven't heard of a "final" keyword for C++, I would be really disappointed if it was implemented. That's a feature of Java that I really didn't like. Can't we point guns at our feet when we want to?

-Albatross
Tbh, final is dumb. Who are you to say that no-one should be able to derive from your class?
the original author. (hence the owner of the intellectual property of the code)
Last edited on
I'd have liked to keep restrictions on the uses of individual pieces of code and tiny sub-sections of a library out of open source software.

-Albatross
Doesn't have to be open source. Could be closed source with an interface for the library. Don't get me wrong I'm all for the current c++ philosophy and completely unrestricted code. Just playing the devils advocate here.
Just playing the devils advocate here.

Where's my holy water? I need to sprinkle some on the seraph. ;)

I'm pretty sure Java has a keyword for creating interfaces separate from the implementations, but I haven't touched the language in so long I almost forgot.

-Albatross
Last edited on
auto... I already sorta made something like that:
#define let(x,y) typeof(y) x=y
Also, if you don't want people to inherit, why not just do this:
class Foo{//If you inherit, I'll hunt you down and eat your soul
(Using whatever threat you can get away with, obviously.)
@Albatross: It does. It's basically a class with only (pure?) virtual functions. Since Java doesn't support multiple inheritance it's the only way to do some things in it.
Last edited on
May I ask why exactly you want to prevent inheritance?

I haven’t any desire to do that. It’s only theoretical question. I have found it in one questions list for test C++ knowledge, and can’t get answer.
By the way, another theoretical question.
In Java, keyword ‘final’ also can prevent the redefining of method. Is there any trick in C++ to do the same?
This works for me, but I don't know if it's portable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
template <class T>
struct FriendMaker { typedef T Type; };

template <class T>
class Uninheritable
{
    //compiler bug \/
    friend class FriendMaker<T>::Type;
    ~Uninheritable(){}
};

class NotABase: virtual public Uninheritable<NotABase> {};
class NotADerived: public NotABase {};

int main()
{
    NotABase a;

    //uncommenting any of the following statements
    //results in a compilation error

    //NotADerived b;
    //NotADerived * pb=new NotADerived;
    //NotABase * pa=new NotADerived;

    return 0;
}

EDIT: Forgot to put the virtual keyword there...
It forces NotADerived to call the Uninheritable<NotABase> dtor, which causes an error.
Last edited on
1
2
//compiler bug \/
friend class FriendMaker<T>::Type;

In Visual Studio you can use the
Friend T;
Without using of auxiliary FriendMaker.
Topic archived. No new replies allowed.