Google Answers Logo
View Question
 
Q: C++ OOP ( No Answer,   1 Comment )
Question  
Subject: C++ OOP
Category: Computers > Programming
Asked by: azl42-ga
List Price: $2.00
Posted: 28 Oct 2002 09:25 PST
Expires: 28 Oct 2002 12:18 PST
Question ID: 91056
What are C++'s rules regarding placement and duplication of #include
statements such as #include "math.h" when using classes where the
classes rely on other classes?
Answer  
There is no answer at this time.

Comments  
Subject: Re: C++ OOP
From: mr_eponymous-ga on 28 Oct 2002 10:17 PST
 
As far as I know, the C++ preprocessor is responsible for replacing
#include statements with the contents of the actual files.  With my
version of gcc, this seems to be the only rule.  If you were to do
something stupid like this

(contents of file named recurs.cc:)

#include <iostream>
#include "recurs.cc"

void main() {
  cout << "recursive!" << endl;
}

the preprocessor keeps doing a recursive include until it gets an
error.  On my system [gcc version 2.95.4 20011002 (Debian prerelease)]
compilation of this eventually resulted in this error:

recurs.cc:2: macro or `#include' recursion too deep


So, to answer your question, you can duplicate things as much as you
like as long as you are careful.  If the same file is included more
than once and it declares variables or functions the compiler will
return a variable already defined error.  The standard libraries are
generally not a problem because they define flags that keep the
contents of the file from being used more than once.  Math.h is
probably surrounded by something like

#ifndef __MATH_H__
#define __MATH_H__

// contents of math.h here

#endif

This ensures that no matter how many times math.h is included in a
file, it will only get read once, since on subsequent passes
__MATH_H__ will be defined (#ifndef means process the section until
#endif only if the variable following is not defined).

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy