CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2006
    Posts
    230

    What is the different between including *.h file and foreword deceleration ?

    Hello all
    I notice in c++ code sometimes there is foreword deceleration of class but sometimes its just including the class "*.h" file
    What is the difference and when to use what?

  2. #2
    Join Date
    Feb 2010
    Location
    Russia GMT+3
    Posts
    12

    Re: What is the different between including *.h file and foreword deceleration ?

    Usually a header file contains the definition of a class. This is required to define/create an instance. If you have used the forward declaration then you can define pointers or references but you cannot access members of that class. So called "incomplete type".
    There is one subtle here. You cannot do "Class * ptr = new Class" if Class is incomplete but (suppose the instance was created in other translation unit) you can do "delete ptr" and it leads to undefined behaviour.
    Last edited by evlan; October 24th, 2010 at 03:15 AM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured