Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a member variable as a default argument in C++?

I want to make an argument for one of the member functions optional. When no argument is provided, it would use an member variable.

However, when I tried to compile it it shows "error: invalid use of non-static data member 'Object::initPos'"

Just to isolate the problem, I tried defaulting an int type and it compiled fine. I wonder what is the problem with my code and how I could use a member function as default value.

Thank you for your help!

Object.h

class Object {     public:        ...        void MoveTo(double speed, Point position);      protected:        Point initPos;         Point currPos;  }; 

Object.c

void Object::MoveTo(double speed, Point position = initPos) {     currPos = postion; } 

Point.h

class Point {     ...      private:        double x;        double y;        double z;  }; 
like image 343
tuzzer Avatar asked Feb 15 '12 01:02

tuzzer


People also ask

Can we use default arguments in C?

Yes. :-) But not in a way you would expect. Unfortunately, C doesn't allow you to overload methods so you'd end up with two different functions. Still, by calling f2, you'd actually be calling f1 with a default value.

What can be used as a default function argument?

In C++ programming, we can provide default values for function parameters. If a function with default arguments is called without passing arguments, then the default parameters are used. However, if arguments are passed while calling the function, the default arguments are ignored.

Can we give default value to arguments?

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.

What is default arguments give example?

The default value is given in the form of variable initialization. Example : void defaultvalue(int n1 = 10, n2 = 100); 3. The default arguments facilitate the function call statement with partial or no arguments.


1 Answers

Default argument expressions for a member function can only depend on things that are in class or global scope. The default argument also has to be specified in the method's declaration (i.e. in the header file).

To get around this, you need 2 overloads of your MoveTo method. One that takes 1 argument, and another that takes 2 arguments. The method taking 1 argument calls the other method, passing along the value that you consider as the default.

void Object::MoveTo(double speed) {     MoveTo(speed, initPos); }  void Object::MoveTo(double speed, Point position) {     // Everything is done here. } 

Note that when you make MoveTo(double) call MoveTo(double, Point), it allows you to write the implementation of MoveTo only once, thereby respecting the DRY principle.

like image 118
Emile Cormier Avatar answered Oct 08 '22 19:10

Emile Cormier