473,779 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[c++] Nested calsses and access to them.

Hi there.

I have a problem. I have created nested classes but don't know how to access to
inner classes. I know I can create objects:

Hen Object;
Hen::Nest ObjectNest;
Hen::Nest::Egg ObjectEgg;

and have access to particular elements

Object.el_hen = 10;
ObjectNest.el_n est = 20;
ObjectEgg.el_eg g = 30;

or functions

ObjectEgg.displ ay();
ObjectNest.disp lay();
Object.display( );

but if I have only e.g. ObjectEgg, could I have access to both inner classes? I
would like to know the same using pointers!

Regards.
<code>
class Hen
{
public:
int el_hen;
class Nest
{
public:
int el_nest;
class Egg
{
public:
int el_egg;
void display();
};
void display();
};
void display();
};
</code>

Aug 2 '06 #1
5 2193
ZikO wrote:
I have a problem. I have created nested classes but don't know how to
access to inner classes. I know I can create objects:

Hen Object;
Hen::Nest ObjectNest;
Hen::Nest::Egg ObjectEgg;

and have access to particular elements

Object.el_hen = 10;
ObjectNest.el_n est = 20;
ObjectEgg.el_eg g = 30;

or functions

ObjectEgg.displ ay();
ObjectNest.disp lay();
Object.display( );

but if I have only e.g. ObjectEgg, could I have access to both inner
classes?
What do you mean?

Classes nest in your example. The objects don't. An instance of 'Hen'
in your code does *not* have a data member of type 'Nest'. The same goes
on down: an instance of 'Hen::Nest' does *not* have a data member of type
'Hen::Nest::Egg '. What do you want your ObjectEgg to access?

If you need to make your 'ObjectEgg' be able to manipulate some other
instance of, say, 'Hen', then you need to pass a reference or a pointer to
that 'Hen' to some member function of 'Hen::Nest::Egg ', or create a member
of type 'Hen&' or 'Hen*' in 'Hen::Nest::Egg ' and set it to refer (point)
to some real object of type 'Hen'.

I believe you have some misconceptions (probably from Java) about how
types and objects interrelate, especially when nesting is involved.
I would like to know the same using pointers!
The same what?
Regards.
<code>
class Hen
{
public:
int el_hen;
class Nest
{
public:
int el_nest;
class Egg
{
public:
int el_egg;
void display();
};
void display();
};
void display();
};
</code>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 2 '06 #2
Sorry. Maybe I didn't explain it enough ...
Whenever the object of my class is created, which contains another nested
classes by means of:

Hen ObjectEgg;

the compiler also reserves memory for nested elements, because the type is biult
this way. So if I have object 'ObjectEgg' of my type 'Hen' I can get access to
el_hen and give it some value by means of

ObjectEgg.el_he nn = 5;

or run function Display() by means of

ObjectEgg.displ ay();

But there is something more inside Hen class, intead of int or double there is
another class, user type which could have his own object but how to define it
and how to get access to it. Whenever I create object of Hen the memory is
reserved for the whole object with those nested classes.
>I would like to know the same using pointers!
The same what?

Hen *p_Hen;

this is a pointer of Hen class. Again, how to get access to elements of nested
classes when I create pointer of Hen class and give an address fo my object:

Hen *p_Hen = new Hen; // for example
><code>
class Hen
{
public:
int el_hen;
class Nest
{
public:
int el_nest;
class Egg
{
public:
int el_egg;
void display();
};
void display();
};
void display();
};
</code>
V

Aug 2 '06 #3
ZikO wrote:
Sorry. Maybe I didn't explain it enough ...
Whenever the object of my class is created, which contains another
nested classes by means of:

Hen ObjectEgg;

the compiler also reserves memory for nested elements, because the type
is biult this way. So if I have object 'ObjectEgg' of my type 'Hen' I
can get access to el_hen and give it some value by means of

ObjectEgg.el_he nn = 5;

or run function Display() by means of

ObjectEgg.displ ay();

But there is something more inside Hen class, intead of int or double
there is another class, user type which could have his own object but
how to define it and how to get access to it. Whenever I create object
of Hen the memory is reserved for the whole object with those nested
classes.
>>I would like to know the same using pointers!
>The same what?


