Topic: Forward Declare Classes


Author: "Fred Wasmer" <73743.1660@compuserve.com>
Date: 1996/11/19
Raw View

(Previously posted in comp.lang.c++.moderated)

It has always been my understanding that it is allowable
to forward-declare class names, and then use pointers and
references to the name, as in...

    class C;

    C& Fun1 (C& x);
    C* Fun2 (C* x);

My first question: is this legal and portable?  I have always
believed that it is.

Recently, I read "Large-Scale C++ Software Design", by John
Lakos (An excellent book, by the way).  I was quite suprised to
see him using forward-declared class names to declare functions
that passed object by value, as in...

    class C;

    C Fun3 (C x);

Is this legal and portable?  FWIW, The Borland C++ 5.01 compiler
accepts this.

Thanks,
Fred Wasmer



[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1996/11/20
Raw View
Fred Wasmer writes:

>     class C;

>     C& Fun1 (C& x);
>     C* Fun2 (C* x);
[snip]
>     C Fun3 (C x);

> Is this legal and portable?

Yes, it is.  The Sept'96 DWP, in [dcl.fct], states:

5. [...] The type  of
  a  parameter or the return type for a function declaration that is not
  a definition may be an incomplete class type.

As this is not a definition, the code snippet should compile.
However, if one tried to define Fun3 before giving class C a
definition, it should fail.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]