Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - equivalent of named arguments for constructors

 
Thread Tools Search this Thread
Old 11-03-2009, 05:11 AM   #1
Default equivalent of named arguments for constructors


Given the following:
class AType;
class BType;
class CType;

AType *AType_init();
BType *BType_init();
CType *CType_init();
AType *a = AType_init();
BType *b = BType_init();
CType *c = CType_init();

class DType {
public: DType(AType& _a = *a, BType& _b = *b, CType& _c = *c) { }
};

Is it possible to call DType:Type() so that only _a and _c are
explicitly defined and _b uses default? Since C++ is agnostic to
constructor chaining, DType:Type(AType& _a = *a, CType& _c = *c)
{ DType(_a, *b, _c); } won't have the intended effect. Or will it?

Having commas causes syntax errors, but is there a way to call
constructors so that arguments L, M, and N (by argument index) are
explicitly defined without kludging up constructors as done in the
previous paragraph?


Andrey Vul
  Reply With Quote
Old 11-03-2009, 06:11 AM   #2
Andrey Vul
 
Posts: n/a
Default Re: equivalent of named arguments for constructors
On Nov 3, 12:11*am, Andrey Vul <andrey....@gmail.com> wrote:

Edit:
modify
> DType:Type(AType& _a = *a, CType& _c = *c)

to
DType:Type(AType& _a, CType& _c)

The default arguments in the reduced constructor make constructor
choice ambiguous and thus illegal.


Andrey Vul
  Reply With Quote
Old 11-03-2009, 07:53 AM   #3
Saeed Amrollahi
 
Posts: n/a
Default Re: equivalent of named arguments for constructors
On Nov 3, 8:11*am, Andrey Vul <andrey....@gmail.com> wrote:
> Given the following:
> class AType;
> class BType;
> class CType;
>
> AType *AType_init();
> BType *BType_init();
> CType *CType_init();
> AType *a = AType_init();
> BType *b = BType_init();
> CType *c = CType_init();
>
> class DType {
> public: DType(AType& _a = *a, BType& _b = *b, CType& _c = *c) { }
>
> };
>
> Is it possible to call DType:Type() so that only _a and _c are
> explicitly defined and _b uses default? Since C++ is agnostic to
> constructor chaining, DType:Type(AType& _a = *a, CType& _c = *c)
> { DType(_a, *b, _c); } *won't have the intended effect. Or will it?
>
> Having commas causes syntax errors, but is there a way to call
> constructors so that arguments L, M, and N (by argument index) are
> explicitly defined without kludging up constructors as done in the
> previous paragraph?



Hi Andrey

There are several related items here:
1. Default arguments may be provided for trailing arguments only. I
can't declare/define
a function like this:
DType:Type(AType& _a = *a, BType& _b, CType& _c = *c) { }
2. Because C++ uses arguments based on position rather than name, a
possible workaround
for your problem is to switch second and third parameter:
DType:Type(AType& _a = *a, CType& _c = *c, BType& _b = *b) { }
// ...
DType D(*AType_init(), *CType_init());
3. Again, because C++ uses arguments based on position rather than
name, A solution
is suggested by experts called "Named Parameters Idiom". It is very
useful for a class
with constructors and mostly default values for data members:
class DType {
AType a;
BType b;
CType c;
public:
DType() {}
DType& A() { a = *AType_init(); return *this; }
DType& B(BType& b_) { b = b_; return *this; }
DType& C() { c = *CType_init(); return *this; }
};

// use DType
DType d().A().C();
// use BType explicit
// ...

I recommend see the D&E by Bjarne Stroustrup under "Keyword Arguments"
and the following
link:
http://parashift.com/c++-faq-lite/ctors.html#faq-10.18
4. FYI, in C++0x (next revision of C++), we will have delegating
constructor, so we can call
one constructor in another one.

I hope it helps,
-- Saeed Amrollahi



Saeed Amrollahi
  Reply With Quote
