Method Default Argument Fetched Internally from Class?

I need to pass a parameter to a method. Said parameter may sometimes not be passed, then, it needs to default to an internally stored value.

1
2
3
4
5
6
7
8
9
10
11
class simplicity
{
private:
    int x;
public:
    simplicity(): x(0){}
    void dosmth(int nx = x)
    {
        x = nx;
    }
};


But this does not compile:
error: invalid use of non-static data member 'simplicity::x'

How can I fix this?
You can create another function that takes no arguments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class simplicity
{
private:
    int x;
public:
    simplicity(): x(0){}
    void dosmth(int nx)
    {
        x = nx;
    }
    void dosmth()
    {
        dosmth(x);
    }
};
I realized that, but I want the function to take multiple arguments:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class simplicity
{
private:
    int x, y, z, w, q;
public:
    simplicity(): x(0), y(0), z(0), w(0), q(0){}
    void dosmth(int nx = x, int ny = y, int nz = z, int nw = w, int nq = q)
    {
        x = nx;
        y = ny;
        z = nz;
        w = nw;
        q = nq;
    }
};


It seems so redundant to create so many methods, but if there's no other way...
u can pass your argument into your constructor also .
or u can use accessor and mutator to get it

example : accessor and mutator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class TheClass{//First word must be upper case .
private:
        int argument;
public:
        TheClass(){ x = 0 ; }
        void setArgument( int theArgument ){ argument = theArgument ; }
        int getArgument(){ return argument };
};

int main(){
        TheClass theclass ;
        theclass.setArgument(99);
       
        cout << theclass.getArgument() << endl;

        return 0;//exit program
}
Last edited on
Of course, Felicia123, but that's not the kind of functionality I had in mind.
Also, "//First word must be upper case .", I don't think that's C++ specific, only style-specific, like how you use the "{" directly after a function or class on the same line. It's your style.

Basically, the class should return a value upon setting. If the setting was incomplete, it should use the old, stored value:

1
2
3
4
5
6
7
8
class Sum
{
public:
    Sum(): x(0), y(0) {}
    int getSetSum(int a = x, int b = y){x = a; y = b; return x + y;}
private:
    int x, y;
}


But of course, this does not work.
It si in general a bad idea to combine calculating the sum with setting of x and y.

I would split it into separate functions

1
2
3
4
5
6
7
8
9
10
class Sum
{
public:
    Sum(): x(0), y(0) {}
    void setX( int a = 0 ) { x = a; }
    void setY( int b = 0 ) { y = b; }
    int getSum const ( return x + y;}
private:
    int x, y;
}

Well Vlad I need to set something whilst returning at the same time. (For convenience).

But I think I'll just make different functions taking different arguments: (overloading)

1
2
3
4
5
6
7
8
9
10
class Sum
{
public:
    Sum(): x(0), y(0) {}
    int setSum() {return x + y;}
    int setSum(int a) {x = a; return x + y;}
    int setSum(int a, int b) {x = a; y = b; return x + y;}
private:
    int x, y;
}
Topic archived. No new replies allowed.