1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

forward declaration..

Discussion in 'C' started by qcpp, May 13, 2009.

  1. qcpp

    qcpp New Member

    Joined:
    May 13, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    hello friends,
    i am new to C++ i have a question,
    say i have 2 classes in 2 files (in same directory) say... A.cpp A.h and B.cpp B.h
    in B.cpp's public i.e. (Class B's public) i want to have an instance of Class A say A *test;
    Now, my question is whether it is sufficient to include the file A.h in B.h ? or i also need to say Class A (as a forward declaration in B.h) in B.h ?
    When to use the forward declaration ? when is it necessary ? is there any specific conditions ?
    Thanks in Advance.. :)
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    If you just need to use a pointer to A like A *test, then you should use forward declaration of A. Of course including A.h will also do the job, but will make the compilation process slower. If Class B inherits from A (implicitly or explicitly), then including A.h is a must.

    Whenever possible, 'cuz it's faster.

    It's never necessary ! I mean there's nothing that cannot be done without forward declaration.

    Yes, there are. When you forward declare, the compiler has no idea about the contents of the class, it just knows that a class exists. So, you cannot use forward declaration for a class, when you want to access the members of that class.

    Some examples, for further clarification. When you forward declare a class A, you can do things like:

    • Declaring a reference or pointer to it.
    • Declaring a func, whose arg type or return type is that class.
    • Declaring a func, whose arg type or return type is pointer/reference to that class.
    But you cannot:

    • Declare a member of that type.
    • Use it as base class.
    • Define func, whose return/arg-type is that class.
    • De-reference a pointer of the forward declared class.
    You are welcome ! :smile:
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    > there's nothing that cannot be done without forward declaration

    Yes there is.
    Code:
    class A {
      B *b_ptr;
    };
    
    class B {
      A *a_ptr;
    };
    
    This is impossible without at least one forward reference. If A and B are in separate headers then #including each in the other will send the compiler into an infinite loop.

    Solution:
    Code:
    class B; // forward reference
    
    class A {
      B *b_ptr;
    };
    
    class B {
      A *a_ptr;
    };
    
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    :surprised :thinking:

    I would use #pragma once above every header file, instead of forward declaration (just to prove that it can be avoided :p ) :wink:
     
    Last edited: May 13, 2009
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    That's compiler specific (all #pragma's are). Forward declarations are portable.
     
  6. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    Though #pragma once is accepted by MOST compilers,:iagree: that they are not portable. Well, I was just trying to point out a way to avoid forward declaration, it's not my enemy LOL.

    So, we learn, there are somethings(Cyclic dependency) that can be resolved only by forward declaration.
     
  7. qcpp

    qcpp New Member

    Joined:
    May 13, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Wow.. !!! :) thanks a million for all those explanations.. :)

    but i have one more question.. because i have a situation here.. where even though i include .h file and if i try to compile the code without forward declaration , the compiler throws error saying ISO C++ forbids something (at the line where i use a A *test)
    So, even though i include A.h why cant the compiler find that there is a class named A, What makes the compiler to throw error ?
     
  8. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    If class A (in A.h) does not contain anything related to class B (in B.h), but only class B contains A *test, then including A.h in B.h should work. :surprised:

    If it does not, please post the contents of A.h and B.h and the details of the error.
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Post the exact error please, not a vague interpretation of what you think it might say.
     

Share This Page