0

I am trying to use the class member as the default parameter in one of the class methods of the same class.

Sample Code:

class A{
    public:
    struct asp *N;
    // some other members..
    void my_method(int arg1, struct asp *arg2=this->N){
    // Definition
    }
};

When I try to compile the code, I get the following error:

asp.cpp:19:50: error: ‘this’ may not be used in this context

I have tried both with and without this keyword, without any success. Is there any restrictions to the usage of the data members within the class?

Tarun Gaba
  • 1,103
  • 1
  • 8
  • 16
  • The other question's answer clarifies that we cannot use `this` as it is defined at run-time. But it does not states why we cannot use the member without the `this` keyword, since it is defined at compile time as well. – Tarun Gaba Sep 20 '15 at 07:50
  • And the solution is to add another function that supplies the second argument `void my_method(int arg1) { my_method(arg1, N); }` – Bo Persson Sep 20 '15 at 09:10
  • That is a convincing hack around. Thanks man! Was looking for something more intuitive, but that works for now. :) – Tarun Gaba Sep 20 '15 at 09:36

0 Answers0