Skip to main content
gamedev.net
🔒 Locked

Visual c++ file relations

Started by superk 6 replies 450+ views
Original Post
superk
superk
Hey all, could I get some help here???? I have been programming in visual c++/directx for some time but just recently decided to start splitting up me code into different c++ files. Can you have multiple .cpp files with functions that can call each other, or do all but one of those files have to be class code??? How do the .h and .cpp files relate??? Can I cross call .cpp files'' functions??? Thanks, Kevin
krez
krez
use header files. basically, for each .cpp file you have that defines functions and classes that will be used somewhere else, you put the prototype of the function or class in the .h file.
      
// testfile.cpp

class SomeClass
{
// class stuff (private variables, functions, et cetera)

};
int SomeFunc(int SomeArg)
{
// do stuff, return an integer

};

then:
  
// testfile.h

class SomeClass;
int SomeFunc(int);

then, in any .cpp file that uses the functions or classes, you include the header file:
  
// otherfile.cpp

#include "testfile.h";
int main()
{
SomeClass feh;
int Foo;
return SomeFunc(Foo);
};

header files basically are just inserted into the source code before it is compiled, so they are used to define prototypes from other source files...
better now?

--- krez (krezisback@aol.com)

EDIT -- the source tags take out carriage returns? i had to put a semicolon after my include above, because main was being attached to the previous line... weird...

Edited by - krez on November 27, 2001 10:49:50 PM
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
superk
superk
Ya, for sure.

Thanks a lot.
superk
superk
Hey, Some more questions for all of ya.

.cpp files in a program can access global variables of other .cpp files, right??? Should I define my globals in the certain .cpp file''s header, or just in the program''s .cpp.

Thanks
kevin
Sturm
Sturm
First off the answer would be to create a new header, and call it something like global.h. This file contains all global vars and functions.

But basically globals point in a direction of not so great design. Not that you can avoid globals at all time, but in a pure OO world there aren''t anything like a global variable. Remember that all variables belong to a class, so at max you would have a global class, which you instantiate in your main method, and from there on just passed as a parameter to whomever needs it.

-- Sturm
---------------------------------------------------Life after death? No thanks, I want to live NOW --- Sturm 2001
superk
superk
Ok...

So should I put all of the global variables for the application in a class in a global.h file???

Kevin
Oluseyi
Oluseyi
quote:
Original post by superk
So should I put all of the global variables for the application in a class in a global.h file???

Yes and no. Look up the extern keyword.


Start Here!
Arild Fines
Arild Fines
quote:
Original post by superk
Ok...

So should I put all of the global variables for the application in a class in a global.h file???

Kevin


Put them in the recycle bin and redesign your app not to use globals.



Psalm 137:9: "Happy shall he be, that taketh and dasheth thy little ones against the stones."
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.