I want to create a class that has two integer member variables and a function that has two optional arguments. If these arguments are provided, the function returns their sum; if these arguments are not provided, the function returns the sum of its two member variables.
Here is the code:
class Foo { private: int x_; int y_; public: Foo(int x, int y) : x_(x), y_(y){} int Bar(int a = x_, int b = y_) { int z = a + b; return z; } };
However, I get the following compilation error:
invalid use of non-static data member 'Foo::x_' int x_; ^ invalid use of non-static data member 'Foo::y_' int y_; ^
This suggests that member variables must be static in order to use them as default arguments in a function. But I do not want them to be static ...
What's the solution?
source share