Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > using member data as default argument

Reply
Thread Tools

using member data as default argument

 
 
hall
Guest
Posts: n/a
 
      12-10-2003
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 - )

 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      12-10-2003
On Wed, 10 Dec 2003 14:05:39 +0100, hall <(E-Mail Removed)>
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class data member or member function argument? Dwight Army of Champions C++ 4 02-22-2010 01:29 AM
Why doesn't the default argument allow const member? Divick C++ 29 09-30-2009 09:56 PM
Class and member templates, Default template argument, and Specialization danilo.horta@gmail.com C++ 3 03-11-2006 12:32 AM
Template member function default argument Mike Alexeev C++ 6 05-22-2004 11:50 PM
design question - member function argument or pointer member Gert Van den Eynde C++ 8 01-26-2004 05:20 PM



Advertisments