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?
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.