direct vs copy initialization error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    #1

    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

  • atonalpanic@gmail.com

    #2
    Re: direct vs copy initialization error

    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.

    Comment

    • Sumit Rajan

      #3
      Re: direct vs copy initialization error

      subramanian100i n@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 <sumit.rajan@gm ail.com>

      Comment

      • Neelesh Bodas

        #4
        Re: direct vs copy initialization error

        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

        Comment

        Working...