![]() |
|
|
|||||||
![]() |
C++ - equivalent of named arguments for constructors |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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: explicitly defined and _b uses default? Since C++ is agnostic to constructor chaining, DType: { 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 |
|
|
|
|
#2 |
|
Posts: n/a
|
On Nov 3, 12:11*am, Andrey Vul <andrey....@gmail.com> wrote:
Edit: modify > DType: to DType: The default arguments in the reduced constructor make constructor choice ambiguous and thus illegal. Andrey Vul |
|
|
|
#3 |
|
Posts: n/a
|
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: > explicitly defined and _b uses default? Since C++ is agnostic to > constructor chaining, DType: > { 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: 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: // ... 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 |
|
|
|
#4 |
|
Posts: n/a
|
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: > > explicitly defined and _b uses default? Since C++ is agnostic to > > constructor chaining, DType: > > { 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: > 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: > * *// ... > * *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 |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |