473,378 Members | 1,420 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,378 software developers and data experts.

Constructor vs assignment

Hello all,

What, if any, is the difference between

string s("toto");
string s = "toto";

In the first case, I am using the constructor:

basic_string(const value_type *ptr);

In the second case, I believe I first call another constructor:

basic_string();

and then operator= is executed...? Only I don't see any reference to
operator= in my documentation.

I am puzzled.

Nudge

Jul 19 '05 #1
2 4693

"Grumble" <in*****@kma.eu.org> wrote in message news:bp**********@news-rocq.inria.fr...
In the second case, I believe I first call another constructor:

basic_string();

and then operator= is executed...? Only I don't see any reference to
operator= in my documentation.


No, that isn't correct. The = in the declaration isn't assignment, it's denotes
initialization, specifically copy initialization. The other case is called direct
initialization. The right side of the = is converted
to a temporary of the type of the new object and then direct initalized (via the
copy constructor) to the created object In most cases the compiler
optimizes them to the same code (omitting the unnecessary temporary/copy).
Copy-initialization is left over from C, and is necessary for the brace style
aggregate initialization for which there is no direct-initialization syntax.
Jul 19 '05 #2
"Grumble" <in*****@kma.eu.org> wrote in message
news:bp**********@news-rocq.inria.fr...
| What, if any, is the difference between
|
| string s("toto");
| string s = "toto";
|
| In the first case, I am using the constructor:
|
| basic_string(const value_type *ptr);
|
| In the second case, I believe I first call another constructor:
|
| basic_string();
|
| and then operator= is executed...?
No, but this is a classic confusion.

What happens, in the second case, is that a temporary
may first be created to convert "toto" (the array of char)
into an std::string, and then the copy constructor will
be called, and the temporary destructed.
Many compilers will optimize-out the intermediate copy,
but the copy-constructor still has to be accessible.

By using the direct-initialization syntax, you
ensure that this intermediate copy will not occur.

See also, for some details and terminology:
groups.google.com/groups?selm=bod097%24iiv%241%40newshispeed.ch
hth -Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #3

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

Similar topics

4
by: away | last post by:
1. When a class defined with a member of pointer type, it's necessary to have a copy constructor and assignment operator. If don't pass objects of such class as value in a function and don't do...
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
14
by: Arne | last post by:
In C++ we have a copy constructor. What is the equivalent in .Net? Would that be a clone method?
3
by: Christian Christmann | last post by:
Hi, I've a class that contains a private member object that in turn is a reference: class A { public: A( classB & );
2
by: Henrik Goldman | last post by:
Hi, Lets say you have class A which holds all data types as private members. Class B then inherits from A and does *only* include a set of public functions which uses A's existing functions for...
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
6
by: Taras_96 | last post by:
Hi everyone, The FAQ at http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6 states that: "Consider the following constructor that initializes member object x_ using an initialization...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.