Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > equivalent of named arguments for constructors

Reply
Thread Tools

equivalent of named arguments for constructors

 
 
Andrey Vul
Guest
Posts: n/a
 
      11-03-2009
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?
 
Reply With Quote
 
 
 
 
Andrey Vul
Guest
Posts: n/a
 
      11-03-2009
On Nov 3, 12:11*am, Andrey Vul <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Saeed Amrollahi
Guest
Posts: n/a
 
      11-03-2009
On Nov 3, 8:11*am, Andrey Vul <(E-Mail Removed)> 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

 
Reply With Quote
 
Andrey Vul
Guest
Posts: n/a
 
      11-04-2009
On Nov 3, 2:53*am, Saeed Amrollahi <(E-Mail Removed)> wrote:
> On Nov 3, 8:11*am, Andrey Vul <(E-Mail Removed)> 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?
 
Reply With Quote
 
Andrey Vul
Guest
Posts: n/a
 
      11-04-2009
On Nov 3, 8:55*pm, Andrey Vul <(E-Mail Removed)> wrote:

> this-chain constructor?

should be
this-chain construction

sorry for the typo
 
Reply With Quote
 
Saeed Amrollahi
Guest
Posts: n/a
 
      11-04-2009
On Nov 4, 5:01*am, Andrey Vul <(E-Mail Removed)> wrote:
> On Nov 3, 8:55*pm, Andrey Vul <(E-Mail Removed)> 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

 
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
[ANN] rdp-arguments -- named arguments for 1.8 and 1.9! Roger Pack Ruby 2 01-04-2010 02:56 AM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments