Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Using classes in multiple object files


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 13 October 1999 - 04:06 AM

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?

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


Sponsor:

#2 MikeD   Members   -  Reputation: 158

Like
Likes
Like

Posted 11 October 1999 - 11:14 PM

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.


#3 TANSTAAFL   Moderators   -  Reputation: 1152

Like
Likes
Like

Posted 13 October 1999 - 03:12 AM

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)


#4 MikeD   Members   -  Reputation: 158

Like
Likes
Like

Posted 13 October 1999 - 04:06 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS