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

Z

ZikO

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>
 
V

Victor Bazarov

ZikO said:
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
 
Z

ZikO

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.
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
 
M

Mark P

ZikO said:
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.




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
 
T

Thomas J. Gritzan

ZikO said:
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.
 
Z

ZikO

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top