Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a struct member in one of the struct's method as default parameter?

Tags:

c++

structure

I want to pass the variable a as a default parameter in method func().

struct S {
    int a;
    int func(int b = a) {
        // do something
    }
};

On compiling it shows error. Is it possible to somehow get around this?

like image 284
Debadipto Biswas Avatar asked Dec 29 '25 13:12

Debadipto Biswas


1 Answers

It is not possible to use the value of a non-static member as default argument. You can use an overload instead:

struct S {
    int a;
    int func(int b) {
        // do something
    }
    int func() { return func(a); }
};
like image 67
463035818_is_not_a_number Avatar answered Jan 01 '26 04:01

463035818_is_not_a_number



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!