Re: Working with strings in c++



"Vladimir Grigoriev" <vlad.moscow@xxxxxxx> wrote in message
news:%23K7IUc5SHHA.412@xxxxxxxxxxxxxxxxxxxxxxx

"John Carson" <jcarson_n_o_sp_am_@xxxxxxxxxxxxxxx> wrote in message
news:%23YjI6R4SHHA.2212@xxxxxxxxxxxxxxxxxxxxxxx
Is not the two declaration above the same?
For example what is the difference between
int i = 10;
and
int i( 10 );

No difference when you are talking about built in types. When talking
about user-defined types, however, there is a difference.

const CString myStr = "val1;val2;val3";

is supposed to involve two steps:

1. The creation of a temporary CString object initialised by
"val1;val2;val3"

2. The use of a copy constructor to initialise myStr from the temporary
CString object.

By contrast,

const CString myStr("val1;val2;val3");

involves a single step: the calling of the CString constructor to
initialise myStr.


You are wrong! In both cases the single CString constructor with
parameters is called. Neither copy constructor is called. I think you can
test this youself with a simple test program.

Try reading the answer.

"as an optimisation, the compiler is allowed (but not obliged) to
replace the two steps involved in

const CString myStr = "val1;val2;val3";

with a single call to the myStr constructor."

That is exactly what VC++ does. Nevertheless, your code should allow the
compiler to do it the two-step way, or the compiler is allowed to refuse to
compile the code --- even if it would optimise away the two steps were those
two steps possible.

To illustrate Paul's point, try compiling this:

#include <cstring>
using namespace std;

class MyString
{
char *str;
public:
explicit MyString(const char* arg)
{
str = new char[strlen(arg)+1];
strcpy(str, arg);
}
MyString(const MyString& rhs)
{
delete[] str;
str = new char[strlen(rhs.str)+1];
strcpy(str, rhs.str);
}
};


int main()
{
MyString ms2 = "test";
return 0;
}

To illustrate my point, try compiling this using Comeau online

http://www.comeaucomputing.com/tryitout/

#include <cstring>
using namespace std;

class MyString
{
char *str;
public:
MyString(const char* arg)
{
str = new char[strlen(arg)+1];
strcpy(str, arg);
}
private:
MyString(const MyString& rhs)
{
delete[] str;
str = new char[strlen(rhs.str)+1];
strcpy(str, rhs.str);
}
};


int main()
{
MyString ms2 = "test";
return 0;
}

--
John Carson


.



Relevant Pages

  • Re: strange compiler message
    ... >> int main ... strcpy(blah, " blah blah "); ... When I use the wrong signature the compiler issues the following error: ... All of this because C++ introduces the concept of calling a constructor ...
    (comp.lang.cpp)
  • Re: Requesting sample code!
    ... int m_i1, m_i2; ... then a constructor is not made automatically by the compiler. ...
    (microsoft.public.vc.language)
  • Re: Effeciency
    ... As lame as his class is, the compiler ... > distinguish between my class and an int. ... the presence of the constructor from unsinged int means ... check with an actual integral type (via a typedef) for your release mode ...
    (comp.lang.cpp)
  • Re: operator function
    ... Well I guess you are talking about a copy constructor right? ... The compiler is talking about the result of the addition, ... an int. ... > operator returns a Setting object. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: explicit conversion
    ... >> of letting the compiler figure out that 'Type' should be constructed from ... >> the 'arg'. ... now replacing the constructor in Type2 with the cast in Type ...
    (comp.lang.cpp)