473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Sep 24 '06 #1
7 2065
Thomas Matthews wrote:
Field * p_field(NULL);
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!!!
Sep 24 '06 #2
In article <Yc****************@newssvr14.news.prodigy.com>,
ph******@yahoo.com says...

[ ... ]
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.
Sep 24 '06 #3
Phlip wrote :
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.

Sep 24 '06 #4
Jerry Coffin wrote:
In article <Yc****************@newssvr14.news.prodigy.com>,
ph******@yahoo.com says...

[ ... ]
>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
Sep 24 '06 #5
Thomas Matthews wrote :
I've switch to using MS Visual C++ 6.0
Then switch again to a C++ compiler.
Sep 24 '06 #6
Thomas Matthews wrote:
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.

Sep 24 '06 #7

loufoque wrote:
Thomas Matthews wrote :
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

Sep 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
4
by: Bret Pehrson | last post by:
I just stumbled across the following problem: //.h class Masses { static double mass1; static double mass2; static double mass3; };
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
37
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
148
by: onkar | last post by:
Given the following code & variable i . int main(int argc,char **argv){ int i; printf("%d\n",i); return 0; } here i is allocated from bss or stack ?
12
by: rjtucke | last post by:
Hi all- this is my first post here- just a quick question: #include <math.h> double foo = cosh(0.621); int main() { return 0; } fails with
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.