Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not possible: this pointer as a default argument. Why?

The following code won't compile. Why?

class A
{
   int j;
   void f( int i = this->j );
}

Edit, for clarity. This is what I was trying to do, using less lines of code...

class A
{
   void f( int i ){};
   void f( );
   int j;
};

void A::f()
{
    f( j );
}
like image 610
alexandreC Avatar asked Oct 10 '12 20:10

alexandreC


People also ask

How do you fix non default argument follows default argument?

The Python "SyntaxError: non-default argument follows default argument" occurs when we define a function with a positional parameter that follows a default parameter. To solve the error, make sure to specify all default parameters after the positional parameters of the function.

CAN default arguments be provided for pointers to functions?

Default argument cannot be provided for pointers to functions.

Which case default argument passing in function is not allowed?

Default arguments are only allowed in the parameter lists of function declarations and lambda-expressions, (since C++11) and are not allowed in the declarations of pointers to functions, references to functions, or in typedef declarations.

Why shouldn't you make the default arguments an empty list?

Answer: d) is a bad idea because the default [] will accumulate data and the default [] will change with subsequent calls.


1 Answers

Default argument values are bound at compile time.

"this" is only defined at run time, so can't be used.

See here for a fuller explanation: Must default function parameters be constant in C++?

like image 155
Eric Avatar answered Sep 23 '22 16:09

Eric