By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 1,882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

object of the class inside its definition?

P: 84
Is it possible to put an object of class inside a class? What I mean isexmplified in this code:
Expand|Select|Wrap|Line Numbers
  1. class  Spline : public Mathobject, AnchorEnabled {
  2. public:
  3. //(...)
  4.  Spline *mother;
  5. //(...)
  6.  
Is maybe possible becuase there is redundant inclusion of a header file: the header file includes itself, it's called spline.h and at the start you have a:
Expand|Select|Wrap|Line Numbers
  1. #include<spline.h>
Mar 23 '07 #1
Share this Question
Share on Google+
2 Replies


DeMan
100+
P: 1,806
You can declare an instance of a class inside itself (very useful for most data structures).
One of the ways you can get around problems this might cause in teh .h being included multiple times is to put in a failsafe....

in Spline.h, surround exerything in the file with:
Expand|Select|Wrap|Line Numbers
  1. #ifndef _SPLINE_H
  2. #define _SPLINE_H
  3.  
  4. /* Do all the normal things here */
  5.  
  6. #endif
  7.  
This means when Spline.h is included for the first time you set a value to show that you have already declared this header, and it won't be declared again.
Mar 23 '07 #2

P: 84
You can declare an instance of a class inside itself (very useful for most data structures).
But why is it useful? I mean if the compiler doesn't read twice the definition of the class it does not know what and object of type spline is.
One of the ways you can get around problems this might cause in teh .h being included multiple times is to put in a failsafe....

in Spline.h, surround exerything in the file with:
Expand|Select|Wrap|Line Numbers
  1. #ifndef _SPLINE_H
  2. #define _SPLINE_H
  3.  
  4. /* Do all the normal things here */
  5.  
  6. #endif
  7.  
This is already done, it's not me who is writing the program I'm trying to understand it, but thanks for the clarification.
Mar 24 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.