Would it more or less efficient to call the accessor function than reference the data directly when calling other functions

This is more of a curiosity to me than an actual problem.

Assume that there is a class example which is defined as such.
1
2
3
4
5
6
7
8
9
10
11
class example
{
public: 
      int getNum() const;
      void setNum(int); 
      void printNum() const;
      example(int = 0);

private:
      int num;    
}

Also Assume that the basic program is implemented as.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

example::example(int i)
{
  setNum(i);
}
int example::getNum() const
{
  return num;
}
void example::setNum(int i)
{
  num = i;
}
void example::print() const
{
  cout<< getNum() << endl;
}
int main()
{
  example object(3);
  object.print();
  return 0 ;
}


Now My question comes down to this ignoring that this is an incredibly simple program that compile time and runtime would not matter however would having the extra function call like this:

1
2
3
4
void example::print() const
{
  cout<< getNum() << endl;
}


or calling the data directly such as:

1
2
3
4
void example::print() const
{
  cout<< num << endl;
}


Be more appropriate when calling the print function.
When you just have to return a value which encounters no modifications, I suggest you not to call a function. What's more efficient in terms of runtime? To call a function, return a value and then cout, or directly the cout?

When you have the opposite of what I have said earlier, it's better to call a function which processes the data. By this way, you avoid making your code unreadable when it becomes bigger.

The beauty after all in everything is maintaining an equilibrium between all the factors.

Hope this is what you have asked,
RobertEagle
If the getNum() function is inline then there is no overhead. This is what the inline facility is for.

If the getNum() function is inline then there is no overhead. This is what the inline facility is for.


If a getNum function was needed, then inlining it would be OK, because it is so short & simple. I wouldn't call it from the print function.

I wouldn't use inline for the getNum function in this situation, the following is the correct way to do it:

1
2
3
4
void example::print() const  //why const when there are no args?
{
  std::cout<< num << std::endl;
}


There is no function call, so no need for inlining anything.

That is why member functions have direct access to member variables. There is no inefficiency in this.

One would only have a getNum function, if it was actually needed. That is, if something outside the class needs it.

Sometimes people think they need to provide a get / set function for each member variable. This is often not the case. You have constructors, mutator functions that set multiple variables from 1 function, output functions like print() that display the value of multiple variables.

In general inline functions need a bit of caution, there can be some unwanted side effects, such as code bloating. There are real problems with inlining constructors & destructors because compilers can generate all kinds of code, which can go in the constructor, to deal with destruction coming from an exception being thrown in a constructor. So the constructor is no longer empty. It's worse when base & derived classes have inline constructors because that multiplies the code bloating. The bloating is also multiplied for each variable created.

There are also problems with inline template functions, and pointers to functions

The inline directive is only a request - the compiler may well ignore it.

I got the stuff about caution with inline functions from Scott Meyers Effective C++ 3rd edition item 30.

HTH



> If a getNum function was needed, then inlining it would be OK, because it is so short & simple.
> I wouldn't call it from the print function.
¿why not?

> void example::print() const //why const when there are no args?
The C equivalent would be void print(const example *this)
 
void example::print() const  //why const when there are no args? 

const on the function has nothing to do with any arguments. It tells the compiler that the print() does not modify example. If you should happend to add a statement that modifies example, the compiler would flag that statement.
@ne555 & AbstractionAnon

Thanks for the info about const.

> If a getNum function was needed, then inlining it would be OK, because it is so short & simple.
> I wouldn't call it from the print function.
¿why not?


Why call a function when you have direct access to it?

1
2
3
void example::print() const {
  std::cout<< num << std::endl;
}


As I said, that is why member functions have direct access to member variables.

Why would you call example::getNum() from example::print() ?
Topic archived. No new replies allowed.