Getter Setter & Performance [new]

I've created already one topic (2months ago~)
You'll find my problem description in this link:
http://www.cplusplus.com/forum/general/131696/


I've tried now the following code:

void MyClass::setObject(int &i)
{
this->i = i;
}

using setter with reference has no real impact on performance, when using simple types/parameter (in this case int)

but when I use a vector with 10++ items, as parameter using reference for setter is twice as fast as without reference/pointer (tested it)

Using getter with reference:

int& MyClass::getObject()
{
return i;
}

or better:

const int& MyClass::getObject()
{
return i;
}

also has no impact using simple types,
much better performance when i use it for vectors or other "big things".


is even a better and faster way?
let me know your thoughts about this.


~slei


In general, getters and setters are bad anyway... so this is kind of a moot point. If you are writing a bunch of getters/setters for your classes, then you are probably not designing your classes properly.
i tried the same test in java and somehow the java version is faster than my c++ version ._.
Try turning compiler optimizations on ;) (Build in Release mode)
Last edited on
i built it in release mode
Topic archived. No new replies allowed.