Go Back   Rhinocerus > Newsgroup > Newsgroup comp.language.c++ > Newsgroup comp.language.c++.moderated

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 11-05-2005, 11:32 AM
meigel
Guest
 
Posts: n/a
Default temporary object creation

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! ]

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 11-06-2005, 11:08 AM
Thomas Maeder
Guest
 
Posts: n/a
Default Re: temporary object creation

"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! ]

Reply With Quote
  #3 (permalink)  
Old 11-06-2005, 11:11 AM
Valentin Samko
Guest
 
Posts: n/a
Default Re: temporary object creation

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! ]

Reply With Quote
  #4 (permalink)  
Old 11-06-2005, 11:19 AM
ben
Guest
 
Posts: n/a
Default Re: temporary object creation

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! ]

Reply With Quote
 
Reply

Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off


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



All times are GMT. The time now is 12:33 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.