| | |
A doubt in Type Conversion Program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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 :
Now my question is in the following expression
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
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
I have one doubt when i read a book to understand type conversion concepts .
Here's the listing used in that book to demonstrate :
C++ Syntax (Toggle Plain Text)
// 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 }
C++ Syntax (Toggle Plain Text)
s1 = xstr;
Will the constructor be invoked after an object is created ?(here 's1')
If it is like this means
C++ Syntax (Toggle Plain Text)
String s1 = xstr;
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
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.
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.
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
will invoke the following one argument constructor
is it true ?
if so can the constructor be called after an object is created ?
The explanation of the program also insist that the following expression
c++ Syntax (Toggle Plain Text)
s1 = xstr;
c++ Syntax (Toggle Plain Text)
String( char s[] ) //1-arg constructor { strcpy(str, s); } // convert C-string to String
is it true ?
if so can the constructor be called after an object is created ?
•
•
•
•
Dragon , thanks for your reply but the program doesn't contain any definition for operator = function
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? ![]() |
Similar Threads
- type conversion error (C++)
- currency conversion program error (C++)
- Type Conversion of User Input: (C++)
- Need a short program (Windows NT / 2000 / XP)
- type conversion (C)
- Need help with this conversion program (C++)
Other Threads in the C++ Forum
- Previous Thread: classes
- Next Thread: Linked List
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






