Class pre-definition?

Started by
1 comment, last by colinisinhere 20 years, 7 months ago
You know how with functions, you can ''pre-define'' them? example:

void fart(int gas_desity);

void main()
{
    for(int i = 0; i < 10000; i++) fart();
}

void fart(int gas_desity)
{
//...blah

}
Well i need to do somthing like this with classes but i dont think its possible; its somthing like this: #include "main.h" <-Define my class ''player'' #include "weapons.h" <-Use class ''player'' #include "player.h" <-^needs to use weapon.h''s functions!^ please help!
Advertisement
What you call a "pre-definition" is known as a declaration .
A type that has been declared but not defined is known as an incomplete type .

Example of class declaration : class player;

An incomplete type can be used as

  • Parameter or return type in a function declaration : player foo( player f );
  • As the associated type of a pointer or reference : player* foo;


However, you cannot create an actual instance of the type (variable, value member) until the type is fully defined, as its size is not known. There is no way around that, but there is a lot you can already do with a pointer to an incomplete type.

e.g. Bridge pattern / Pointer-to-implementation (pimpl) idiom.



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on September 21, 2003 12:16:19 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Very useful to speedup compilation time and avoid nasty loops in header files.

This topic is closed to new replies.

Advertisement