Default Arguments

T

Trevor M. Lango

For the sake of discussion, consider a private member variable within a
class as follows:

int someVar;

Now consider two public member function as follows:

void setSomeVar( const int = 0 );
int getSomeVar( ) const;

Is it possible to have the setSomeVar member function's default argument
be the current value of someVar rather than some arbitrary number (zero
in this example)? So basically if setSomeVar is called without
arguments, it leaves the value of someVar unchanged?

I tried both the following (which don't work):

void setSomeVar( const int = someVar );
void setSomeVar( const int = getSomeVar( ) );

Any ideas on how to either fix one (or both) of these, or an entirely
different approach...?

Any suggestions appreciated!
 
P

Patrik Stellmann

Trevor said:
For the sake of discussion, consider a private member variable within a
class as follows:

int someVar;

Now consider two public member function as follows:

void setSomeVar( const int = 0 );
int getSomeVar( ) const;

Is it possible to have the setSomeVar member function's default argument
be the current value of someVar rather than some arbitrary number (zero
in this example)? So basically if setSomeVar is called without
arguments, it leaves the value of someVar unchanged?

I tried both the following (which don't work):

void setSomeVar( const int = someVar );
void setSomeVar( const int = getSomeVar( ) );

Any ideas on how to either fix one (or both) of these, or an entirely
different approach...?

Any suggestions appreciated!
more a kind of work around but with same result:
[...]
void setSomeVar(const int i) // note: no default argument!
{
someVar = i;
}
void setSomeVar()
{
// do nothing!
}
[...]
alternatively if in the 1st setSomeVar more then the assignment needs to
happen you could of course implement the 2nd like this:
void setSomeVar()
{
setSomeVar(someVar);
}
 
C

Christian Jaeger

With two overloads maybe ?

void setSomeVar() const {/*do nothing*/}
void setSomeVar(int);

But I fail to see the use of this (?)
 
K

Karl Heinz Buchegger

Trevor M. Lango said:
For the sake of discussion, consider a private member variable within a
class as follows:

int someVar;

Now consider two public member function as follows:

void setSomeVar( const int = 0 );
int getSomeVar( ) const;

Is it possible to have the setSomeVar member function's default argument
be the current value of someVar rather than some arbitrary number (zero
in this example)? So basically if setSomeVar is called without
arguments, it leaves the value of someVar unchanged?

Aehm. Why not remove the call at all?
If you don't want a function to change your object then
simply don't call it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top