473,466 Members | 1,296 Online
Bytes | Software Development & Data Engineering Community
Create 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(const Test &rhs)
{
x = rhs.x;
cout << "copy ctor called. x = " << x << endl;
}
Test::Test(const 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 1699
On Jul 2, 10:38 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
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(const Test &rhs)
{
x = rhs.x;
cout << "copy ctor called. x = " << x << endl;}

Test::Test(const 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*********@gmail.com>
Jul 3 '07 #3
On Jul 3, 8:38 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
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
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
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...
5
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
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...
4
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <string> using namespace std; class Test { public:
2
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
2
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...
9
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
by: abhash | last post by:
I am bit puzzled at the following piece of code I tried: ---------------------------------------------------------------------------------- #include <iostream> using namespace std; class Test...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
1
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
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...
0
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 ...

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.