Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old September 24th, 2006, 07:15 PM
Thomas Matthews
Guest
 
Posts: n/a
Default Variable Initialization: MSVC vs. world

Hi,

When assigning variables at initialization, is the following
allowed:
class Field;

int main(void)
{
Field * p_field(NULL);
return EXIT_SUCCESS;
}

I've switch to using MS Visual C++ 6.0 and it is giving me
errors for the assignment. However, Borland 5.2 and GNU
g++ 3.4.4 do not give any errors.

My belief is that the above is correct syntax.

Unfortunately, I'm have to change all of these and
similar instances to:
Field * p_field = NULL;
to get the code to compiler under MSVC++ 6.0

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
  #2  
Old September 24th, 2006, 07:25 PM
Phlip
Guest
 
Posts: n/a
Default Re: Variable Initialization: MSVC vs. world

Thomas Matthews wrote:
Quote:
Field * p_field(NULL);
Quote:
I've switch to using MS Visual C++ 60 and it is giving
me errors for the assignment.
VC++ >6 permits that correct notation.

I suspect there are those who will promote constructor-notation for all
initializations. However, I can't think of a rationale besides "it's just
obviously better!"

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


  #3  
Old September 24th, 2006, 07:35 PM
Jerry Coffin
Guest
 
Posts: n/a
Default Re: Variable Initialization: MSVC vs. world

In article <YcARg.7723$Ij.2977@newssvr14.news.prodigy.com>,
phlipcpp@yahoo.com says...

[ ... ]
Quote:
I suspect there are those who will promote constructor-notation for all
initializations. However, I can't think of a rationale besides "it's just
obviously better!"
The obvious reasoning here is that constructor notation instructs the
compiler to construct the object with the value directly. Notation like:

whatever x=something;

really instructs the compiler to create a temporary object initialized
with 'something', then use whatever's copy constructor to create x, then
destroy the temporary. For built-in types, this is rarely a problem, but
for other types that take longer to create, copy and/or destroy, this is
substantially slower. Likewise, for extremely large objects, this wastes
memory creating two objects where only one is really needed.

--
Later,
Jerry.

The universe is a figment of its own imagination.
  #4  
Old September 24th, 2006, 08:15 PM
loufoque
Guest
 
Posts: n/a
Default Re: Variable Initialization: MSVC vs. world

Phlip wrote :
Quote:
I suspect there are those who will promote constructor-notation for all
initializations. However, I can't think of a rationale besides "it's just
obviously better!"
The constructor notation can cause problems related to function declaration.

  #5  
Old September 24th, 2006, 09:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Variable Initialization: MSVC vs. world

Jerry Coffin wrote:
Quote:
In article <YcARg.7723$Ij.2977@newssvr14.news.prodigy.com>,
phlipcpp@yahoo.com says...
>
[ ... ]
>
Quote:
>I suspect there are those who will promote constructor-notation for
>all initializations. However, I can't think of a rationale besides
>"it's just obviously better!"
>
The obvious reasoning here is that constructor notation instructs the
compiler to construct the object with the value directly. Notation
like:
>
whatever x=something;
>
really instructs the compiler to create a temporary object initialized
with 'something', then use whatever's copy constructor to create x,
then destroy the temporary. For built-in types, this is rarely a
problem, but for other types that take longer to create, copy and/or
destroy, this is substantially slower. Likewise, for extremely large
objects, this wastes memory creating two objects where only one is
really needed.
The reality is a bit better but somewhat convoluted, I am afraid.
The compiler is explicitly allowed to forgo creation of a temporary
in such case, even if the copy c-tor has side effects (see 12.8/15).
However, the copy c-tor has to be accessible *as if* copying does take
place.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #6  
Old September 24th, 2006, 10:35 PM
loufoque
Guest
 
Posts: n/a
Default Re: Variable Initialization: MSVC vs. world

Thomas Matthews wrote :
Quote:
I've switch to using MS Visual C++ 6.0
Then switch again to a C++ compiler.
  #7  
Old September 24th, 2006, 10:55 PM
Bart
Guest
 
Posts: n/a
Default Re: Variable Initialization: MSVC vs. world

Thomas Matthews wrote:
Quote:
Hi,
>
When assigning variables at initialization, is the following
allowed:
class Field;
>
int main(void)
{
Field * p_field(NULL);
return EXIT_SUCCESS;
}
>
I've switch to using MS Visual C++ 6.0 and it is giving me
errors for the assignment.
Well, that's what you get for using an 8-year-old compiler. If you're
going to use MSVC at least use the most recent version (which is a lot
more standard-compliant BTW).

Regards,
Bart.

  #8  
Old September 25th, 2006, 02:55 AM
Jeff Faust
Guest
 
Posts: n/a
Default Re: Variable Initialization: MSVC vs. world


loufoque wrote:
Quote:
Thomas Matthews wrote :
>
Quote:
I've switch to using MS Visual C++ 6.0
>
Then switch again to a C++ compiler.
It compiles on VC++ 8, which is much much closer to being a C++
compiler.

Jeff

 

Bookmarks

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

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

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.