Hen *p_Hen;

this is a pointer of Hen class. Again, how to get access to elements of
nested classes when I create pointer of Hen class and give an address fo
my object:

Hen *p_Hen = new Hen; // for example
What? You need to post a clear example which exhibits the problem and
explain what the problem is.

I'm going to take a wild guess here and suppose that you're asking
something quite different than what you've said so far. Are you asking
how to initialize an object which is a member of another object?

class A
{
public:
A( int i ) {...}
};

class B
{
public:
B();
A my_A;
};

Do you mean how do you pass the parameter i to the A member object when
you construct a B object?

This has nothing to do with nested classes but the answer is to use an
initializer list. So the constructor for B may look like:

B::B() : my_A(42) {...}

- Mark
Aug 2 '06 #4
ZikO schrieb:
But there is something more inside Hen class, intead of int or double
there is another class, user type which could have his own object but
how to define it and how to get access to it. Whenever I create object
of Hen the memory is reserved for the whole object with those nested
classes.
class Hen
{
int m_someInt;
float m_someFloat;

class someClass;
typedef int someType;
};

If you create an object of class Hen by doing this:

Hen hen;

The compiler will reserve space for m_someInt and m_someFloat, because
they are objects. There will be no space for someClass and someType,
because both are not objects but types. Type are some kind of blue print
for objects, you need to instantiate them to get an object of that type:

class Hen
{
class Egg;
{
// definition...
};

Egg m_anEgg;
};
Hen hen;

Now you can access the Egg in Hen through hen.m_anEgg.

--
Thomas
Aug 2 '06 #5
class Hen
{
class Egg;
{
// definition...
};
Egg m_anEgg;
};
Hen hen;
Now you can access the Egg in Hen through hen.m_anEgg.
Yes. That's what I needed to understand :). Sorry people. I have just started to
learn C++ and there are many things I don't understand so that it's more
difficult to explain.

Regards.

Aug 2 '06 #6

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

Similar topics

2
3657
by: Robert M. Gary | last post by:
I'm curious what the ANSI C++ standard says about nested classes. I'm not able to find where in the ANSI C++ standard this is addressed. The issue is the accessibility of sibling nested classes. Example... class A{ private: class B{ }; class C{
8
3945
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access private members of nesting class. template <typename T> class Container { // NO friendship given to any other public: class ContainerIterator;
3
6472
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
6
559
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations like MyClass.MyColors = Color.Green or, a 'get', such as Color forecolor = MyClass.MyColors; I want to use an indexer so I can take parameters, such as the color type (e.g. "Foreground", "Background" etc.). With a single member function I couldn't...
4
2469
by: Steve Franks | last post by:
I have this cool nested master page scenario working great. However what is the correct way to be able to access a strongly typed property at the top level master from a content page that has a nested master page between the content page and the top level master? For example, assume the very top level master is called CompanyWide.master. And the nested master page is called DepartmentA.master, which inherits from CompanyWide master. ...
37
2792
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators in a nested scope, on variables from the outer scope: PythonWin 2.4.3 (#69, Mar 29 2006, 17:35:34) on win32. Portions Copyright 1994-2004 Mark Hammond (mhammond@skippinet.com.au) -
5
2284
by: Jake K | last post by:
What purpose does nesting a class inside another class typically server? Are there conditions where this would be beneficial? Thanks a lot.
4
2315
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one could implement an equivalent behaviour with plain C (in this case I'm thinking of the language I'm developing, which shall be converted into C for target compilation). So far I didn't touch the topic "nested functions", since I just don't see an...
3
2127
by: Cousson, Benoit | last post by:
I don't think so; my original email was mainly a question. I do agree that they are other ways to do what I'm trying to achieve; there are always several ways to solve an issue. Few days ago, I decided to use nested class because I realized that it was the most convenient way to implement my need. Since this feature is supported in many languages, I was just surprised that Python did support it only partially, hence my original email. ...
0
9632
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
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8958
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7478
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
5372
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
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4036
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
3631
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.