A doubt in Type Conversion Program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

A doubt in Type Conversion Program

 
0
  #1
Jul 11th, 2007
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 :

  1. // strconv.cpp
  2. // convert between ordinary strings and class String
  3. #include <iostream>
  4. using namespace std;
  5. #include <string.h> //for strcpy(), etc.
  6. ////////////////////////////////////////////////////////////////
  7. class String //user-defined string type
  8. {
  9. private:
  10. enum { SZ = 80 }; //size of all String objects
  11. char str[SZ]; //holds a C-string
  12. public:
  13. String() //no-arg constructor
  14. { str[0] = '\0'; }
  15. String( char s[] ) //1-arg constructor
  16. { strcpy(str, s); } // convert C-string to String
  17. void display() const //display the String
  18. { cout << str; }
  19. operator char*() //conversion operator
  20. { return str; } //convert String to C-string
  21. };
  22. ////////////////////////////////////////////////////////////////
  23. int main()
  24. {
  25. String s1; //use no-arg constructor
  26. //create and initialize C-string
  27. char xstr[] = "Joyeux Noel! ";
  28.  
  29. [B]s1 = xstr; //use 1-arg constructor
  30. // to convert C-string to String[/B]
  31. s1.display(); //display String
  32.  
  33. String s2 = "Bonne Annee!"; //uses 1-arg constructor
  34. //to initialize String
  35. cout << static_cast<char*>(s2); //use conversion operator
  36. cout << endl; //to convert String to C-string
  37. return 0; //before sending to << op
  38. }
Now my question is in the following expression
  1. 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
  1. 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
Last edited by Ancient Dragon; Jul 11th, 2007 at 1:34 pm. Reason: add line numbers to code tag
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: A doubt in Type Conversion Program

 
0
  #2
Jul 11th, 2007
The default class constructor is always called then the c++ class object is declared (except as noted below), for example on line 25 of the code you posted. On line 29 the class's operator= method is called, not the class constructor.

The second example you posted works the same way except its all on the same program line. Hint: whenever you see '=' operator you can be sure the class's operator= method gets called. But if you code it like below then only a constructor is called.
String s1(xstr);
Last edited by Ancient Dragon; Jul 11th, 2007 at 1:52 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Re: A doubt in Type Conversion Program

 
0
  #3
Jul 12th, 2007
Dragon , thanks for your reply but the program doesn't contain any definition for operator = function , moreover the comment line insist that it uses one argument constructor for conversion .

The explanation of the program also insist that the following expression
  1. s1 = xstr;
will invoke the following one argument constructor
  1. String( char s[] ) //1-arg constructor
  2. { strcpy(str, s); } // convert C-string to String


is it true ?
if so can the constructor be called after an object is created ?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 488
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: A doubt in Type Conversion Program

 
0
  #4
Jul 12th, 2007
Originally Posted by parthiban View Post
Dragon , thanks for your reply but the program doesn't contain any definition for operator = function
I believe Ancient_Dragon is aware that you haven't explicitly defined operator=() in your class, which is why he mentioned it.

Look at the line in your code which he pointed out - you are calling operator=() but have no definition for it - therefore your class is using its default operator=()

The method of initialisation which occurs when using operator=() is not the same as when using explicit object construction - a temporary copy of the object is created first before your object is initialised by way of the assignment operator.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC