# Speed diffrence?

This topic is 4840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, Is there any diffrence in speed in following example:

class B
{
public:
void DoIt() { // Do something. }
}

class A
{
public:
A() { myB = new B; }
B* GetB()
{
return myB;
}

public:
B *myB;
}

void main()
{
A myA;
myA.GetB()->DoIt();
myA.myB->DoIt();
}


Which is faster? Granting direct access to a class member or creating a Get function? Is there any diffrence? Is it worth creating Get funtions to keep class members protected?

##### Share on other sites
For starters, why does it matter? How many billion times will you be calling it?

But to answer the question, the way you posted it, I think it would be identical. If, however, you seperated the implementation of the get function from the declaration, ie. put it in a source file, it would be slower. When you dump the function body in the class like you have, it is automatically inlined. So the code in the functions is sort of copied to where you use it if you will. If you seperate it out, you have to request for it to be inlined with the inline keyword.

##### Share on other sites
hehe, I was mostly curious. I know it won't make a noticable difference. =) Thanks for your answer!

##### Share on other sites
They should behave (preformance wise) identically. Declaring GetB as it is - within the class body - makes it implicitly inline (as in the keyword's properties applied to the function), which although quite possibly ignored by your compiler (it is a suggestion, not a mandiate), will be what happens anyways (the compiler can inline what it wants for the most part) - except maybe in Debug mode (in which case the function may truely be seperate so debugging information may be inserted - but compilers can do some inlining even then, AFAIK.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633271
• Total Posts
3011161
• ### Who's Online (See full list)

There are no registered users currently online

×