473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 5009
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
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
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...
8
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
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...
9
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?...
1
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!...
5
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...
5
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.