A non derivable class question

A

Amal P

Dears,

I have a question.

class Data
{
};
class DisableDerive:virtual protected Data // A base class
{
private:
DisableDerive(){}; // Made the constructor as private
friend class NonDerivable; // Made NonDerivable as friend so that
it can access private constructor
};

class NonDerivable: virtual protected DisableDerive // Intermediate
class - Virtual base
{
private:
int m_nVal;
public:
NonDerivable(){ m_nVal = -1;};
void SetValue( int nVal ){ m_nVal = 10; }
};

class TryDerive: public NonDerivable // Deriving from non derivable
class
{
};

int main()
{
TryDerive a;
return 0;
}


The above code does not allow to derive from class TryDerive. It gives
error on line TryDerive a. But when i changed the code as below it is
compiling.

class Data
{
};
class DisableDerive:virtual protected Data // A base class
{
private:
~DisableDerive(){}; // Made the constructor as private
friend class NonDerivable; // Made NonDerivable as friend so that
it can access private constructor
};

class NonDerivable: virtual protected DisableDerive // Intermediate
class - Virtual base
{
private:
int m_nVal;
public:
NonDerivable(){ m_nVal = -1;};
void SetValue( int nVal ){ m_nVal = 10; }
};

class TryDerive: public NonDerivable // Deriving from non derivable
class
{
};

int main()
{
TryDerive a;
return 0;
}

In the above code i changed the destructor as private.

What is the reason for this?

Thanks and regards,
Amal P.
 
N

Naresh Rautela

Dears,

I have a question.

class Data
{};

class DisableDerive:virtual protected Data // A base class
{
private:
DisableDerive(){}; // Made the constructor as private
friend class NonDerivable; // Made NonDerivable as friend so that
it can access private constructor

};

class NonDerivable: virtual protected DisableDerive // Intermediate
class - Virtual base
{
private:
int m_nVal;
public:
NonDerivable(){ m_nVal = -1;};
void SetValue( int nVal ){ m_nVal = 10; }

};

class TryDerive: public NonDerivable // Deriving from non derivable
class
{

};

int main()
{
TryDerive a;
return 0;

}

The above code does not allow to derive from class TryDerive. It gives
error on line TryDerive a. But when i changed the code as below it is
compiling.

class Data
{};

class DisableDerive:virtual protected Data // A base class
{
private:
~DisableDerive(){}; // Made the constructor as private
friend class NonDerivable; // Made NonDerivable as friend so that
it can access private constructor

};

class NonDerivable: virtual protected DisableDerive // Intermediate
class - Virtual base
{
private:
int m_nVal;
public:
NonDerivable(){ m_nVal = -1;};
void SetValue( int nVal ){ m_nVal = 10; }

};

class TryDerive: public NonDerivable // Deriving from non derivable
class
{

};

int main()
{
TryDerive a;
return 0;

}

In the above code i changed the destructor as private.

What is the reason for this?

Thanks and regards,
Amal P.

Friendship is not inherited. So while DisableDerive has designated
NonDerivable as its friend, TryDerive is not its friend. In your code
you are invoking the constructor of DisableDerive through TryDerive
and hence it fails. To fix this designate TryDerive as a friend of
DisableDerive.
 
A

Amal P

Friendship is not inherited. So while DisableDerive has designated
NonDerivable as its friend, TryDerive is not its friend. In your code
you are invoking the constructor of DisableDerive through TryDerive
and hence it fails. To fix this designate TryDerive as a friend of
DisableDerive.- Hide quoted text -

- Show quoted text -

Dear Naresh Rautela,

I guess you misunderstood my question. My question is when the
constructor is private the object creation fails. But when destruction
is private it works. What is this difference between constructor and
distructor.

Thanks and regards,
Amal P.
 
V

v.r.marinov

My question is when the constructor is private the object creation fails. But when destruction
is private it works. What is this difference between constructor and
distructor.

There should be no difference. Here is what the standard has to say:

"A program is ill-formed if the class for which a destructor is
implicitly defined has:
- a non-static data member of class type (or array thereof) with an
inaccessible destructor, or
- a base class with an inaccessible destructor."

Obviously some compilers don't implement the standard strictly
enough.
(I'm shocked, s-h-o-c-k-e-d!)
 
A

Amal P

Dear Naresh Rautela,

I guess you misunderstood my question. My question is when the
constructor is private the object creation fails. But when destruction
is private it works. What is this difference between constructor and
distructor.

Thanks and regards,
Amal P.- Hide quoted text -

- Show quoted text -

Hi,

I managed to find that this code is getting compiled in VC++ 2005
and not in VC++ 6.0. So I have posted this question in msdn community.
Sorry for the confusion. In this link i have posted the question.
http://forums.microsoft.com/msdn/ShowPost.aspx?postid=1685411&siteid=1
..

Thanks for the help,
Amal P.
 
J

James Kanze

class Data
{};
class DisableDerive:virtual protected Data // A base class
{
private:
DisableDerive(){}; // Made the constructor as private
friend class NonDerivable; // Made NonDerivable as friend so that
it can access private constructor
};
class NonDerivable: virtual protected DisableDerive // Intermediate
class - Virtual base
{
private:
int m_nVal;
public:
NonDerivable(){ m_nVal = -1;};
void SetValue( int nVal ){ m_nVal = 10; }
};
class TryDerive: public NonDerivable // Deriving from non derivable class
{
};

This shouldn't work. As DisableDerive is a virtual base, the
(compiler generated) constructor of TryDerive can't call its
constructor. (You won't get an error, however, unless you
actually try to create an instance.)
int main()
{
TryDerive a;
return 0;
}
The above code does not allow to derive from class TryDerive.

It doesn't allow derivation from NonDerivable.
It gives error on line TryDerive a. But when i changed the
code as below it is compiling.

Which is correct.
class Data
{};
class DisableDerive:virtual protected Data // A base class
{
private:
~DisableDerive(){}; // Made the constructor as private
friend class NonDerivable; // Made NonDerivable as friend so that
it can access private constructor

};
class NonDerivable: virtual protected DisableDerive // Intermediate
class - Virtual base
{
private:
int m_nVal;
public:
NonDerivable(){ m_nVal = -1;};
void SetValue( int nVal ){ m_nVal = 10; }
};
class TryDerive: public NonDerivable // Deriving from non derivable class
{
};
int main()
{
TryDerive a;
return 0;
}
In the above code i changed the destructor as private.
What is the reason for this?

What's the reason for what? Both are illegal, as written. You
can create an instance of TryDerive with new, but you can't
destruct it with delete, and you can't create local instances,
etc.

Curiously enough, both Sun CC and VC++ accept your second
program, although the standard clearly says that it is the most
derived class which must call the destructor of a virtual base
class (and that access control is applied to destructors). G++
gets it right.
 
Joined
Oct 10, 2008
Messages
3
Reaction score
0
Generic solution for non-derivable classes in C++

Making a class non-derivable is really simple. You can find the answer in FAQ. However, having a generic solution for this problem is better, but harder to implement. I was investigating this problem a long time ago. Basically, a mix of template friends, access levels and virtual inheritance does this job. The article about my solution can be found here - http://www.jetsnail.com/tech-blog/38...noninheritable. I hope you will like it!
:stupido:
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top