469,945 Members | 2,248 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

unfamiliar syntax: class MyClass;

I have seen this on the top of a few header files

class SomeClass;
What does it mean?
I would expect some sort of declaration or defintion following such as

class SomeClass
{
SomeClass();
~SomeClass();

void SomeFunc();
};

or

class SomeClass class_instance;

Jun 2 '06 #1
3 1554
<cp***@austin.rr.com> wrote:
I have seen this on the top of a few header files

class SomeClass;

What does it mean?


It's a forward declaration:

http://www.parashift.com/c++-faq-lit...html#faq-39.11

S.
Jun 2 '06 #2
cp***@austin.rr.com wrote:
I have seen this on the top of a few header files

class SomeClass;
What does it mean?


It is a "forward declaration" that tells the compiler that SomeClass is
a class name. That is enough information to compile a class declaration
that contains a pointer or reference to SomeClass.

class SomeClass;

class Other
{
SomeClass* pSome;
};

Notice that this header file can be compiled without needing to
#include "SomeClass"

This trick is needed if SomeClass contains a pointer for Other. The
forward reference lets you eliminate the circular includes.

--
Scott McPhillips [VC++ MVP]

Jun 2 '06 #3
cpisz wrote:
class SomeClass; What does it mean?


http://www.google.com/search?q=forward+declarations

It means the header file knows the class's name but not its size or members.

C++ code needs to see a class's body to know its size and members. If code
only needs the name (such as to declare a reference), then a header may omit
compiling the header containing that SomeClass, and thus omits every header
that header calls.

Forward declarations become important as a program grows because they
prevent small changes to rare headers from making everything recompile for
no reason. Google "compilation firewall".

If you see it right above its definition, someone was being officious
without adding value.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by cppaddict | last post: by
4 posts views Thread by Matthias Käppler | last post: by
2 posts views Thread by bor_kev | last post: by
5 posts views Thread by Rob | last post: by
2 posts views Thread by =?Utf-8?B?QW5kcmV3?= | last post: by
21 posts views Thread by REH | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.