Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Guest Anonymous Poster

Using classes in multiple object files

This topic is 6922 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here's the standard solution to multiple usage of an h file.
If we had a file called example.h at the top of the file simply put

#ifndef _EXAMPLE_H_
#define _EXAMPLE_H_

All your code

#endif

The tag _EXAMPLE_H_ is just an abstract name but it is useful to make it a name which makes sense, so this is the format I use.

This prevents multiple declarations and allows you to declare classes in the .h file which is the best place to put them for external use.

Share this post


Link to post
Share on other sites
Advertisement
the use of sentinels/include guards/whatever you call them is customary. however, you can do the work of those three lines with

#pragma once

somewhere in each of your .h files. (i usually put it as the first line after the comment saying what the file is)

Share this post


Link to post
Share on other sites
I hadn't even heard of this pragma directive, so I'm glad to say I learnt something
However, a warning. Pragma's are compiler specific and there's no guarentee that every compiler will know this directive.
It's probably nothing to worry about but, as you can probably tell, I like having the last word

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
With classes, is it possible to declare them in one object file and use them in another? you can do this with individual functions using extern, but how can you do it with classes, since I've found the only way is to put the class in the .h file, but won't that mean I will be declaring it multiple times, and I'll get conflicts when I try to link it?

------------------

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!