C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

constructor problem

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
campos
Guest





PostPosted: Fri Dec 29, 2006 9:26 am    Post subject: constructor problem Reply with quote



Here are my codes:

#include <iostream>

using namespace std;

class Test
{
public:
Test() { cout << "Test()" << endl; }
Test(const string& s) { str = s; cout<< "Test(string)" << endl; }
Test(const Test& t) { str = s; cout << "Test(const Test&)" << endl;
}
private:
string str;
}

int main()
{
string str("hello");
Test t1(str);
Test t2 = str;
return 0;
}

I use VC++ 7.0 to compile and run the program, the result is:
-----------------
Test(string)
Test(string)
-----------------

copy constructor has not been called.


But in "C++ Primer 4th Edition", Ch 13.1
-----------------------------------------------------------
Usually the difference between direct- or copy-initialization is at
most a matter of low-level optimization. However, for types that do not
support copying, or when using a constructor that is nonexplicit
(Section 12.4.4, p. 462) the distinction can be essential:

ifstream file1("filename"); // ok: direct initialization
ifstream file2 = "filename"; // error: copy constructor is private
// This initialization is okay only if
// the Sales_item(const string&) constructor is not explicit
Sales_item item = string("9-999-99999-9");
-----------------------------------------------------------

However, even if I declare the copy constructor of Test class as
private, still it can be compiled and executed correctly. That seems
the following sentences are treated as the same:
--------------------
Test t1(str);
Test t2 = str;
--------------------

Hope sb can help me.
Thanks in advance!
Back to top
Salt_Peter
Guest





PostPosted: Fri Dec 29, 2006 9:42 am    Post subject: Re: constructor problem Reply with quote



campos wrote:
Quote:
Here are my codes:

#include <iostream
#include <ostream

#include <string>

Quote:

using namespace std;

class Test
{
public:
Test() { cout << "Test()" << endl; }

always, always initialize all your members.

Test() : str() { std::cout << "default Test()" << std::endl; }

Quote:
Test(const string& s) { str = s; cout<< "Test(string)" << endl; }

The above should be:
Test(const std::string& s) : str(s) { std::cout<<
"Test(string)" << std::endl; }
Which by the way, is a conversion ctor.

Quote:
Test(const Test& t) { str = s; cout << "Test(const Test&)" << endl;
}
private:
string str;
}

; // semicolon required

Quote:

int main()
{
string str("hello");
Test t1(str);
Test t2 = str; // invoking conversion ctor
return 0;
}

I use VC++ 7.0 to compile and run the program, the result is:
-----------------
Test(string)
Test(string)
-----------------

copy constructor has not been called.

There was no copy.

Quote:


But in "C++ Primer 4th Edition", Ch 13.1
-----------------------------------------------------------
Usually the difference between direct- or copy-initialization is at
most a matter of low-level optimization. However, for types that do not
support copying, or when using a constructor that is nonexplicit
(Section 12.4.4, p. 462) the distinction can be essential:

ifstream file1("filename"); // ok: direct initialization
ifstream file2 = "filename"; // error: copy constructor is private
// This initialization is okay only if
// the Sales_item(const string&) constructor is not explicit
Sales_item item = string("9-999-99999-9");
-----------------------------------------------------------

However, even if I declare the copy constructor of Test class as
private, still it can be compiled and executed correctly. That seems
the following sentences are treated as the same:
--------------------
Test t1(str);
Test t2 = str;
--------------------

Try:
Test t2 = t1; // thats a copy since t1 and t2 are of the same type
t1 = t2; // thats now an assignment
Back to top
Venkatesh
Guest





PostPosted: Fri Dec 29, 2006 10:10 am    Post subject: Re: constructor problem Reply with quote



Ctors are implicit by default..which means this statement
Test t2 = str; is no different from Test t2 (str);

Make the 2nd ctor as explicit
explicit Test(const string& s))
if you want to avoid an assignment to call a non copy ctor.

-
Venkatesh
John Carson wrote:
Quote:
"campos" <huwenyan (AT) gmail (DOT) com> wrote in message
news:1167362791.419607.288180 (AT) k21g2000cwa (DOT) googlegroups.com
Here are my codes:

#include <iostream

using namespace std;

class Test
{
public:
Test() { cout << "Test()" << endl; }
Test(const string& s) { str = s; cout<< "Test(string)" << endl; }
Test(const Test& t) { str = s; cout << "Test(const Test&)" << endl;
}
private:
string str;
}

int main()
{
string str("hello");
Test t1(str);
Test t2 = str;
return 0;
}

I use VC++ 7.0 to compile and run the program, the result is:
-----------------
Test(string)
Test(string)
-----------------

copy constructor has not been called.

This is permissible as an optimization. However, according to the Standard,
the copy constructor is required to be accessible even if not used in this
scenario. Comeau online refuses to compile the code when the copy
constructor is private. VC++ 2005 also refuses to compile it if language
extensions is disabled (/Za switch).


--
John Carson
Back to top
John Carson
Guest





PostPosted: Fri Dec 29, 2006 10:10 am    Post subject: Re: constructor problem Reply with quote

"campos" <huwenyan (AT) gmail (DOT) com> wrote in message
news:1167362791.419607.288180 (AT) k21g2000cwa (DOT) googlegroups.com
Quote:
Here are my codes:

#include <iostream

using namespace std;

class Test
{
public:
Test() { cout << "Test()" << endl; }
Test(const string& s) { str = s; cout<< "Test(string)" << endl; }
Test(const Test& t) { str = s; cout << "Test(const Test&)" << endl;
}
private:
string str;
}

int main()
{
string str("hello");
Test t1(str);
Test t2 = str;
return 0;
}

I use VC++ 7.0 to compile and run the program, the result is:
-----------------
Test(string)
Test(string)
-----------------

copy constructor has not been called.

This is permissible as an optimization. However, according to the Standard,
the copy constructor is required to be accessible even if not used in this
scenario. Comeau online refuses to compile the code when the copy
constructor is private. VC++ 2005 also refuses to compile it if language
extensions is disabled (/Za switch).


--
John Carson
Back to top
Squeamizh
Guest





PostPosted: Fri Dec 29, 2006 10:10 am    Post subject: Re: constructor problem Reply with quote

Salt_Peter wrote:
Quote:
campos wrote:
class Test
{
public:
Test() { cout << "Test()" << endl; }

always, always initialize all your members.

Test() : str() { std::cout << "default Test()" << std::endl; }

Why? You're just default constructing the std::string, anyway.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.