473,569 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A non derivable class question

Dears,

I have a question.

class Data
{
};
class DisableDerive:v irtual 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:v irtual 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
5 1882
On May 31, 11:59 pm, Amal P <enjoyam...@gma il.comwrote:
Dears,

I have a question.

class Data
{};

class DisableDerive:v irtual 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:v irtual 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
On Jun 2, 3:33 am, Naresh Rautela <nraut...@gmail .comwrote:
On May 31, 11:59 pm, Amal P <enjoyam...@gma il.comwrote:


Dears,
I have a question.
classData
{};
classDisableDer ive:virtual protected Data // A baseclass
{
private:
DisableDerive() {}; // Made the constructor as private
friendclassNonD erivable; // Made NonDerivable as friend so that
it can access private constructor
};
classNonDerivab le: 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 fromnonderivabl e
class
{
};
int main()
{
TryDerive a;
return 0;
}
The above code does not allow to derive fromclassTryDer ive. It gives
error on line TryDerive a. But when i changed the code as below it is
compiling.
classData
{};
classDisableDer ive:virtual protected Data // A baseclass
{
private:
~DisableDerive( ){}; // Made the constructor as private
friendclassNonD erivable; // Made NonDerivable as friend so that
it can access private constructor
};
classNonDerivab le: 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 fromnonderivabl e
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
On Jun 4, 3:44 am, Amal P <enjoyam...@gma il.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
On Jun 4, 9:44 am, Amal P <enjoyam...@gma il.comwrote:
On Jun 2, 3:33 am, Naresh Rautela <nraut...@gmail .comwrote:


On May 31, 11:59 pm, Amal P <enjoyam...@gma il.comwrote:
Dears,
I have a question.
>classData
{};
>classDisableDe rive:virtual protected Data // A baseclass
{
private:
DisableDerive() {}; // Made the constructor as private
friendclassNonD erivable; // Made NonDerivable as friend so that
it can access private constructor
};
>classNonDeriva ble: 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 fromnonderivabl e
>class
{
};
int main()
{
TryDerive a;
return 0;
}
The above code does not allow to derive fromclassTryDer ive. It gives
error on line TryDerive a. But when i changed the code as below it is
compiling.
>classData
{};
>classDisableDe rive:virtual protected Data // A baseclass
{
private:
~DisableDerive( ){}; // Made the constructor as private
friendclassNonD erivable; // Made NonDerivable as friend so that
it can access private constructor
};
>classNonDeriva ble: 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 fromnonderivabl e
>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
On Jun 1, 8:59 am, Amal P <enjoyam...@gma il.comwrote:
class Data
{};
class DisableDerive:v irtual 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:v irtual 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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 4 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2868
by: Tim Hill | last post by:
I'm just getting up to speed on Java, and have a question about local class behavior. Here's a mini-app to illustrate: interface Foo { void print(); } class LocalClassTest { public static void main(String args) { Foo foo = getFooImpl(100); foo.print();
2
2767
by: Charles Keepax | last post by:
Sorry about the newbie question. If a class contains a pointer to some data, and memory is allocated to that pointer in the constructor. Naturally the destructor should free that memory, right? So since C++ is by default call-by-value, if you pass that class as an arguement to a function a copy of the class will be made however a copy of the...
1
1573
by: Jean Pion | last post by:
Dear readers, Can I have a class selector the pseudoclasses like: .menuw { general_stuff_for_this_class; } a.menuw:link { special_for_link;} a.menuw:visited {special_for_visited; } a.menuw:hover { special_for_hover; } a.menuw:active { special_for_active; }
2
1599
by: Chris Puncher | last post by:
Hi. I have a RCW class that was generated by the VS.NET2003 IDE when I added a COM dll reference to my project. This all works fine when I call methods on it. My initial problem is that in ASP.NET I would like to add this to the Session object whilst using SQLServer as the session storage. Unfortunately, as this required the RCW class to be...
6
1407
by: mrmagoo | last post by:
I have a class that accepts String and Long datatypes. Class Menu Private FirstTier as String Private SecondTier as Long Sub New(ByVal FirstTier As String, _ ByVal SecondTier As Long) ....
7
1275
by: Brian C | last post by:
Hello all, I have, what I'm sure is an idiotic question, but I can't seem to find an answer for it (probably looking in the wrong direction of course). I'm coming from numerous years in C programming, trying to expand my horizons and get into C++. I did a little bit of Java programming along my C days, and this problem was solved with...
6
2070
by: Xemnosyst | last post by:
Hello. I have an obscure question for y'all. Consider an inner class: class Outer1 { class Inner1 { } } Now in a different inner class I extend the first one. Does anyone know how to test if <i is an inner class of o> in "aMethod", below?
1
1074
by: sittner | last post by:
Hello there, i am pretty new to object-oriented programming and i have a question: let's say i have a simple class such as: class father: age=... name=.... def abcd..... class son(father): age=.... name=....
4
1336
by: t f | last post by:
hi just a quick question - i have the something like the following public abstract class ClassA { protected int iA; public int Value { get {return iA;} set { iA = value;}}
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.