object of a class as its member???

A

ashok

A class can contain the pointer to object of that class as one of its
members.
Its understood. That's why linked lists are possible.

But I couldn't understand why
A class allows a static instance of an object of that class as its own
members but doesn't allow an ordinary variable object of that class
as its own members.

like

class one
{
one *o1 //ok..That's why linked lists are possible.
static one o2 //Ok
one o3 //error
.....
.....
};
If it doesn't allow o3 to be its own member then why it allows o2?

regards,
arj
 
P

Phoenix Fyrestar

I'm not completely sure if this is correct or not, maybe someone else can
tell me if I'm on the right track, but it seems to me the reason is this:

if I have a pointer, say:
one *a;
then I just have an address, or NULL. That address could even point back to
itself. If, on the other hand, I have
one b;
then, since b is an object of class one, then it must contain an object of
class one, which must contain an object of class one ad nauseum.
 
V

Victor Bazarov

ashok said:
A class can contain the pointer to object of that class as one of its
members.
Its understood. That's why linked lists are possible.

But I couldn't understand why
A class allows a static instance of an object of that class as its own
members but doesn't allow an ordinary variable object of that class
as its own members.

like

class one
{
one *o1 //ok..That's why linked lists are possible.
static one o2 //Ok
one o3 //error
....
....
};
If it doesn't allow o3 to be its own member then why it allows o2?

Essentially, o2 is a member of the class, not a member of the instance.
A static data member is a _class-wide_ object. It exists at the same
level (namespace level) as the class functions and as other static data
members. It also exists where other, non-members, static objects defined
outside of any block, are.

V
 
D

David White

Phoenix Fyrestar said:
I'm not completely sure if this is correct or not, maybe someone else can
tell me if I'm on the right track,

You are.
but it seems to me the reason is this:

if I have a pointer, say:
one *a;
then I just have an address, or NULL. That address could even point back to
itself. If, on the other hand, I have
one b;
then, since b is an object of class one, then it must contain an object of
class one, which must contain an object of class one ad nauseum.

Yes, it's a practical impossibility because it requires the creation of
objects infinitely recursively, whereas a single static object doesn't
contain itself so it doesn't have the same problem.

DW
 
A

ashok

To make the things more complex..the following code will run
successfully.

template<class T>
class one
{
one o;
};

if we remove the template<class T> statements then the code agains
gives compilation error..
Can any one tell me what is happeing in each case?

How a templated class is different from ordinary non templated class?


regds,
arj
 
J

John Harrison

ashok said:
To make the things more complex..the following code will run
successfully.

template<class T>
class one
{
one o;
};

if we remove the template<class T> statements then the code agains
gives compilation error..
Can any one tell me what is happeing in each case?

How a templated class is different from ordinary non templated class?


regds,
arj

This code compiles

template<class T>
class one
{
one o;
};

int main()
{
}

but this code doesn't

template<class T>
class one
{
one o;
};

int main()
{
one<int> x;
}

Templates don't get fully compiled until they are used (the technical term
is instantiated). The first case compiles because the compiler doesn't
notice there is a problem until you actually use the template to declare a
variable. But the problem is exactly the same as previously, an object
cannot contain itself, which is only common sense.

john
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top