String Class Constructor/Method Arguments (Operator Overloading)

This is not really a problem I have but more of me trying to understand classes better.

I don't know what this question or topic should be called. I could not find the answer to it or if it was posted before (because I don't know what it would be called). All I know is it has something to do with the String Class Arguments and how they are passed.

The only way I know how to pass an argument to a function or class is like this:

SomeClass Class_Instance(Arg);

Class_Instance.Class_Method(Arg);

However, with the String class, you could do something like this:

String StringInstance = "Some String Text";

How does it do it with the "=" sign, not parentheses"()"?
How would I get something like this to work when building a new class?

Here is another way I found that I don't understand (I found this in some StackExchange code and I don't know what it does):

  String toString() {
    return this->name + " - " + (String)score;
  }

What is with the curly Braces now!?
How would you get that to work??
What does it pass through how do I begin to understand it?

If this question has been posted before or asked somewhere else before, please point me to it and I will gladly take this question down. I don't wish to post the same question it is was asked before already, I just don't know what it would be called to see if it was posted before.

Any help will be much appreciated,
Thank You very much.

I THINK that is another syntax to invoke the constructor (from WString.h):

String(const char *cstr = "");

C++ has several initialization syntaxes: (), =, {}. The differences between them seem rather esoteric to me. But, I'm sure there's a reason for them that perhaps someone may be able to explain.

For simple initializations, I've found that several forms will work. The compiler seems to get more picky as the initializations get more complex. I usually end up playing with different forms until the compiler is happy. Like I said, there's probably some subtle difference.

the base is "operator overloading" : you can define the functionality of operators for your class:. See https://en.cppreference.com/w/cpp/language/operators

and you can see this implementation in
https://github.com/arduino/Arduino/blob/ide-1.5.x/hardware/arduino/avr/cores/arduino/WString.cpp row 231ff

Side note
When you want to refer to a line number (or group of lines) from GitHub, just click on the line(s) number and a pop up menu will let you copy a direct link there.
In your example ➜ this will go to line 231

Or pasting the URL here alone at the start of a new line will show a few lines of the GitHub file around the selected line

That is exactly what I needed, Thank you very much. The term "Operator Overloading" is what I needed :grinning:

I do not believe that is an example of operator overloading.
Overloading of the operator=() function would look like:

String StringInstance;
void setup() {
	StringInstance = "Some String Text";
}

The structure @issac25e is asking about includes the "String" class name at the beginning. So, it must be an instantiation of a String object. It's given a value at the same time. So, that makes it an initialization.

Instantiation of an object requires invoking the class's constructor. These are the available String constructors:

	String(const char *cstr = "");
	String(const String &str);
	String(const __FlashStringHelper *str);
       #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
	String(String &&rval);
	String(StringSumHelper &&rval);
	#endif
	explicit String(char c);
	explicit String(unsigned char, unsigned char base=10);
	explicit String(int, unsigned char base=10);
	explicit String(unsigned int, unsigned char base=10);
	explicit String(long, unsigned char base=10);
	explicit String(unsigned long, unsigned char base=10);
	explicit String(float, unsigned char decimalPlaces=2);
	explicit String(double, unsigned char decimalPlaces=2);

So, my claim as stated in #2, is that this one is being invoked:

String(const char *cstr = "");

The only other possibility I can think of is that a String rvalue (i.e. temporary) is being constructed from the c-string literal using

String(const char *cstr = "");

Then the 'StringInstance' object gets move-constructed from that using

String(String &&rval);

"String StringInstance" creates an instance of the String class. "StringInstance = "Some String Text" invokes the = over-ride to initialize the instance. Functionally, it is precisely equivalent to:

int x = 1234;

Disagree. I maintain it all happens in one shot as a form of Initialization. See: https://en.cppreference.com/w/cpp/language/initialization

That page describes initialization of variables. We are not talking about variables here, but objects, which may contain numerous variables. This bout it - How could the compiler possibly know WHICH of those object member variables to intialize, OTHER than by using the defined = overload operator? Does it simply guess?

How did you possibly come to that conclusion? The majority of the text is about initializing instances of classes.
I'm going with Copy Initialization
image

So explain HOW it knows WHICH member variable to initallize, if NOT by using the = overload definition?

I agree with gfvalvo

In this case String s = "str"; is the same as String s("str");, it will call the constructor.

Then later s = "str"; will of course call the operator overload

Read the page on Copy Initialization that I linked.