-4

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 - this is understood- but how the compiler also trigger the class constructor after the equal sign , I mean how the compiler knows that it needs to constructor an instance of the class with initial value of "Hi there"

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;
    }   
    
};

int main()
{
    Mystring x{"ABC"};
    Mystring test="Hi there";
    
    return 0;
}

`

It works ok and the compiler were able to create an instance of the class with the initializer of "Hi there" 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"

wovano
  • 4,543
  • 5
  • 22
  • 49
ANDREW
  • 21
  • 4
  • 1
    it doesnt "construct a class". constructors construct instances – 463035818_is_not_an_ai Nov 03 '22 at 12:32
  • Don't try to approach initialisation in C++ by trying to guess how it works, extrapolation from other bits you know. Initialization in C++ is one of the most complicated topics in C++. `Mystring test = "Hi there"` is copy initialisation. Thats just how it is defined. https://en.cppreference.com/w/cpp/language/initialization – 463035818_is_not_an_ai Nov 03 '22 at 12:36
  • The compiler simply analyzes the code and sees either ` { };` or ` = ;` and then figures out what actually needs to happen. The details aren't really important, unless you're implementing a compiler, code analyzer or similar. – fabian Nov 03 '22 at 12:41
  • There are rules in the language/standard which essentially cause `Mystring test="Hi there"` to be a constructor call – Peter Nov 03 '22 at 13:13
  • In C++, some symbols and keywords are reused to mean different things in different contexts. When you use an equal sign in a variable declaration, it is part of an initialization, not an assignment. The case of `Mystring test="Hi there";` is [copy initialization](https://en.cppreference.com/w/cpp/language/copy_initialization) and is just another way of initializing an new variable like `Mystring x{"ABC"};` – François Andrieux Nov 03 '22 at 13:18
  • If the question is answered, please do not update the question with a new/follow-up question. Instead first search for an answer to your new question, and if you didn't found it, ask a new question here instead of changing this one. – wovano Nov 08 '22 at 11:06

1 Answers1

-1

You might want to overload the assignment operator to get the job done, that is because you can't assign any string to an object. Read more about it here: https://www.learncpp.com/cpp-tutorial/overloading-the-assignment-operator/

A possible solution might be:

Mystring& operator=(const char* s) {
        str = new char[std::strlen(s)+1];
        std::strcpy(str,s);
        std::cout<<"constructor is called for: "<<this->str<<std::endl;
        return *this;
    }
  • But in `Mystring test="Hi there";` there is no assignment, just initailization. The use of `=` is a leftover from C. – BoP Nov 03 '22 at 12:50
  • Okay, I'm not familiar with C, thought the person wanted to overload some sort. I see what you mean. – Iwan de Jong Nov 03 '22 at 12:52
  • I was trying to get the first answer to reach to the next question of: test="new value"; //this will trigger the overloaded assignment operator, the "new value" argument will be passed into (const Mystring &rhs) , the question how this part (const Mystring &rhs) will trigger the constructor ? – ANDREW Nov 04 '22 at 09:49
  • You might want to look up the difference between "new value" being string and what you want to achieve with "new value" being an object. Remember that (const Mystring &rhs) is an object and not a string. I heard there is something regarding C, but as far as C++ goes, this is what I know. – Iwan de Jong Nov 04 '22 at 10:29