473,848 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing private methods of nested classes

Is the following legal?

class Outer
{
class Inner
{
private:
Inner() { }
};

Outer()
{
Inner inner;
}
};

Should people trying to do something similar be using the friend
keyword?

Nov 29 '06 #1
6 5037
earthwormgaz wrote:
Is the following legal?

class Outer
{
class Inner
{
private:
Inner() { }
};

Outer()
{
Inner inner;
}
};

Should people trying to do something similar be using the friend
keyword?
No, and yes (or a virtual constructor or similar).

Cheers! --M

Nov 29 '06 #2
earthwormgaz wrote:
Is the following legal?

class Outer
{
class Inner
{
private:
Inner() { }
};

Outer()
{
Inner inner;
}
};

Should people trying to do something similar be using the friend
keyword?
I am not sure it's legal. I would be if the situation were reversed,
i.e. 'Inner' tried accessing 'Outer's private members. Yes, using
'friend' is the simplest work-around.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 29 '06 #3
mlimber wrote:
No, and yes (or a virtual constructor or similar).
Well, maybe its better to consider this, I mean any old method, not
just constructors et etcetera.

class Outer
{
class Inner
{
private:
void Method();
};

Outer()
{
Inner inner;
inner.Method(); // is this legal?
}
};

For the record, VC++ 2005/8.0 says yes, Metrowerks says no, g++ 3.4.4
says no.

Nov 29 '06 #4

Victor Bazarov a écrit :
earthwormgaz wrote:
Is the following legal?

class Outer
{
class Inner
{
private:
Inner() { }
};

Outer()
{
Inner inner;
}
};

Should people trying to do something similar be using the friend
keyword?

I am not sure it's legal. I would be if the situation were reversed,
i.e. 'Inner' tried accessing 'Outer's private members. Yes, using
'friend' is the simplest work-around.
It's definitely not legal - accessing a private member that is not
yours is not legal C++, even if you "own" the class. That's the whole
principle of encapsulation using an access control mechanism.

There is also no point on making Inner private members public to Outer
using the friend keyword. If Inner is supposed to be hidden from any
class but Outer (ie Inner is defined as private in Outer), just define
Inner members as public. However, I strongly advise you to avoid doing
so, as there is also no point in putting a bunch of public members in a
inner class so that they can be accessed only by the owner - just add
private members to your Outer class then. If you want to create an
inner class that actually have some added value, think about it twice:
what should it expose, and how should it expose it? Remember that an
inner class is just another class, which happens to have a particular
semantic in the sense that it is strongly tied to its Outer class. It
is subject to the same design principles as all the other classes.

Regards,

-- Emmanuel Deloget, Artware

Nov 29 '06 #5
Emmanuel Deloget wrote:
There is also no point on making Inner private members public to Outer
using the friend keyword. If Inner is supposed to be hidden from any
class Outer
{
public: // I presume this changes everything?
class Inner
{
private:
void Method();
};

Outer()
{
Inner inner;
inner.Method(); // is this legal?
}

};

That is actually a better representation of what I have come across. It
is something somebody has done in VC++ code, and its knackered me in a
porting effort.

It appears as though I am within my rights to tell them they shouldn't
have made the method in question in the Inner class private.

Nov 29 '06 #6

earthwormgaz a écrit :
Emmanuel Deloget wrote:
There is also no point on making Inner private members public to Outer
using the friend keyword. If Inner is supposed to be hidden from any

class Outer
{
public: // I presume this changes everything?
class Inner
{
private:
void Method();
};

Outer()
{
Inner inner;
inner.Method(); // is this legal?
}

};
No, it doesn't change anything - Method() is still a private method of
Outer::Inner, and as such is only acessible from Outer::Inner, friend
classes and friend functions of Outer::Inner. It does change something
in the design of the application, as Inner is now accessible from the
outside of Outer.
That is actually a better representation of what I have come across. It
is something somebody has done in VC++ code, and its knackered me in a
porting effort.

It appears as though I am within my rights to tell them they shouldn't
have made the method in question in the Inner class private.
You're right - it is definitely a bad thing to do. It would have been
far better to publish the correct, public methods in Inner and to
encapsulate the internal of Inner using these methods.

Regards,

-- Emmanuel Deloget, Artware

Nov 29 '06 #7

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

Similar topics

4
1854
by: jblazi | last post by:
Let us assume, I have to classes A and B: class A { "type" x; }; and class B { A a;
2
2318
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point in Rectangle, the compiler tells me Point::x is protected. I would have expected Rectangle to see the protected members of any Point. Compiling the following code give me this error: g++ -o rectangle main.cc main.cc: In member function `size_t...
8
2842
by: Etienne Boucher | last post by:
Nested classes are usualy objects made to only live in their parent object instance. In other words... public class Outter { public class Inner { } }
6
7768
by: Carlos | last post by:
Hi all, I am trying to access a public field of another form class within the same namespace. The field is public, what is the best way to access it from a different class? I defined as private MyNameSpace.Form1 cForm1; and I am trying to use it later as TextBox.Text = cForm1.TextBox.Text;
9
3691
by: Joel Moore | last post by:
I'm a little confused here. If I have the following: Public ClassA Friend varA As Integer Private varB As Integer Private ClassB Public Sub MethodA() ' How can I access varA and varB here? End Sub
1
2520
by: John Dann | last post by:
I'm realising that there's something important that I don't understand about acccessing class properties. In fact I'm not even sure that I can explain clearly what it is that I don't understand! But let me try: Let's say that I have a specific parameter value that needs to be available to several different classes within a program. What I'm doing currently is to store this specific value in a property of MyClass1. This class is then...
5
2710
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and Engine is another class inside it with its own set of properties. I want to know if there is a way to access the methods and the properties of the Owner class for the class that's inside it? I.e. I want to find out within Engine what make the Car is which is exposed by the property Car.Model.
5
3570
by: JamesO | last post by:
Hello all, Is there a way to make a class private to the namespace so that other classes in the namespace can see and use it but noone outside a namespace can see or use it? Feel free to send me to links to read, I just cannot seem to find anything about this, maybe it's not possible. Thanks in advance, jmy
2
1071
by: Simon Woods | last post by:
Hi I'm still trying to get my head around how to do TDD. I have a situation where I have a set of classes which build a tree which contains nested (subselect) SQL statements. The tree is completely internal to my project and there is no need to make it public in any way. The logic is pretty complex (for me anyway) and so I want to be able to test it with a variety of test cases. The tree generates SQL which is then used to create a...
0
9892
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10661
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10347
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7889
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7067
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5731
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5917
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4542
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
2
4134
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.