direct vs copy initialization error

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

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
 
A

atonalpanic

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.
 
S

Sumit Rajan

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.
 
N

Neelesh Bodas

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top