Question about declaring classes

Discussion in 'C++' started by Antonio Rivas, Feb 14, 2008.

  1. Hello again :)

    Lately I find code that declares a class this way:

    mainheader.h
    ------------
    #include "myClass.h"

    ....

    class myClass;

    .....

    This declaration use to be in a header (mainheader.h in this case) that
    calls another header in which that class is declared in a standard way,
    i.e.:

    myClass.h
    ---------
    class myClass {

    public:
    myClass();
    ~myClass();
    ....
    }; // myClass

    What I don't understand is the purpose of the second empty declaration
    of myClass after call myClass.h where is declared for the first time.
    What puzzles me most is that there's no safeguard #ifdef to prevent
    multiple declarations of myClass and the only reason I see for such
    absence is that is legal code that won't throw a multiple declaration
    error and that has a reason to make it this way.
    An enlightment in this matter will be welcome.
     
    Antonio Rivas, Feb 14, 2008
    #1
    1. Advertising

  2. Antonio Rivas

    Ondra Holub Guest

    On 14 Ún, 11:17, Antonio Rivas <> wrote:
    > Hello again :)
    >
    > Lately I find code that declares a class this way:
    >
    > mainheader.h
    > ------------
    > #include "myClass.h"
    >
    > ...
    >
    > class myClass;
    >
    > ....
    >
    > This declaration use to be in a header (mainheader.h in this case) that
    > calls another header in which that class is declared in a standard way,
    > i.e.:
    >
    > myClass.h
    > ---------
    > class myClass {
    >
    > public:
    > myClass();
    > ~myClass();
    > ...
    >
    > }; // myClass
    >
    > What I don't understand is the purpose of the second empty declaration
    > of myClass after call myClass.h where is declared for the first time.
    > What puzzles me most is that there's no safeguard #ifdef to prevent
    > multiple declarations of myClass and the only reason I see for such
    > absence is that is legal code that won't throw a multiple declaration
    > error and that has a reason to make it this way.
    > An enlightment in this matter will be welcome.


    I do not know what could it be good for. I think it is nonsense.

    The forward declaration of class is useful when such class is used
    only be its pointers or references, so you can decrease header
    dependencies:

    // file my_class.hpp

    #ifndef MYCLASS_HPP_INCLUDED
    #define MYCLASS_HPP_INCLUDED

    class MyClass
    {
    // ...
    };

    #endif


    // file other_class.hpp

    #ifndef OTHER_CLASS_HPP_INCLUDED
    #define OTHER_CLASS_HPP_INCLUDED

    class MyClass;

    class OtherClass
    {
    // ...

    // No need to #include myclass.hpp for following line
    void Fn(MyClass& my_class);
    };

    #endif


    // file other_class.cpp

    #include "other_class.hpp"
    #include "my_class.hpp"

    void OtherClass::Fn(MyClass& my_class)
    {
    // For following line we need to have included my_class.hpp
    my_class.method();
    }
     
    Ondra Holub, Feb 14, 2008
    #2
    1. Advertising

  3. Ondra Holub escribió:
    > On 14 Ún, 11:17, Antonio Rivas <> wrote:
    >> Hello again :)
    >>
    >> Lately I find code that declares a class this way:
    >>
    >> mainheader.h
    >> ------------
    >> #include "myClass.h"
    >>
    >> ...
    >>
    >> class myClass;
    >>
    >> ....
    >>
    >> This declaration use to be in a header (mainheader.h in this case) that
    >> calls another header in which that class is declared in a standard way,
    >> i.e.:
    >>
    >> myClass.h
    >> ---------
    >> class myClass {
    >>
    >> public:
    >> myClass();
    >> ~myClass();
    >> ...
    >>
    >> }; // myClass
    >>
    >> What I don't understand is the purpose of the second empty declaration
    >> of myClass after call myClass.h where is declared for the first time.
    >> What puzzles me most is that there's no safeguard #ifdef to prevent
    >> multiple declarations of myClass and the only reason I see for such
    >> absence is that is legal code that won't throw a multiple declaration
    >> error and that has a reason to make it this way.
    >> An enlightment in this matter will be welcome.

    >
    > I do not know what could it be good for. I think it is nonsense.
    >

    Was my first thought too the first time I saw it but as I said I find
    such forward declarations quite often lately.

    > The forward declaration of class is useful when such class is used
    > only be its pointers or references, so you can decrease header
    > dependencies:
    >
    > // file my_class.hpp
    >
    > #ifndef MYCLASS_HPP_INCLUDED
    > #define MYCLASS_HPP_INCLUDED
    >
    > class MyClass
    > {
    > // ...
    > };
    >
    > #endif
    >
    >
    > // file other_class.hpp
    >
    > #ifndef OTHER_CLASS_HPP_INCLUDED
    > #define OTHER_CLASS_HPP_INCLUDED
    >
    > class MyClass;
    >
    > class OtherClass
    > {
    > // ...
    >
    > // No need to #include myclass.hpp for following line
    > void Fn(MyClass& my_class);
    > };
    >
    > #endif
    >
    >
    > // file other_class.cpp
    >
    > #include "other_class.hpp"
    > #include "my_class.hpp"
    >
    > void OtherClass::Fn(MyClass& my_class)
    > {
    > // For following line we need to have included my_class.hpp
    > my_class.method();
    > }


    This makes sense to me. Reduce header dependencies could be the reason.

    Thank you for the enlightment :)
     
    Antonio Rivas, Feb 14, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mortal Wombat
    Replies:
    3
    Views:
    6,837
    Mortal Wombat
    Aug 7, 2003
  2. ctor
    Replies:
    1
    Views:
    363
  3. alternativa
    Replies:
    8
    Views:
    341
    Cy Edmunds
    Apr 23, 2006
  4. Markus Svilans

    Declaring structs vs. classes

    Markus Svilans, May 15, 2006, in forum: C++
    Replies:
    7
    Views:
    374
    Markus Svilans
    May 16, 2006
  5. Replies:
    2
    Views:
    673
    mlimber
    Jun 9, 2006
Loading...

Share This Page