question on forward declaration

S

subramanian100in

Consider the program:

class Test;

class First
{
Test t_obj;
};

int main()
{
return 0;
}

If I compile, I get compilation error for the line
Test t_obj;
because Test class is not defined.

My doubt is the following:
Since I am not creating an object of class First, why doesn't the
compiler allow me to have an object of incomplete type(here Test) as
data member ? If an object of type First is created, then the compiler
will need to know the size of the object and in turn it needs to know
the size of each data member. Here no object is created.

I am unable to understand the reason. Where am I going wrong ? Kindly
explain

Thanks
V.Subramanian
 
A

Amal P

Consider the program:

class Test;

class First
{
Test t_obj;

};

int main()
{
return 0;

}

If I compile, I get compilation error for the line
Test t_obj;
because Test class is not defined.

My doubt is the following:
Since I am not creating an object of class First, why doesn't the
compiler allow me to have an object of incomplete type(here Test) as
data member ? If an object of type First is created, then the compiler
will need to know the size of the object and in turn it needs to know
the size of each data member. Here no object is created.

I am unable to understand the reason. Where am I going wrong ? Kindly
explain

Thanks
V.Subramanian

This is not any of the purpose of forward declaration. That is why
compiler is not allowing you to do so. You can declare a pointer of
the Test anyway. But not an object. Because class Test does not exist
and no storage information is available. If it is pointer it is
possible because every pointer is same. Just points to an address. So
the forward declaration can be useful in declaring a class pointer
without including the whole class header.

Something like this is also valid.

class Test;

class First
{
Test t_obj;

};

class Test
{
};
 
C

Craig Scott

Consider the program:

class Test;

class First
{
Test t_obj;

};

int main()
{
return 0;

}

If I compile, I get compilation error for the line
Test t_obj;
because Test class is not defined.

My doubt is the following:
Since I am not creating an object of class First, why doesn't the
compiler allow me to have an object of incomplete type(here Test) as
data member ? If an object of type First is created, then the compiler
will need to know the size of the object and in turn it needs to know
the size of each data member. Here no object is created.

I am unable to understand the reason. Where am I going wrong ? Kindly
explain

To answer your question directly, when the compiler sees the
declaration of class First, it needs a full declaration of Test in
order to know things like how big it is (so it knows how to lay out
memory for a First object). The fact that you don't create a First
object doesn't matter. You gave the compiler a declaration of First so
it tries to compile it and it must be a valid declaration. This is why
forward declarations are useful, since they just tell the compiler
that a symbol exists but that you don't want to provide a full
declaration just yet. That lets the compiler create pointers and
references to the object, but not to dereference the pointer or use
the reference without the full declaration being provided.
 
S

subramanian100in

To answer your question directly, when the compiler sees the
declaration of class First, it needs a full declaration of Test in
order to know things like how big it is (so it knows how to lay out
memory for a First object).

Does it mean that when the class definition is compiled, the size of a
class object should be known to the compiler ?

Kindly give some detail. I am unable to understand this.

Thanks
V.Subramanian
 
J

John Harrison

Does it mean that when the class definition is compiled, the size of a
class object should be known to the compiler ?

Kindly give some detail. I am unable to understand this.

Thanks
V.Subramanian

It's not just the creation of objects its the problem. How would you
compile this code without knowing the size of Test?

class Test;

class First
{
Test t_obj;
int i;
};

int func(First* f)
{
return f->i;
}

I'm sure that the C++ language could be rewritten to make the code you
wrote legal. I'm also sure that it was thought that it would be extra
complexity for compiler writers at no great benefit for anyone.

john
 
C

Craig Scott

Does it mean that when the class definition is compiled, the size of a
class object should be known to the compiler ?

Kindly give some detail. I am unable to understand this.

Whenever you define a class, all instance objects (ie not pointers or
references) must have full definitions visible at that point. You can
think of it as the compiler must be *able* to instantiate the class at
any point after the class definition, which is stronger than only *if*
an instance of that class is actually required. The relevant part of
the standard which states this is 9.2/2:

"A class is considered a completely-defined object type ... (or
complete type) at the closing } of the class-specifier."

The example I gave about the compiler must know the size of an object
is really just a reason why the standard requires it - knowing the
size is not a direct requirement of the standard (as far as I know)
but rather a consequence of what the standard requires in 9.2/2
 

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,291
Messages
2,571,493
Members
48,164
Latest member
KerrieWind

Latest Threads

Top