using member data as default argument

H

hall

Hi.
I'd like to use a data member as a default argument to a member
function, something like this:

class A{
public:
int data;

A(): data(0) { };

fun(int arg=data){ /*function body*/ };
};

The code above will not compile (my borlad compiler says: member A::data
cannot be used without an object), but is there a way to achieve this
(without too much code and too much impact on speed of execution)?

regards
hall
 
T

tom_usenet

Hi.
I'd like to use a data member as a default argument to a member
function, something like this:

class A{
public:
int data;

A(): data(0) { };

fun(int arg=data){ /*function body*/ };
};

The code above will not compile (my borlad compiler says: member A::data
cannot be used without an object), but is there a way to achieve this
(without too much code and too much impact on speed of execution)?

Sure:

void fun(int arg){ /*function body*/ }
void fun() {fun(data);}

You can't access the "this" pointer for a default argument, so the
above is the best you can do. It should have no impact on speed,
thanks to inlining (just make sure the forwarding version is inline).

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top