Re: Problem forward declaration of "typedef struct"
On Feb 19, 12:05 pm, "Mohammad Omer Nasir" <momer...@gmail.com> wrote:
But i defined AA specifically for making Objects and LPAA for
specifically pointer of A structure... i want to know the behavior of
"typedef struct" type structures, when we need to forward declaration
of it...
i can't answer that question, but i feel compelled to point out that
what you did here:
when i tried to write forward declaration code of structure A
in Temp.h header file which is
....
is NOT a forward declaration. You redefined the whole class. A forward
declaration takes ONLY the name of the class and no members, like
this:
class CTemp;
But there are limitations of what a forward decl can do. For example,
if you only have a type available via a forward decl then it is an
"incomplete type" and you cannot call functions on it:
class Foo;
....
Foo foo = Foo(); // illegal because Foo ctor not visible
....
Foo * foo = new Foo(); // same
....
foo->bar(); // illegal because Foo::bar() is not visible
But a fwd decl is okay for some purposes, like declaring a function
which takes a pointer or reference to that type:
void myFunction( CTemp const & ); // legal
or as part of a class:
class Foo;
class XYZ {
....
private:
Foo * m_foo; // legal
Foo m_bar; // NOT legal because Foo is incomplete
};