Re-forward declaration of types which were already forward declared

Discussion in 'C++' started by qazmlp, Feb 15, 2004.

  1. qazmlp

    qazmlp Guest

    Is it a good style to re-forward declare the types, which were already
    forward declared in base header file, in derived class header file?


    // base.h
    class addrClass;
    class base
    {
    public:
    virtual void setAddress(addrClass* node);

    // Other members
    } ;


    // derived.h

    // Is it a good style to forward declare addrClass here?
    // As setAddress method and hence the parameter of type 'addrClass*'
    // is from the base class, it may not be required, I suppose.
    class derived : public base
    {
    public:
    virtual void setAddress(addrClass* node);

    // Other members
    } ;


    And, similarly, what about #include-ing the header which has to be
    added
    only because of the Base class method signatures?
     
    qazmlp, Feb 15, 2004
    #1
    1. Advertising

  2. "qazmlp" <> wrote in message:

    > Is it a good style to re-forward declare the types, which were

    already
    > forward declared in base header file, in derived class header file?
    >
    >
    > // base.h
    > class addrClass;
    > class base
    > {
    > public:
    > virtual void setAddress(addrClass* node);
    >
    > // Other members
    > } ;
    >
    >
    > // derived.h
    >
    > // Is it a good style to forward declare addrClass here?
    > // As setAddress method and hence the parameter of type 'addrClass*'
    > // is from the base class, it may not be required, I suppose.
    > class derived : public base
    > {
    > public:
    > virtual void setAddress(addrClass* node);
    >
    > // Other members
    > } ;
    >


    Obviously, you need to include "base.h" here, at least indirectly. It
    is not necessary to forward declare addrClass, sinmce a forward
    declaration will already be included.

    Under some circumsances it is good programming practice to forward
    decalre a class decalred by an included header, for instance, if
    addrClass was just an implementation detail in "base.h" which might be
    subject to change. Here it is part of the interface of a public
    virtual function defined in the base class, so I don't see any reason
    to redeclare it.

    > And, similarly, what about #include-ing the header which has to be
    > added
    > only because of the Base class method signatures?


    Which header? The header containing the definition of addrClass?
    Definitely not needed in your example. Never include more headers than
    absolutely necessary.

    Jonathan
     
    Jonathan Turkanis, Feb 15, 2004
    #2
    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. JohnZing

    declared or not declared ?

    JohnZing, Feb 5, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    1,619
    Jon Paal
    Feb 5, 2006
  2. Replies:
    4
    Views:
    730
  3. Replies:
    2
    Views:
    325
    xarax
    Sep 18, 2004
  4. James Stroud

    Learning which modules were loaded

    James Stroud, Jun 6, 2008, in forum: Python
    Replies:
    1
    Views:
    275
    Gary Herron
    Jun 6, 2008
  5. Anthony Williams
    Replies:
    10
    Views:
    601
    Anthony Williams
    Sep 16, 2003
Loading...

Share This Page