|
-
October 24th, 2010, 12:46 AM
#1
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?
-
October 24th, 2010, 03:00 AM
#2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|