class SomeClass {
public SomeClass() {
init();
}
private init() {
// initialize here...
}
Java vs. C++
Hello guys!
A simple question...
In Jave I can say
but if I do similary in C++ nothing happens when i call the init() function! Does the function have to be virtual ore something like that, because I've tried to make it virtual, seems like the init() function never get called.
Can anyone help ?
class SomeClass {public: void SomeClass() { init(); }private: void init() { // initialize here... }};
works perfect for me ;-)
What exactly is the problem? And how did you worte it in cpp
class SomeClass{public: SomeClass() // no return type since it's a constructor { init(); }private: void init() { cout << "oi\n"; }};
const int FACE_COUNT = 6; // 6 faces for a boxclass Box {public: Box(float w, float h, float l, char *img[FACE_COUNT]); ~Box() {} void drawBox(); int LoadGLTextures(); void setWidth(float w) { width = w; } void setHieght(float h) { height = h; } void setLength(float l) { length = l; } void setLocation(float x, float y, float z) { location[0] = x; location[1] = y; location[2] = z; } void addToLocation(float x, float y, float z) { location[0] += x; location[1] += y; location[2] += z; }private: AUX_RGBImageRec *LoadBMP(char *Filename) { // FIle handler FILE *File = NULL; // Check if name of the file is valid, ei. the file path existed ? if (!Filename) { return NULL; } // Read the file File = fopen(Filename, "r"); // Close file handler and return a pointer to bitmap if (File) { fclose(File); return auxDIBImageLoad(Filename); } return NULL; } float location[3]; float width; float height; float length; GLuint boxTextures[1];};Box::Box(float w, float h, float l, char *img[]) { width = w; height = h; length = l; LoadGLTextures();}void Box::drawBox() { // TODO: add rotations and other stuff here... glBindTexture(GL_TEXTURE_2D, boxTextures[0]); // *** Front of the box *** glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); glTexCoord2f(0.5f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // *** Back of the box *** glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f); // *** Top of the box *** glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f); // *** Bottom of the box *** glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, -1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // *** Right of the box *** glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f); // *** Left of the box *** glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glEnd();}int Box::LoadGLTextures() { int Status = FALSE; AUX_RGBImageRec *TextureImage[1]; memset(TextureImage, 0, sizeof(void *)*1); // Loading the texture image if ( TextureImage[0] = LoadBMP("images/NeHe2.bmp") ) { Status = TRUE; } // Create and generate the texture glGenTextures(1, &boxTextures[0]); glBindTexture(GL_TEXTURE_2D, boxTextures[0]); // Generating the actual texture glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); // Set the filtering for the texture glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Free up the memory if (TextureImage[0]) { if (TextureImage[0]->data) { free(TextureImage[0]->data); } free(TextureImage[0]); } return Status;}
I've solved the problem: I was trying to create the textures before I had a valid OpenGL context... dough!!!
This isn't the cause of the problem, but you should probably make it your practice to declare member function implementation separately from the class declaration. Some compilers might interpret functions implemented inside the class as requests for inlining, which you won't want in most cases (with larger functions anyway). Many compilers are smart enough to decide on their own whether or not to inline, but not all of them are.
For example do this:
Instead of this:
For example do this:
class Foo { public: void bar();};void Foo::bar() { //implement}
Instead of this:
class Foo { public: void bar() { //implementation }};
I think that comes from a long use of Java where everything dealing with the class is inside one .java file. To me that makes much more sense and my early studies of C++ were very confusing until I figured out what that dern :: was for (scope resolution operator i think it's called). This is the first time I've heard that there is any sort of difference between where we implement the functions.
I always put all the code in the class definition until its finished THEN I put them into a header file. Its a pain jumping between files when you keep changing stuff.
This is something you may not know, but every time you define a function within your class header instead of its implementation, the function is automatically declared inline. You add quite a bit of bloat to the executable/binary doing that.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement