Hi Folks,

I want to avoid a class from being inherit but instantiating it should be possible. Can anyone tell me how can I do this in C++? Is it possible or if not are there any tweaks for it ?


Thanks.

Recommended Answers

All 6 Replies

Thanks for your fast reply. Out of the three approach mentioned I took the third one. It fulfills the requirement but upto an extent. I had made a sample based on that solution which is posted below

#include<iostream>
using namespace std;
class Fred;
class DummyFredBase
{
private:
        friend class Fred;
        DummyFredBase(){
                cout<<"DummyFredBase Ctor"<<endl;   }
};

class Fred : private virtual DummyFredBase // 1. private virtual ?
{

        public:
        Fred(){
          cout<<"Fred Ctor"<<endl;      }

        void donothing()  {
                cout<<"Fred: I m doing nothing"<<endl;       }

};

class FredSon: public Fred  //2.<--- Error should be thrown here :(
{
        public:
         void myfunc(){ }

};

int main()
{
        Fred frobj;
        frobj.donothing();

        FredSon frsobj;  //3. error :(
        return 0;
}

After trying the above approach I have following issues :

1. Ref [1] in above code, what does private virtual means in terms of copies of base class ? What Fred holds ? What FredSon holds ?

2. Ref [2] in above code, with proposed solution compiler wont throw an error at the time of derivation instead it does so while making instance of FredSon[3].

What I need, the error should be thrown at the time of deriving the class that means at place [2]. So that keeping worries away from client but giving it to developer :).

Is there someway out now ?
Thanks.

>1. Ref [1] in above code, what does private virtual means in terms of
>copies of base class ? What Fred holds ? What FredSon holds ?
Private inheritance is a variation of composition. This:

class Fred : private virtual DummyFredBase {};

Is essentially the same thing as this:

class Fred {
private:
  DummyFredBase base;
};

The only real difference is syntax.

>2. Ref [2] in above code, with proposed solution compiler wont throw
>an error at the time of derivation instead it does so while making instance of FredSon[3].
Yes, that's correct.

>the error should be thrown at the time of deriving the class that means at place [2].
Sorry, you can't do that without using some language extension or preprocessor. C++ doesn't support that kind of static check.

For Q1:
I can understand the use of private -> to inherit dummy members privately.
But what role does "virtual" playing here. As far as I know virtual keyword is required to avoid the problems like Deadly diamond of death so that only one copy of base class get inherits. In this case we dont have condition like that then why virtual. If I remove virtual from here my motive wont fullfill :(

For Q1:
I can understand the use of private -> to inherit dummy members privately.
But what role does "virtual" playing here. As far as I know virtual keyword is required to avoid the problems like Deadly diamond of death so that only one copy of base class get inherits. In this case we dont have condition like that then why virtual. If I remove virtual from here my motive wont fullfill :(

Can anyone throw some light on it ?

Many Thanks to all.

For Q1:
I can understand the use of private -> to inherit dummy members privately.
But what role does "virtual" playing here. As far as I know virtual keyword is required to avoid the problems like Deadly diamond of death so that only one copy of base class get inherits. In this case we dont have condition like that then why virtual. If I remove virtual from here my motive wont fullfill :(

Can anyone throw some light on it ?

Many Thanks to all.

Simply put, the role of virtual means that any derived class constructor needs to call the virtual base class's constructor. - However, the base class is inherited privately, so all members of the virtual base, including the constructor, are hidden from the derived class. This means its impossible to create an instance of any derived class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.