Hi all ,
I have one doubt when i read a book to understand type conversion concepts .
Here's the listing used in that book to demonstrate :
// strconv.cpp
// convert between ordinary strings and class String
#include <iostream>
using namespace std;
#include <string.h> //for strcpy(), etc.
////////////////////////////////////////////////////////////////
class String //user-defined string type
{
private:
enum { SZ = 80 }; //size of all String objects
char str[SZ]; //holds a C-string
public:
String() //no-arg constructor
{ str[0] = '\0'; }
String( char s[] ) //1-arg constructor
{ strcpy(str, s); } // convert C-string to String
void display() const //display the String
{ cout << str; }
operator char*() //conversion operator
{ return str; } //convert String to C-string
};
////////////////////////////////////////////////////////////////
int main()
{
String s1; //use no-arg constructor
//create and initialize C-string
char xstr[] = "Joyeux Noel! ";
[B]s1 = xstr; //use 1-arg constructor
// to convert C-string to String[/B]
s1.display(); //display String
String s2 = "Bonne Annee!"; //uses 1-arg constructor
//to initialize String
cout << static_cast<char*>(s2); //use conversion operator
cout << endl; //to convert String to C-string
return 0; //before sending to << op
}
Now my question is in the following expression
s1 = xstr;
It's explained that one argument constructor will be invoked to perform that conversion .
Will the constructor be invoked after an object is created ?(here 's1')
If it is like this means
String s1 = xstr;
It's clear for me that constructor will be invoked in this case and conversion will takes place but how come the earlier one ?
Please clarify me actually what is happening ?
Thanks in advance