CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2024
    Posts
    2

    Question Why in one case, copy Ctor is called, while in another case, another Ctor is called?

    This is the common code snippet for two cases:
    Code:
    #include <string>
    
    class A {
    public:
        //string Ctor
        A(const std::string& s = std::string()) : ps(new std::string(s)) { }
        //Copy Ctor
        A(const A& p) : ps(new std::string(*p.ps)), i(p.i) { }
        ~A() { delete ps; }
    private:
        std::string* ps;
        int i;
    };
    Below is case 1:
    Code:
    A v = "abc";
    I was told by someone that the compiler would do the following steps:
    1) Implicitly convert "abc" to a temporary std::string object.
    2) Implicitly convert the temporary std::string object to a temporary A object.
    3) Call the copy Ctor with the temporary A object to construct v.
    Because there are two implicitly conversions before the copy Ctor function, the compiler reports an error (cannot convert from 'const char [4]' to 'A').

    Below is case 2:
    Code:
    A v = std::string("abc");
    From the step by step debugging, I can figure out that the compiler would do the following steps:
    1) Explicitly convert "abc" to a temporary std::string object.
    2) Call the string Ctor with the temporary std::string object to construct v.

    My question is as follows: After step 1) of both cases, we got a temporary std::string object. I cannot see any differences at this point between two cases. But after that, two cases run in different execution paths as detailed in above steps. Why the difference after the same temporary std::string object is obtained? Thanks.

  2. #2
    Join Date
    May 2024
    Posts
    2

    Re: Why in one case, copy Ctor is called, while in another case, another Ctor is call

    Problem solved. Can you please let me know how to delete it or, if you are the mediator, delete it for me? Thanks,

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,429

    Re: Why in one case, copy Ctor is called, while in another case, another Ctor is call

    Quote Originally Posted by zzzhhh View Post
    Problem solved. Can you please let me know how to delete it or, if you are the mediator, delete it for me? Thanks,
    Please, share your solution.
    Then you could "Mark Thread resolved" (in the menu "Thread Tools")
    Victor Nijegorodov

  4. #4
    Join Date
    May 2024
    Posts
    2

    Re: Why in one case, copy Ctor is called, while in another case, another Ctor is call

    In C++, whether the copy constructor or another constructor is called depends on how the object is being created or initialized.

    Copy Constructor: This constructor is invoked when a new object is created as a copy of an existing object. For example:
    cpp
    Copy code
    MyClass obj1;
    MyClass obj2 = obj1; // Copy constructor called
    In this case, obj2 is being initialized with obj1, so the copy constructor of MyClass will be called to create a copy of obj1.

    Another Constructor: Any other constructor can be called explicitly or implicitly depending on how the object is being initialized or created. For example:
    cpp
    Copy code
    MyClass obj1(10); // Constructor with parameter called
    MyClass obj2 = MyClass(20); // Constructor with parameter called
    In these cases, constructors other than the copy constructor are being explicitly invoked to create new objects. The copy constructor is not used because we are not creating a copy of an existing object. FMWhatsApp

    So, in summary, the choice between the copy constructor and another constructor depends on whether you're creating a new object as a copy of an existing one or creating a new object using a different constructor.
    Last edited by alora2; June 12th, 2024 at 09:44 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured