473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

question on forward declaration

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

Jun 29 '07 #1
1 1049
On Jun 29, 11:35 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
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
{
};

Jun 29 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define...
6
by: Markus Dehmann | last post by:
I have a circular dependency between two classes. The FAQ hint about a forward declaration does not help in my case ( How can I create two classes that both know about each other?) Error:...
2
by: Plok Plokowitsch | last post by:
After over a decade of programming in C++ I seem to have missed some substantial point (mental note: am I getting too old for this?). A little bit of help would be *very* appreciated. I'm trying...
3
by: Michael Sgier | last post by:
Hello with the original code below I get the error: "forward declaration of `struct CPlayer'" class CPlayer; // what does this do? Instantiate the class CPlayer? // what's a forward...
24
by: ark | last post by:
Hello group, Could you help me with this: static const int x; ............ something ............. static const int x = 17; It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
4
by: Steve | last post by:
Hi, I always though to return an instance of a class by value, it had to be defined - i.e. forward declaration isn't good enough? Consider the following code snippet: class RGBA; class...
9
by: wizwx | last post by:
There are two typical implementations of a singleton. The first one is to use a static pointer class Singleton { static Singleton * pSingleton; public: static Singleton * instance() {...
6
by: Goran | last post by:
Hi all, this is just a "design" question. I'm unsure where to put my "#includes". Should I put it directly into headerfiles? Or into sourcefiles with a forward type declaration in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.