hmm c to c++ issues

dave05
Unregistered
 
Post: #1
hey everyone.

I've been working on an OpenGL game in Carbon/C for some time now, and the amount of code is beginning to get overwhelming. I'm currently implementing all the in-game graphics in a single 5000-line (and growing) C file called "UserInterface.c". I have separate files for OpenGL implementation, Quicktime Music Implementation, Sound Effects, Etc. Each c file has a corresponding header file,

and since the amount of data structures and enumerations was becoming insane, I have seperate header files for my structs and enums. All the headers are precompiled. Anyways:

My goal is to convert all my data structures to C++ objects. I have done this with one of many of the structures, making it self-contained. However, placing some object declarations in UserInterface.c didn't work. So I thought C++ objects could only be recognized by c++ source files, so I changed the name of my UI implementation file to "UserInterface.c++". However, now all my internal calls to UserInterface functions, as well as external calls to C functions defined in "OpenGL.h", are listed as unrecognized functions or something.

My question is how to proceed. I believe what I need to do is change all my c files to c++, and alter their definitions in the headers. But I don't really need everything to be an object...

Is it possible to simply declare in OpenGL.h as such?

class {

function declarations...
}

without actually having to instantiate an OpenGL object in my other source files? How exactly does a static function work in c++? Should I declare all my methods as static? Someone who's a c++ expert, help! Please & thanks.
Oldtimer
Posts: 834
Joined: 2002.09
Post: #2
First off, the file extension should be .cpp, not .c++. Smile
Second, you're right that you can't use C++ objects in C files, but the opposite is also true - to a degree. All of your C headers need to be bracketed in an:
Code:
extern "C"
{
    #include "MyHeader.h"
}
to be able to call C functions from the C++ code.

Quote:without actually having to instantiate an OpenGL object in my other source files?
Not sure what you're meaning here, but I'm guessing that you're trying to encapsulate all of your functions in a C++ object. If so, you'd need them to be static (more about that later) but it doesn't really make sense. If there are functions that do not need to be a part of a class, let them stay as functions - they'll still work just fine.

As for static methods, they are members of a class, but you don't need an actual instance of the class to work on them. For instance, a regular member class needs the following:
Code:
MyObject* theObject = new MyObject();
theObject->MyMethod();
But a static member can be used thus:
Code:
MyObject::MyStaticMethod();

HTH
dave05
Unregistered
 
Post: #3
man do i love these forums! Smile
Thread Closed