By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

A non derivable class question

P: n/a
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.

Jun 1 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On May 31, 11:59 pm, Amal P <enjoyam...@gmail.comwrote:
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.

Jun 1 '07 #2

P: n/a
On Jun 2, 3:33 am, Naresh Rautela <nraut...@gmail.comwrote:
On May 31, 11:59 pm, Amal P <enjoyam...@gmail.comwrote:


Dears,
I have a question.
classData
{};
classDisableDerive:virtual protected Data // A baseclass
{
private:
DisableDerive(){}; // Made the constructor as private
friendclassNonDerivable; // Made NonDerivable as friend so that
it can access private constructor
};
classNonDerivable: virtual protected DisableDerive // Intermediate
class- Virtual base
{
private:
int m_nVal;
public:
NonDerivable(){ m_nVal = -1;};
void SetValue( int nVal ){ m_nVal = 10; }
};
classTryDerive: public NonDerivable // Deriving fromnonderivable
class
{
};
int main()
{
TryDerive a;
return 0;
}
The above code does not allow to derive fromclassTryDerive. It gives
error on line TryDerive a. But when i changed the code as below it is
compiling.
classData
{};
classDisableDerive:virtual protected Data // A baseclass
{
private:
~DisableDerive(){}; // Made the constructor as private
friendclassNonDerivable; // Made NonDerivable as friend so that
it can access private constructor
};
classNonDerivable: virtual protected DisableDerive // Intermediate
class- Virtual base
{
private:
int m_nVal;
public:
NonDerivable(){ m_nVal = -1;};
void SetValue( int nVal ){ m_nVal = 10; }
};
classTryDerive: public NonDerivable // Deriving fromnonderivable
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.- 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.

Jun 4 '07 #3

P: n/a
On Jun 4, 3:44 am, Amal P <enjoyam...@gmail.comwrote:
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!)

Jun 4 '07 #4

P: n/a
On Jun 4, 9:44 am, Amal P <enjoyam...@gmail.comwrote:
On Jun 2, 3:33 am, Naresh Rautela <nraut...@gmail.comwrote:


On May 31, 11:59 pm, Amal P <enjoyam...@gmail.comwrote:
Dears,
I have a question.
>classData
{};
>classDisableDerive:virtual protected Data // A baseclass
{
private:
DisableDerive(){}; // Made the constructor as private
friendclassNonDerivable; // Made NonDerivable as friend so that
it can access private constructor
};
>classNonDerivable: virtual protected DisableDerive // Intermediate
>class- Virtual base
{
private:
int m_nVal;
public:
NonDerivable(){ m_nVal = -1;};
void SetValue( int nVal ){ m_nVal = 10; }
};
>classTryDerive: public NonDerivable // Deriving fromnonderivable
>class
{
};
int main()
{
TryDerive a;
return 0;
}
The above code does not allow to derive fromclassTryDerive. It gives
error on line TryDerive a. But when i changed the code as below it is
compiling.
>classData
{};
>classDisableDerive:virtual protected Data // A baseclass
{
private:
~DisableDerive(){}; // Made the constructor as private
friendclassNonDerivable; // Made NonDerivable as friend so that
it can access private constructor
};
>classNonDerivable: virtual protected DisableDerive // Intermediate
>class- Virtual base
{
private:
int m_nVal;
public:
NonDerivable(){ m_nVal = -1;};
void SetValue( int nVal ){ m_nVal = 10; }
};
>classTryDerive: public NonDerivable // Deriving fromnonderivable
>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.- 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.- 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/Sho...85411&siteid=1
..

Thanks for the help,
Amal P.

Jun 4 '07 #5

P: n/a
On Jun 1, 8:59 am, Amal P <enjoyam...@gmail.comwrote:
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 4 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.