I broke the memory.

Started by
16 comments, last by swiftcoder 16 years, 9 months ago
Quote:Original post by Niddles
Ok, besides being a little mean, this is great information, and also the type of stuff I was looking for.
However is there anything good about my code?

Kudos for not getting mad at all the (fairly harsh) comments.
In reallity, there isn't much to recomend that particular snippet. As others have said: all those unneccessary pointers are just plain dangerous, passing small value types such as floats by const reference is bad (passes an extra pointer around internally). Eequally KModel is probably not that lightweight and *should* be passed by reference, and if you are making this const-correct (as you most definitely should) *all* functions that don't modify the instance should be const (including GetName() for instance).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Advertisement
I have another question. Is it C++ standard, that when you are in a member function and you are going to modify or get a member variable, should you use this->? Here is an example.
class MyClass{    private:        int MyInt;    public:        int GetInt() const        {            return this->MyInt;        }};

Is this correct, and should I do it all the time?
Also, can I declare a const function if it does this?
void EnableFog() const{    glEnable(GL_FOG);    glFogi(GL_FOG_MODE, GL_EXP);    glFogf(GL_FOG_COLOR, color);    glFogf(GL_FOG_DENSITY, density);}//orvoid DisableFog() const{    glDisable(GL_FOG);}
Quote:Original post by Niddles
I have another question. Is it C++ standard, that when you are in a member function and you are going to modify or get a member variable, should you use this->?

You can, and whether you choose to or not doesn't matter to the compiler. Most people omit it in my experience, but if you really think it looks better then I see no reason not to. If you have a local variable of the same name as a member variable, then the this pointer is required to access to member variable, but you shouldn't have several variables with the same name anyway.

Quote:Also, can I declare a const function if it does this?

Any member function that doesn't alter non-mutable members can be declared const and in most cases should.
You dont have to but can, although it clutters things a bit, its more useful for resolving name problems.

class Foo
{
public:

void Function(int x, int y)
{
// .... code code code

x += 5; // do you mean member or argument "x"
}

protected:

int x, y; // could solve by doing, "int mX, mY"
};

not the best example but illustrates the idea.
Quote:Original post by Niddles
this->? Here is an example.
Is this correct, and should I do it all the time?


It is correct, but you should normally not bother with it. Do it when not doing it would mean something else or would not work (this can happen with advanced use of templates, or more commonly, when you want a parameter of a member function to have the same name as a data member).
Quote:Original post by Niddles
I have another question. Is it C++ standard, that when you are in a member function and you are going to modify or get a member variable, should you use this->?

You can, but it does clutter things a little. The only place I would use it regularly is in constructors, where it is often handy to have the argument names match the member variable names. But if you use initialiser lists it is unnecessary there as well.

Quote:Is this correct, and should I do it all the time?
Also, can I declare a const function if it does this?
*** Source Snippet Removed ***

As long as you don't change any of those member variables, it can be declared const. How you read from them is no concern of the compiler.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Hmmm, I have a class member function problem, and I can't find where the problem is, because I have four functions that set light properties(ambient, diffuse, etc.) and I have it stored in a map. Now the map size increases everytime the set functions are called, but when I call the enable function one call later the members inside the map dissapear. Take a look:
//These five are called right after each othervoid KLight::SetPosition(const KVector3<float> &pos){    LightProperties.insert(std::pair<LightMode, KVector3<float> >(GL_POSITION, pos));    //LightProperties[GL_POSITION] = pos;}void KLight::SetAmbient(const KVector3<float> &amb){    LightProperties.insert(std::pair<LightMode, KVector3<float> >(GL_AMBIENT, amb));    //LightProperties[GL_AMBIENT] = amb;}void KLight::SetDiffuse(const KVector3<float> &dif){    LightProperties.insert(std::pair<LightMode, KVector3<float> >(GL_DIFFUSE, dif));    //LightProperties[GL_DIFFUSE] = dif;}void KLight::SetSpecular(const KVector3<float> &spc){    LightProperties.insert(std::pair<LightMode, KVector3<float> >(GL_SPECULAR, spc));    //LightProperties[GL_SPECULAR] = spc;}//And this function is where they disappear.void KLight::Enable(){    const GLenum lights[] = {GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3,                             GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7};    const GLenum enablinglight = lights[LightNumber];    glEnable(GL_LIGHTING);    glEnable(enablinglight);    char a[20];    sprintf(a, "%d", LightProperties.size());    MessageBox(NULL, a, a, NULL);    for(std::map<LightMode, KVector3<float> >::iterator it = LightProperties.begin();        it != LightProperties.end(); it++)    {        KVector3<float> outcolor = it->second;        glLightfv(enablinglight, it->first, outcolor.GetArray());    }}
A couple of points:
1) Your set functions will only work the first time, because std::map.insert() only inserts if there is no current entry with the same key.
2) glLightfv typically takes a 4-component vector for those arguments, so you will need to pad your 3-element vector.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement