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> 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
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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{
|
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;
|
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...
|
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...
|
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. ...
| |
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) -
|
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.
|
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...
|
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.
...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |