Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have simplified the class for the sake of question.

Please I need someone to explain the following:
when the compiler see
Mystring x{"ABC"};
it will directly trigger the class constructor, but how the compiler also trigger the class constructor after the equal sign
Mystring test="Hi there";



#include<iostream>
#include<cstring>

class Mystring
{
private:
    char * str;
    
public:
    
    Mystring(char * s)
    :str{nullptr}
    {
        str = new char[std::strlen(s)+1];
        std::strcpy(str,s);
        std::cout<<"constructor is called for: "<<this->str<<std::endl;
    }   
    


//overaloaded assignment operator
Mystring& operator=(const Mystring & rhs) 
    {
std::cout<<"copy assignment"<<std::endl;

      if(this == &rhs)
             return *this;   
             delete [ ] str;     
             str = new char[std::strlen(rhs.str)+1];
             std::strcpy(this->str,rhs.str);
       
        return *this;
    }
};

int main()
{
    Mystring x{"ABC"};

    Mystring y="Old value";
    
     y="new value";
    
    return 0;
}
C++



What I have tried:

It works ok but I need to know how the compiler triggers the class constructor when there is no Mystring class name before the initialization value of "Hi there"
Posted
Updated 3-Nov-22 23:40pm
v5

 
Share this answer
 
Comments
Andrew12500 4-Nov-22 5:25am    
Thanks

CPallini

the link you gave is more than enough. Thank you it worked.
CPallini 4-Nov-22 8:13am    
You are welcome.
In both cases you are declaring an instance of the Mystring class. And since the only data being referenced matches the contructor's parameter, then it will call the constructor with that text.
 
Share this answer
 
Comments
Andrew12500 4-Nov-22 5:44am    
the first line:
Mystring y="Old value"; //this is understood

now in the second assignment:

y="new value"; // this will trigger the copy assignment operator
my question when I pass "new value" as an argument to (const Mystring & rhs) how this part (const Mystring & rhs) will trigger the class constructor again ? I am confused because "new value" is a c-style string while (const Mystring & rhs) is expecting to receive an instance of class, could you please clarify.
Richard MacCutchan 4-Nov-22 6:05am    
The operator= override is defined to take a reference to a Mystring object. So the compiler needs to construct that object from the string, before the copy operation can be actioned.

"new value" is a c-style string
Actually it is a character constant, and is the same in C++.
Richard MacCutchan 4-Nov-22 6:46am    
I explained why in my previous message.
Andrew12500 4-Nov-22 10:11am    
on more question Richard, please to help
here:
y="new value";//the "new value" part will make the compiler to create a temporary r-value instance of the class and this temporary r-value instance will be passed to the overloaded assignment operator(const Mystring & rhs) which accepts l-value, how this happen ?
Richard MacCutchan 4-Nov-22 10:20am    
Well it's actually more complicated than that: Value Categories: Lvalues and Rvalues (C++) | Microsoft Learn[^].

But either way the temporary object is passed to the function, so it can access the object's str property.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900