Check value of member variable directly or through member function?

Take a look at the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class A
{
	A() : empty{false}
	{
		
	}
	
	bool is_empty() const
	{
		return empty;
	}
	
	void fill()
	{
		// Here's my question
		// if(is_empty())
		// or
		// if(empty)
	}
private:
	bool empty;
};


Is it better to check the value of a member variable directly or call a member function that returns the value?

In my opinion calling a member function will definitely be more expensive than checking it directly, but on the other hand you can change the logic of A::is_empty() without affecting the other member functions that rely on that logic.
Last edited on
I don't think it will be more expensive. The compiler will probably choose to inline the function.
Topic archived. No new replies allowed.