advantages of forward declaration?

This is a discussion on advantages of forward declaration? within the C++ Programming forums, part of the General Programming Boards category; What would be the advantages to using a forward declaration of a class?...

  1. #1
    Registered User carebear's Avatar
    Join Date
    May 2006
    Location
    Missouri
    Posts
    7

    advantages of forward declaration?

    What would be the advantages to using a forward declaration of a class?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    I can only think of one, and that is if you had a complex hiearchy of header files. It sort of places a band aid on an organizational problem, and that probably should be addressed in a better way to boot. So the real question is, why are you asking?

  3. #3
    Registered User carebear's Avatar
    Join Date
    May 2006
    Location
    Missouri
    Posts
    7
    Because I'm trying to figure out what any of the advantages would be for a class, and I'd searched my book and it explained how you do it...but not what the advantages would be really and I tried doing several internet searches and board searches to no avail so I decided to just ask and hope that someone who understands it a little better or has maybe actually used it rather than read a little on it would be able to tell me of any advantages they knew of. and Thank You for responding so quickly it's very much appreciated.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    When classes reference each other, you have to forward declare one of the class names before you define either of the classes
    Code:
    class myClassB;
    
    class myClassA {
        void myFunc(const myClassB&);
    };
    
    class myClassB {
        void myFunc(const myClassA&);
    };
    That's what I learned anyway.
    Last edited by SlyMaelstrom; 05-03-2006 at 12:39 AM.
    Sent from my iPadģ

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    I found a tip that says, quote:
    Suppose you want to define a new class B that uses objects of class A.
    1. B only uses references or pointers to A. Use forward declaration then : you don't need to include <A.h>. This will in turn speed a little bit the compilation.
    Code:
    class A ;
      
      class B {
        private:
          A* fPtrA ;
        public:
          void mymethod(const& A) const ;
      } ;
    2. B derives from A or B explicitely (or implicitely) uses objects of class A. You then need to include <A.h>
    But I suggest that you don't follow this either. Compilers are very fast as it is, and this code does nothing but confuse the purpose of A.

    It's a hack.
    Last edited by whiteflags; 05-03-2006 at 12:47 AM.

  6. #6
    Registered User carebear's Avatar
    Join Date
    May 2006
    Location
    Missouri
    Posts
    7
    Thanks a load guys!!! I really appreciate the help, I got down to like 3 questions left and looked all over and seem to have gotten stumped. I'm so glad for the help.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,326
    >> But I suggest that you don't follow this either. Compilers are very fast as it is, and this code does nothing but confuse the purpose of A.

    How large are the projects you are compiling? How long do your projects take to compile? For a beginner, the compile times won't matter, but the point is to learn best practice. Once you get into some larger real-world projects, the compile times make a big difference.

    >> It's a hack.
    No, it's not. Always prefer a forward declaration when it will suffice.

    In addition to speeding up compile times and being the only way you can have two classes reference each other, a forward declaration can be useful in maintaining encapsulation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21