[SOLVED]Accessing members

Get answers to all your basic programming questions. No Ogre questions, please!
User avatar
LJS
Greenskin
Posts: 138
Joined: Wed Jan 09, 2013 8:58 pm
x 6

[SOLVED]Accessing members

Post by LJS »

Why do we use getters and setters, e.g.

Code: Select all

class aBase
{
public:
  int getInt(){ return mInt; };
  int setInt( int int ){ mInt = int; };
protected:
  int mInt;
}
Why not just use:

Code: Select all

class aBase
{
public:
  int mInt;
}
Indeed we can manipulate data passed in a setter or return from a getter but in this example we ain't doing anything to them. And skipping that, would it make the rendering loop faster leaving microsecs for fun stuff?
Last edited by LJS on Thu Jan 17, 2013 2:33 pm, edited 1 time in total.
(am very clumbsy, especially with words on a static screen.)
Ogre 3D 1.9.0 static
Bullet 2.8 static
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: Accessing members

Post by bstone »

The code would be the same in both cases. Why lose the extra flexibility then?
User avatar
saejox
Goblin
Posts: 260
Joined: Tue Oct 25, 2011 1:07 am
x 36

Re: Accessing members

Post by saejox »

LJS wrote:would it make the rendering loop faster leaving microsecs for fun stuff?
Compiler most likely inline those functions. You won't see any speed difference.
Last edited by saejox on Tue Jan 15, 2013 9:58 am, edited 1 time in total.
Nimet - Advanced Ogre3D Mesh/dotScene Viewer
asPEEK - Remote Angelscript debugger with html interface
ogreHTML - HTML5 user interfaces in Ogre
User avatar
Brocan
Orc
Posts: 441
Joined: Tue Aug 01, 2006 1:43 am
Location: Spain!!
x 8

Re: Accessing members

Post by Brocan »

It's basically an encapsulation paradigm from OOP, never expose the internal members of a class. And don't worry about performance, as saejox says, making those functions inline will provoke that compiler will put the calls as direct access to variables :P

Code: Select all

class aBase
{
public:
  inline int getInt(){ return mInt; };
  inline int setInt( int int ){ mInt = int; };
protected:
  int mInt;
}

Code: Select all

aBase test;
int a = test.getInt();
int b = 25;
test.setInt(b);
This will compile in:

Code: Select all

aBase test;
int a = test.mInt;
int b = 25;
test.mInt = b;
BTW, keep in mind that no everything could be inlined. :P
User avatar
LJS
Greenskin
Posts: 138
Joined: Wed Jan 09, 2013 8:58 pm
x 6

Re: Accessing members

Post by LJS »

Code: Select all

class cBase
{
public:
  Ogre::Vector3 Position;
}

...

func:
{
   base->Position.z = 1;
}
rather then:

Code: Select all

class cBase
{
protected:
  Ogre::Vector3 mPosition;
public:
  Ogre::Vector3 getPosition(){ return mDirection; };
  void setPosition( Ogre::Vector3 direction ){ mDirection = direction; };
}

...

func:
{
   Ogre::Vector3 dir = base->getPosition();
   pos.z = 1;
   base->setPosition( pos );
}
bstone wrote:The code would be the same in both cases. Why lose the extra flexibility then?
As far as I know, no extra flexability is added, they're declared but never even used in any other way.
Brocan wrote:...compiler will put the calls as direct access to variables
Still I will need to call it twice without any data change has been made. Manipulating the data in the middle makes it worth calling those :p

Or would base->getPosition().x = 1; work too?

Anyway, I will dive into the inline keyword as it's new to me.

Thanks you all,
Last edited by LJS on Wed Jan 16, 2013 12:57 am, edited 1 time in total.
(am very clumbsy, especially with words on a static screen.)
Ogre 3D 1.9.0 static
Bullet 2.8 static
User avatar
Brocan
Orc
Posts: 441
Joined: Tue Aug 01, 2006 1:43 am
Location: Spain!!
x 8

Re: Accessing members

Post by Brocan »

With your actual code, doing "base->getPosition().x = 1; " is not gonna work. Your function getPosition returns a copy of vector3, so at this time you have two copies, the one in the class, and the one returned, if you modify the returned one, the one in the class will not change.

If you want to do that, you need to pass not a copy, but a reference to the class variable. You could use both pointers or references, but i recommend you to use the last one.

With this code:

Code: Select all

class cBase
{
protected:
  Ogre::Vector3 mDirection;
public:
  Ogre::Vector3& getPosition(){ return mDirection; };
  void setPosition( Ogre::Vector3 direction ){ mDirection = direction };
}
base->getPosition().x = 1; is going to work. BTW, if you need to do something in your cBase class when the position changes, with this version of code you are not going to realise of it.

My recommendation about references and values, is that if your returned value is a class (i.e: Vector3), you should try to return it by reference (normal reference if the value could be changed "Ogre::Vector3&", or const reference if the value could not be changed "const Ogre::Vector3&") rather than return it by value. Returning by value involves copiying the whole class to the stack, and in some situations this could be expensive in execution times.

Hope it helps! :wink:
User avatar
saejox
Goblin
Posts: 260
Joined: Tue Oct 25, 2011 1:07 am
x 36

Re: Accessing members

Post by saejox »

LJS wrote: Or would base->getPosition().x = 1; work too?
No it won't work.
Forget about the inline. It does nothing to alter program behavior. Only affect performance and its automatic. inline keyword doesn't even do anything most of the time.
I shouldn't have mentioned it.

Code: Select all

Ogre::Vector3 getPosition(){ return mDirection; }; 
returns a copy of mDirection. Not the mDirection itself.



Code: Select all

Ogre::Vector3 &getPosition(){ return mDirection; }; // Note the & sign
returns a reference and allows code like this:

Code: Select all

base->getPosition().x = 1;

Anyway, just use direct access. More flexible, less code to write, hassle free.
Getter/Setters are useful for other things.
Nimet - Advanced Ogre3D Mesh/dotScene Viewer
asPEEK - Remote Angelscript debugger with html interface
ogreHTML - HTML5 user interfaces in Ogre
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: Accessing members

Post by bstone »

Brocan wrote:As far as I know, no extra flexability is added, they're declared but never even used in any other way.
The flexibility is in the ability to shuffle the member variable around (e.g. changing its internal presentation, making it calculated based on some other value, moving it to another class and redirecting the access, etc.). All that without ever changing dependent code. Once you have a moderately complex/large project that has to evolve - you'll learn to appreciate that quick enough :wink:

Another benefit is being able to control access to that variable and track any changes. If you want performance equivalent to something like "foo.x += 1" then you can expose a method like "void adjustPositionX( Real delta )" but I don't recommend that - it's seldom worth it.
User avatar
LJS
Greenskin
Posts: 138
Joined: Wed Jan 09, 2013 8:58 pm
x 6

Re: Accessing members

Post by LJS »

@saejox:
You should have mentioned it, saejox. Right you did :). That's what I asked
indirectly, just didn't really knew what I asked myself. Will learn the
keyword as it is implying things happening behind the scene.

I don't mind writting more lines, that's not my issue. Especially with the
automated intellisence and C&P abillity.

@all:
If there is any convention why not to use those directly I am eager to hear.
Actually I never see code of those who knows what they are talking about
using those directly unless stated it is ugly code.

I'll take it as no conventions what so ever except you never know what you
will derive in the future.

Thanks all,
(am very clumbsy, especially with words on a static screen.)
Ogre 3D 1.9.0 static
Bullet 2.8 static