4

For example, class A has a member of class B. In general, for the purpose of minimizing compilation dependency, we often make class A include B's pointer, and pre-declare class B in the class A's declaration. Looks like this:

//B.h
class B
{
    ....
};

//A.h
class B;
class A
{
    B*b;
    A();
    ...
};

//A.cpp
#include "B.h"
A::A()
{
    b=new B();
    ...
};

But now I have a question: if the class of B is defined using typedef like this:

typedef class
{
 ....
}B;

The previous pre-declared method will not work in this case. How should I pre-declare the class B in A.h?

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
KevinZhu
  • 41
  • 3
  • `typedef class B foo;` and after just declare your class normally. What is the point to typedef a class like that ? In cpp your class no need typedef. By the way if you don't have circle include just include B header in A header. – Stargateur Nov 01 '16 at 06:10
  • @Stargateur `class B` and `B` are different in this case – M.M Nov 01 '16 at 06:23
  • 2
    I ask this question because I may use class writed by other people, they declare their class using typedef. I don't want to change their original files. So I want to know whether have method to solve this problem. – KevinZhu Nov 01 '16 at 06:28
  • 2
    @KevinZhu you can't just include their header in you header ? your A.h can include B.h ? – Stargateur Nov 01 '16 at 06:35
  • A related question [Forward declaration of a typedef in C++](http://stackoverflow.com/questions/804894/forward-declaration-of-a-typedef-in-c?rq=1) – Bo Persson Nov 01 '16 at 08:05

1 Answers1

2

In the code typedef class { .... } B; , it is an unnamed class, with B being a type alias for the unnamed class.

It is not possible to declare an unnamed class without defining it (i.e. providing the class body); and it is not possible to declare that B is a typedef for an unnamed class.

There is no such thing as a forward declaration of a typedef. In general a typedef can declare an alias for incomplete type that is completed later, but an unnamed class cannot fill either of those roles.

Removed further advice in response to voter feedback

Note: the question used the term "pre-declare". The C++ Standard does not use the terms "pre-declare" or the more common jargon "forward-declare". In the standard's terminology, a forward declaration of a class is simply called a class declaration. This can be clarified by saying "declaration that is not a definition" if it is not clear from context already.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • 2
    OP is already doing this, so you may as well just post that last statement as a comment. – barak manos Nov 01 '16 at 06:27
  • Not sure why this is downvoted. The first sentence answers OP's question , and the rest of the post gives an alternative way to solve the problem OP is having. @barakmanos there's no rule that answers cannot have additional advice . IMO just saying "You can't." is not as useful an answer. – M.M Nov 01 '16 at 08:03
  • I did not down-vote your answer (it is correct plain and simple). I only suggested that it might be more appropriate as a comment. My guess is that it was down-voted because the community here consists of a rather large amount of nit-pickers. – barak manos Nov 01 '16 at 08:14
  • In fact, the first statement is quite useful, since the OP doesn't seem to be aware of the term 'forward-declaration', which you clearly mention. I will up-vote to compensate... – barak manos Nov 01 '16 at 08:21
  • The answer states that there is no solution to the OP problem and, more importantly, **explains** clearly why that is so. +1 from me. – bolov Nov 01 '16 at 08:44