Writing my own header file ...

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Writing my own header file ...

Postby thepenguin » Fri Sep 26, 2003 3:12 am

Hi All,

I understand that all the standard C++ libraries are under the namespace std. But if I write my own header file (say, myheader.h) and I want to keep the include declaration as <myheader> instead of <myheader.h>, what should I do? Am I supposed to include my header file in the namespace std or should I define my own namespace?

Thanks for any help!

PS: Sorry for the cross-post!
"The Penguin Rules!!!!" 8)
User avatar
thepenguin
 
Posts: 5
Joined: Thu Sep 25, 2003 9:28 pm

Re: Writing my own header file ...

Postby Filter » Fri Sep 26, 2003 4:27 am

thepenguin wrote:I understand that all the standard C++ libraries are under the namespace std. But if I write my own header file (say, myheader.h) and I want to keep the include declaration as <myheader> instead of <myheader.h>, what should I do?


Think twice.

thepenguin wrote: Am I supposed to include my header file in the namespace std or should I define my own namespace?


Leave std alone. It's a question of good programming style to define namespaces for your different header style, but it is not a "must" to do so.
I do define namespaces when the code is intended to be a (more or less) general library , but I do skip them sometimes if it's a program specific module.
--
Filter
Filter
 

Postby thepenguin » Fri Sep 26, 2003 4:36 am

Thanks for the reply, Filter.
"The Penguin Rules!!!!" 8)
User avatar
thepenguin
 
Posts: 5
Joined: Thu Sep 25, 2003 9:28 pm

Re: Writing my own header file ...

Postby Wizard » Fri Sep 26, 2003 7:02 am

thepenguin wrote:Hi All,

I understand that all the standard C++ libraries are under the namespace std. But if I write my own header file (say, myheader.h) and I want to keep the include declaration as <myheader> instead of <myheader.h>, what should I do? Am I supposed to include my header file in the namespace std or should I define my own namespace?

Thanks for any help!

what you put in the include is the exact name of the file. So just save the file as myheader, instead of myheader.h. I'm assuming you know how to include <> as opposed to include ""
And as was mentioned, it's bad form to mess with other peoples namespaces, especially std.

PS: Sorry for the cross-post!

No problem, taken care of :)
User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

Postby omnius » Fri Sep 26, 2003 9:30 am

Convention is to use a .h extension (or something similar) for your header files. If you don't you may confuse people.
omnius
 
Posts: 496
Joined: Wed Sep 24, 2003 12:03 pm

Re: Writing my own header file ...

Postby thepenguin » Fri Sep 26, 2003 6:34 pm

Wizard wrote: what you put in the include is the exact name of the file. So just save the file as myheader, instead of myheader.h. I'm assuming you know how to include <> as opposed to include ""
And as was mentioned, it's bad form to mess with other peoples namespaces, especially std.



Now this is confusing me. I know for the fact that header files should be saved with extension (*.h). From what I read in the other posts, according to the new ANSI standards, we write include <myheader> and not include <myheader.h> and then declare as using namespace that contains the library. Now what you are saying is to save it without .h extension and not use namespaces at all.

I am not sure what is right now. Someone correct me please!
"The Penguin Rules!!!!" 8)
User avatar
thepenguin
 
Posts: 5
Joined: Thu Sep 25, 2003 9:28 pm

Postby Wizard » Fri Sep 26, 2003 6:49 pm

You don't HAVE to save a header file with a .h, .h is just the convention. It's good to use conventions. The other other convention is that the files in the standard C++ libraries do not end in .h. That's why you include them without a .h.

And I meant that you don't need to create a namespace if you don't want to. Namespaces are useful if you have common objects, plan on distributing your code as a library, or want to use the same name without worrying about overloading.

Am I making sense yet, or just making things worse?
User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

Postby thepenguin » Fri Sep 26, 2003 11:05 pm

Thank you Wizard for your patient reply. It now makes perfect sense about what you said earlier.
"The Penguin Rules!!!!" 8)
User avatar
thepenguin
 
Posts: 5
Joined: Thu Sep 25, 2003 9:28 pm


Return to For Beginners

Who is online

Users browsing this forum: No registered users and 1 guest