Old 11-04-2009, 01:55 AM   #4
Andrey Vul
 
Posts: n/a
Default Re: equivalent of named arguments for constructors
On Nov 3, 2:53*am, Saeed Amrollahi <amrollahi.sa...@gmail.com> wrote:
> On Nov 3, 8:11*am, Andrey Vul <andrey....@gmail.com> wrote:
>
>
>
> > Given the following:
> > class AType;
> > class BType;
> > class CType;

>
> > AType *AType_init();
> > BType *BType_init();
> > CType *CType_init();
> > AType *a = AType_init();
> > BType *b = BType_init();
> > CType *c = CType_init();

>
> > class DType {
> > public: DType(AType& _a = *a, BType& _b = *b, CType& _c = *c) { }

>
> > };

>
> > Is it possible to call DType:Type() so that only _a and _c are
> > explicitly defined and _b uses default? Since C++ is agnostic to
> > constructor chaining, DType:Type(AType& _a = *a, CType& _c = *c)
> > { DType(_a, *b, _c); } *won't have the intended effect. Or will it?

>
> > Having commas causes syntax errors, but is there a way to call
> > constructors so that arguments L, M, and N (by argument index) are
> > explicitly defined without kludging up constructors as done in the
> > previous paragraph?

>
> Hi Andrey
>
> There are several related items here:
> 1. Default arguments may be provided for trailing arguments only. I
> can't declare/define
> a function like this:
> * *DType:Type(AType& _a = *a, BType& _b, CType& _c = *c) { }
> 2. Because C++ uses arguments based on position rather than name, a
> possible workaround
> for your problem is to switch second and third parameter:
> * *DType:Type(AType& _a = *a, CType& _c = *c, BType& _b = *b) { }
> * *// ...
> * *DType D(*AType_init(), *CType_init());
> 3. Again, because C++ uses arguments based on position rather than
> name, A solution
> is suggested by experts called "Named Parameters Idiom". It is very
> useful for a class
> with constructors and mostly default values for data members:
> * class DType {
> * * AType a;
> * * BType b;
> * * CType c;
> * *public:
> * * *DType() {}
> * * *DType& A() { a = *AType_init(); return *this; }
> * * *DType& B(BType& b_) { b = b_; return *this; }
> * * *DType& C() { c = *CType_init(); return *this; }
> * };
>
> * // use DType
> * DType d().A().C();
> * // use BType explicit
> * // ...


Would it be correct to refer to this idiom as a this-chain constructor?


Andrey Vul
  Reply With Quote
Old 11-04-2009, 02:01 AM   #5
Andrey Vul
 
Posts: n/a
Default Re: equivalent of named arguments for constructors
On Nov 3, 8:55*pm, Andrey Vul <andrey....@gmail.com> wrote:

> this-chain constructor?

should be
this-chain construction

sorry for the typo


Andrey Vul
  Reply With Quote
Old 11-04-2009, 06:43 AM   #6
Saeed Amrollahi
 
Posts: n/a
Default Re: equivalent of named arguments for constructors
On Nov 4, 5:01*am, Andrey Vul <andrey....@gmail.com> wrote:
> On Nov 3, 8:55*pm, Andrey Vul <andrey....@gmail.com> wrote:
>
> > this-chain constructor?

>
> should be
> this-chain construction
>
> sorry for the typo


Hi

The better/offical name is "Method Chaining". Please see the following
link:
http://www.parashift.com/c++-faq-lite/references.html#[8.4]
In "Named Parameter Idiom", we usually use method chaining technique.

Regards,
-- Saeed Amrollahi



Saeed Amrollahi
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
image (jpg,bmp,gif, etc.) convert to equivalent binary representation using vb.net? archieSupremo Software 0 09-06-2009 12:20 PM
small problem replying in Agent 4.2 GrandpaChuck Computer Support 26 02-10-2007 04:49 AM
Illegal operation carololine Computer Support 12 07-14-2006 01:27 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46