473,795 Members | 3,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

direct vs copy initialization error

Consider the following program:

#include <iostream>
#include <string>

using namespace std;

class Test
{
public:
Test();
Test(const Test &rhs);
Test(const std::string &str);
private:
int x;
};

Test::Test()
{
x = 0;
cout << "default ctor called x = " << x << endl;
}

Test::Test(cons t Test &rhs)
{
x = rhs.x;
cout << "copy ctor called. x = " << x << endl;
}
Test::Test(cons t std::string &str)
{
cout << "Test(const std::string &str) - str = " << str << endl;
}

int main()
{
Test str_obj = "test";

return 0;
}

This program produces the following compilation error for the
statement
Test str_obj = "test";
in g++ under Linux.

error: conversion from `const char[5]' to non-scalar type `Test'
requested

But it compiles well in VC++ 2005 express edition produces the
following output:

Test(const std::string &str) - str = test string

Why is this difference ? I do not understand as to why it doesn't
compile under one implementation( ie g++) but produces output under
another(ie VC++). What does the Standard say in this case ?

But if I used
Test str_obj("direct initialization" );
this statement is compiled without any error in g++ under Linux and
produces the expected output.

Kindly explain.

Thanks
V.Subramanian

Jul 3 '07 #1
3 1716
On Jul 2, 10:38 pm, "subramanian10. ..@yahoo.com, India"
<subramanian10. ..@yahoo.comwro te:
Consider the following program:

#include <iostream>
#include <string>

using namespace std;

class Test
{
public:
Test();
Test(const Test &rhs);
Test(const std::string &str);
private:
int x;

};

Test::Test()
{
x = 0;
cout << "default ctor called x = " << x << endl;

}

Test::Test(cons t Test &rhs)
{
x = rhs.x;
cout << "copy ctor called. x = " << x << endl;}

Test::Test(cons t std::string &str)
{
cout << "Test(const std::string &str) - str = " << str << endl;

}

int main()
{
Test str_obj = "test";

return 0;

}

This program produces the following compilation error for the
statement
Test str_obj = "test";
in g++ under Linux.

error: conversion from `const char[5]' to non-scalar type `Test'
requested

But it compiles well in VC++ 2005 express edition produces the
following output:

Test(const std::string &str) - str = test string

Why is this difference ? I do not understand as to why it doesn't
compile under one implementation( ie g++) but produces output under
another(ie VC++). What does the Standard say in this case ?

But if I used
Test str_obj("direct initialization" );
this statement is compiled without any error in g++ under Linux and
produces the expected output.

Kindly explain.

Thanks
V.Subramanian
umm as far as I can see on. "test" is a const string, but cannot be
referenced since it has no name.
Maybe if you used

string test="test"

then in main set it to the test string it may work. As I understand
it,
Microsoft compilers are far from the following the stardard.

Jul 3 '07 #2
su************* *@yahoo.com, India wrote:
This program produces the following compilation error for the
statement
Test str_obj = "test";
in g++ under Linux.
>
error: conversion from `const char[5]' to non-scalar type `Test'
requested

But it compiles well in VC++ 2005 express edition produces the
following output:
You could try to change the compiler settings to disable language
extensions.

Regards,
Sumit.
--
Sumit Rajan <su*********@gm ail.com>
Jul 3 '07 #3
On Jul 3, 8:38 am, "subramanian10. ..@yahoo.com, India"
<subramanian10. ..@yahoo.comwro te:
Consider the following program:

#include <iostream>
#include <string>

using namespace std;

class Test
{
public:
Test();
Test(const Test &rhs);
Test(const std::string &str);
private:
int x;

};
[code chopped]
>
int main()
{
Test str_obj = "test";

return 0;

}

This program produces the following compilation error for the
statement
Test str_obj = "test";
in g++ under Linux.
Observe what is happening: when you say
Test str_obj = "Test", you are calling for "two user defined
conversions":
a) string literal "test" (of type const char[5]) to std::string
b) std::string to Test

C++ doesnot allow *two* implicit user defined conversions.

So whats the solution? make one of the conversions explicit:

either: Test str_obj = string("Test");
or: Test str_obj = Test("Test").

error: conversion from `const char[5]' to non-scalar type `Test'
requested

But it compiles well in VC++ 2005 express edition produces the
following output:

Test(const std::string &str) - str = test string

Why is this difference ? I do not understand as to why it doesn't
compile under one implementation( ie g++) but produces output under
another(ie VC++). What does the Standard say in this case ?

Giving a compilation error in this case is according to the standard.
>
But if I used
Test str_obj("direct initialization" );
this statement is compiled without any error in g++ under Linux and
produces the expected output.

The reason is that when you are saying

Test str_obj("direct initialization" );

Only one constructor call is implicit : const char[5] -std::string.
The second constructor call string->Test is explicit. Since standard
allows one standard-conversion to be done explicitly, this code
compiles properly.

-Neelesh
Kindly explain.

Thanks
V.Subramanian

Jul 3 '07 #4

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

Similar topics

11
580
by: Kurt Krueckeberg | last post by:
Given a class X class X { public: X(int); X(const X&); //. . . }; Is this line X x1(1); the same thing as X x2 = 2;
1
2118
by: iwdu15 | last post by:
does anyone knpo why this code throws a runtime error saying unable to do this managed code in this sense becayuse iot could hang ''Global Variable Dim song as Audio ''direct x dll already imported
5
2227
by: heng | last post by:
class A { public: int x; A(int x_=0):x(x_){} }; int main() { A obj1(99); //user-defined constructor is used
3
3400
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and demonstrates(?) that the copy constructor is never called (at least no sign of its chatter on std::cout is visible). So - is it necessary to have a public copy constructor in order to use "function style" initializers for an array of objects? ...
4
1438
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <string> using namespace std; class Test { public:
2
1837
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
2
2055
by: subramanian100in | last post by:
Suppose we have a class named Test. We have a function void fn(Test arg); When this function is called, what kind of initialization - direct initialization or copy initialization, happens to construct arg ? The reason for asking this question is the following:
9
2318
by: Anthony Williams | last post by:
Hi, Should the following compile, and what should it print? #include <memory> #include <iostream> void foo(std::auto_ptr<intx) { std::cout<<"copy"<<std::endl;
7
163
by: abhash | last post by:
I am bit puzzled at the following piece of code I tried: ---------------------------------------------------------------------------------- #include <iostream> using namespace std; class Test { public: Test() { cout<<"Cons\n";} Test(Test& a) { cout<<"Copy cons\n";}
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.