+ Reply to Thread
Results 1 to 2 of 2

Thread: Should I include header file for class declaration or use forward class declaration?

  1. #1

    Should I include header file for class declaration or use forward class declaration?

    I am newbie to C/C++ world.

    I want to know what is the best approach to get the class definition.

    Should I include the header file into other header file?

    Or

    I can use forward class declaration for this purpose?

    Code:
    //header file : MyClass1.h
    class MyClass1
    {
    } ;
    
    //another header file : MyClass2.h
    
    //option 1:
    #include "MyClass1.h"
    class MyClass2
    {
    private:
    	 MyClass1 *pMyClass1 ;
    } ;
    
    //option 2:
    class MyClass1 ;
    
    class MyClass2
    {
    private:
    	 MyClass1 *pMyClass1 ;
    } ;
    
    MyClass2.cpp
    #include "MyClass1.h"
    //use MyClass1 here...

  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    28
    It is always good to use forward class declaration whenever possible.

    a. One benefit of this is that it reduces compile time.
    b. Other is that it may avoid cyclic dependency.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts