How to use a member variable as a default argument in C++?
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.
initializing default parameter with class member function/variable
The default argument is considered to be provided from the caller side context. It just doesn't know the object on which the non-static member function return5
could be called on.
You can make return5
a static
member function, which doesn't require an object to be called on. E.g.
class C {
...
static int return5() { return 5; }
void f(int d = return5()) {
}
...
};
Or make another overload function as
class C {
private:
int n{ 5 };
public:
int return5() { return 5; }
void f(int d) {
}
void f() {
f(return5());
}
void ff(int d) {
}
void ff() {
ff(n);
}
};
c++ default parameters class members
According to the C++ Standard (8.3.6 Default arguments)
- ...Similarly, a non-static member shall not be used in a default argument, even if it is not evaluated, unless it appears as the
id-expression of a class member access expression (5.2.5) or unless it
is used to form a pointer to member (5.3.1). [ Example: the
declaration of X::mem1() in the following example is ill-formed
because no object is supplied for the non-static member X::a used as
an initializer.
int b;
class X {
int a;
int mem1(int i = a); // error: non-static member a
// used as default argument
int mem2(int i = b); // OK; use X::b
static int b;
};
You could overload function add
. For example
void add( int key );
void add( int key, node *curr );
The first function would use root
by default. It could simply call the second function passing as the second argument the node root.
Using a class member as a default argument for a member function
[dcl.fct.default]/8:
The keyword
this
shall not be used in a default argument of a member function.
This is a special case of a general problem: You cannot refer to other parameters in a default argument of a parameter. I.e.
void f(int a, int b = a) {}
Is ill-formed. And so would be
class A
{
int j;
};
void f(A* this, int i = this->j) {}
Which is basically what the compiler transforms a member function of the form void f(int i = j) {}
into. This originates from the fact that the order of evaluation of function arguments and the postfix-expression (which constitutes the object argument) is unspecified. [dcl.fct.default]/9:
Default arguments are evaluated each time the function is called.
The order of evaluation of function arguments is unspecified. Consequently, parameters of a function shall not be used in a default
argument, even if they are not evaluated.
Default arguments as non-static member variables
Overload Bar
:
int Bar()
{
return x_ + y_;
}
int Bar(int x)
{
return x + y_;
}
int Bar(int x, int y)
{
return x + y;
}
Thanks to @Jarod42 for this improvement:
int Bar(int a, int b)
{
return a + b;
}
int Bar(int a)
{
return Bar(a, y_);
}
int Bar()
{
return Bar(x_, y_);
}
The real-world problem you're trying to solve is more likely to benefit from this refactoring than the original problem of summing two numbers. This behavior is more obviously identical to that you were hoping to achieve through default arguments.
C default arguments
Not really. The only way would be to write a varargs function and manually fill in default values for arguments which the caller doesn't pass.
Passing a variables as a default parameters in function c++
You could make an overload like this:
void insertData(T data1) {
insertData(data1, TreeTop);
}
void insertData(T data1, Tree<T> *tree) {
// Code
}
Related Topics
Non-Blocking Worker - Interrupt File Copy
How to Get a Process Handle by Its Name in C++
Linux: Executing Child Process With Piped Stdin/Stdout
C++11 Allows In-Class Initialization of Non-Static and Non-Const Members. What Changed
Using Unicode in C++ Source Code
Is the 'Override' Keyword Just a Check For a Overridden Virtual Method
How to Code a Modulo (%) Operator in C/C++/Obj-C That Handles Negative Numbers
Static_Assert Dependent on Non-Type Template Parameter (Different Behavior on Gcc and Clang)
What Is a Subnormal Floating Point Number
Conditional Operator Differences Between C and C++
Macro For Dllexport/Dllimport Switch
What Is More Efficient? Using Pow to Square or Just Multiply It With Itself
C/C++ Unsigned Integer Overflow
How to Convert String to Char Array in C++