473,402 Members | 2,050 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,402 software developers and data experts.

Initialization: = or () ?

I can write

int x(5);

or

int x = 5;

Are both the same ? Is either form preferable for some reason I don't
see ?
Jul 22 '05 #1
11 1250
Mats Weber wrote:
I can write

int x(5);

or

int x = 5;

Are both the same ?
Yes, they are the same.

Is either form preferable for some reason I don't see ?


No really, but you're forced to use the T() syntax for initialization so
being consistent and using :

int x(5);

.... probably has a slight readability advantage.
Jul 22 '05 #2
Mats Weber posted:
I can write

int x(5);

or

int x = 5;

Are both the same ? Is either form preferable for some reason I don't
see ?

Would I be right in saying the following:
When you have a class that has a constructor, copy constructor, all sorts of
operators, and you do the following:

SuperClass super_class(57U);
You're calling the

SuperClass(unsigned int);

constructor.

But...

when you write:
SuperClass super_class = 57U;
Are you not then calling the:

SuperClass(void);

constructor, and then calling

operator=(int)

??
-JKop
Jul 22 '05 #3
JKop wrote:
Mats Weber posted:
I can write

int x(5);

or

int x = 5;

Are both the same ? Is either form preferable for some reason I don't
see ?

Would I be right in saying the following:
When you have a class that has a constructor, copy constructor, all
sorts of operators, and you do the following:

SuperClass super_class(57U);
You're calling the

SuperClass(unsigned int);

constructor.

But...

when you write:
SuperClass super_class = 57U;
Are you not then calling the:

SuperClass(void);

constructor, and then calling

operator=(int)

??


Nope. You're calling the SuperClass(unsigned int) constructor, followed
by SuperClass's copy constructor. However, a decent compiler will
optimize the copy away, effectively doing the same as in the first code
example. But there is still a difference: Even if the compiler doesn't
use the copy constructor, it still requires a copy constructor to be
existing and accessible.

Jul 22 '05 #4

"Mats Weber" <ma***@bluewin.ch> schreef in bericht
news:ma*************************@sicinfo.epfl.ch.. .
I can write

int x(5);

or

int x = 5;

Are both the same ? Is either form preferable for some reason I don't
see ?


You use x(5) also when initialising class members.
You need to use x(5) when x is as const class member because you can't set
it with a = operator.
Jul 22 '05 #5
Wouter de Kort wrote:
You use x(5) also when initialising class members.
You need to use x(5) when x is as const class member because you can't
set it with a = operator.


"MyClass x = 3;" does _not_ use operator=!

Jul 22 '05 #6

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:40**************@mariani.ws...
Mats Weber wrote:
I can write

int x(5);

or

int x = 5;

Are both the same ?


Yes, they are the same.


Not if int is replace by T*.

T* ptr = aTptr; // ok

T* prt( aTPtr ); // error

I'm not sure but I think the latter is being added to the standard as valid.

Jeff Flinn
Jul 22 '05 #7
On 17/6/04 6:53 pm, in article ca**********@bluegill.adi.com, "Jeff Flinn"
<NO****@nowhere.com> wrote:

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:40**************@mariani.ws...
Mats Weber wrote:
I can write

int x(5);

or

int x = 5;

Are both the same ?


Yes, they are the same.


Not if int is replace by T*.

T* ptr = aTptr; // ok

T* prt( aTPtr ); // error

I'm not sure but I think the latter is being added to the standard as valid.


It's accepted by Metrowerks Codewarrior 9.2, but I don't have a copy of the
standard to hand.

I always thought it was valid?
Steve.

Jul 22 '05 #8

"Jeff Flinn" <NO****@nowhere.com> skrev i en meddelelse
news:ca**********@bluegill.adi.com...

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:40**************@mariani.ws...
Mats Weber wrote:
I can write

int x(5);

or

int x = 5;

Are both the same ?
Yes, they are the same.


Not if int is replace by T*.


This is rubbish - it is.
T* ptr = aTptr; // ok

T* prt( aTPtr ); // error No - not an error, just plain initialization.

I'm not sure but I think the latter is being added to the standard as valid.
Jeff Flinn

/Peter
Jul 22 '05 #9

"Mats Weber" <ma***@bluewin.ch> skrev i en meddelelse
news:ma*************************@sicinfo.epfl.ch.. .
I can write

int x(5);

or

int x = 5;

Are both the same ? Is either form preferable for some reason I don't
see ?


The first style is the best. You can use it for all types. The second style
requires the class to have a copy constructor.
Why I often use the second style? Visual C++ 6.0 does not always understand
the "correct" form.

/Peter
Jul 22 '05 #10
Mats Weber wrote:
I can write

int x(5);

or

int x = 5;

Are both the same ?
For type 'int' - yes. In general case - no. It depends on actual types
involved in initialization.

For example, if the initializer and the object being initialized have
different types (the former - T, the latter - U), then the compiler will
make an attempt to convert the initializer from type T to type U. If
this process involves a conversion construction of the initializer,
which happens to be declared as 'explicit', the second form will fail,
while the first form will still compile:

std::auto_ptr<int> p1 = new int; // ERROR
std::auto_ptr<int> p2(new int); // OK

For another example, if T is different from U and if the conversion can
only be performed by using some intermediate type X in the following
two-step manner

1. convert T to X using T's conversion operator
2. convert X to U using U's conversion constructor

then the second form will fail, while the first form will still compile:

struct T { operator void*() const; };
struct U { U(void*); };

T t;
U u1 = t; // ERROR
U u2(t); // OK
Is either form preferable for some reason I don't
see ?


In situations when you actually have a choice use whatever looks better
to your eyes.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #11
JKop wrote:
[snip] But...

when you write:

SuperClass super_class = 57U;

Are you not then calling the:

SuperClass(void);

constructor, and then calling

operator=(int)


No. The above constructs a new object super_class. For
construction there is always a constructor used, never
an operator=.

constructor: create new object and initialize it
operator= : take already existing object and give it a new value
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #12

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,...
6
by: Alexander Stippler | last post by:
Hi, I wonder about the behaviour of como and icc on some very simple program. I thought initializing members of classes, which are of class type, would be 'direct initialized' (as the standard...
1
by: Jacek Dziedzic | last post by:
Hi! A) Why isn't it possible to set a member of the BASE class in an initialization list of a DERIVED class constructor (except for 'calling' the base constructor from there, of course)? I even...
10
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
8
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.