Jump to content
  • Advertisement
Sign in to follow this  
jarod83

Java vs. C++

This topic is 4966 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

Hello guys! A simple question... In Jave I can say
class SomeClass {
public SomeClass() {
init();
}
private init() {
// initialize here...
}

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 ?

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
Share on other sites

class SomeClass
{
public:
SomeClass() // no return type since it's a constructor
{
init();
}

private:
void init()
{
cout << "oi\n";
}
};

Share this post


Link to post
Share on other sites

const int FACE_COUNT = 6; // 6 faces for a box

class 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;
}

Share this post


Link to post
Share on other sites
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:

class Foo {

public:
void bar();

};

void Foo::bar() {

//implement

}



Instead of this:

class Foo {

public:
void bar() {

//implementation

}

};

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!