By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

using member data as default argument

P: n/a
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
--
( - Remove capital X from email to reply - )

Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Wed, 10 Dec 2003 14:05:39 +0100, hall <Xc***********@yahoo.se>
wrote:
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
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.