|
|||
|
Hi,
could someone please explain what the compiler attempts to do with the following code? Why does it not create a temporary object instance of type B? Cheers, Martin. struct A{}; struct B{ B(A a){} }; int main(){ A a; B(a); // error // B((A)a); // compiles ok return 0; } Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1 Copyright 1988-2003 Comeau Computing. All rights reserved. MODE:strict errors C++ "ComeauTest.c", line 9: error: "a" has already been declared in the current scope B(a); // error ^ "ComeauTest.c", line 9: error: no default constructor exists for class "B" B(a); // error ^ "ComeauTest.c", line 8: warning: variable "a" was declared but never referenced A a; ^ "ComeauTest.c", line 9: warning: variable "a" was declared but never referenced B(a); // error ^ 2 errors detected in the compilation of "ComeauTest.c" [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|
||||
|
||||
|
|
|
|||
|
"meigel" <martin.eigel@gmail.com> writes:
> could someone please explain what the compiler attempts to do with > the following code? Why does it not create a temporary object > instance of type B? > > Cheers, Martin. > > > struct A{}; > > struct B{ > B(A a){} > }; > > int main(){ > A a; > B(a); // error Because this statement is equivalent to this: B a; [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
meigel wrote:
> Hi, > > could someone please explain what the compiler attempts to do with the > following code? Why does it not create a temporary object instance of > type B? > > struct A{}; > struct B{ > B(A a){} > }; > int main(){ > A a; > B(a); // error Because this is equivalent to B (a); Which defines a variable 'a' of type B. Also, you do not have a default constructor for type B, thus the second error. -- Valentin Samko - http://www.valentinsamko.com [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
meigel wrote:
[snipped] > int main(){ > A a; > B(a); // error > // B((A)a); // compiles ok > return 0; > } Because B(a); is just about the same as B a;, which tries to creat an object named a of type B. But a is already defined as of type A. Ben [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: Hash object (was Re: Dynamically created dataset using hash | Dorfman, Paul | Newsgroup comp.soft-sys.sas | 0 | 08-28-2006 06:40 AM |
| Hash object (was Re: Dynamically created dataset using hash object?) | Scott Bass | Newsgroup comp.soft-sys.sas | 1 | 08-27-2006 10:16 PM |
| Re: forward a method programmatically within a composite object | Workman, Rob | Newsgroup comp.soft-sys.sas | 0 | 05-22-2006 03:15 PM |
| Re: Frame object attribute references | Tim Muir | Newsgroup comp.soft-sys.sas | 0 | 07-28-2005 03:04 AM |
| Re: SUGGESTION: Allow temporary array element as | Jack Hamilton | Newsgroup comp.soft-sys.sas | 0 | 07-09-2005 05:17 PM |