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

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

P: n/a
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_nest = 20;
ObjectEgg.el_egg = 30;

or functions

ObjectEgg.display();
ObjectNest.display();
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
Share this Question
Share on Google+
5 Replies


P: n/a
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_nest = 20;
ObjectEgg.el_egg = 30;

or functions

ObjectEgg.display();
ObjectNest.display();
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

P: n/a
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_henn = 5;

or run function Display() by means of

ObjectEgg.display();

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

P: n/a
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_henn = 5;

or run function Display() by means of

ObjectEgg.display();

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.