| | |
Type casting
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Solved Threads: 0
In the following code I am giving an int value to class using type casting--
The members of this class are being initialised with zero but after assigning 1 to the object a1.. the value of b changes to a garbage value. Why does it do this way?
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; class A { int a; float b; public: A() { a=0; b=0; } A(int m) { a=m; cout<<"\n values are"<<a<<endl<<b; } }; void main() { A a1; a1=1; }
The members of this class are being initialised with zero but after assigning 1 to the object a1.. the value of b changes to a garbage value. Why does it do this way?
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
Just do this
C++ Syntax (Toggle Plain Text)
A(int m) { a=m; b=m; cout<<"\n values are"<<a<<endl<<b; }
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Solved Threads: 0
I don't want to assign this value to b. My question is that because of default constructor b is initialized to 0 but after typecasting why doesn't it retain it's value zero..it changes to garbage value.
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
becoz the initial value given by default constructor(i.e 0,0) is given to object a1....but when u write a statement
a temporary(unnamed) object is formed which calls ur parameterized contructor which initilizes only a and does not initilizes b...that is why b is having garbage value....
Note: Read about explicit keyword
C++ Syntax (Toggle Plain Text)
a1=1//implicit type conversion from int to user-defined type
Note: Read about explicit keyword
the code you posted should not even compile because class A is missing the overloaded = operator. in main(), the line "a = 1" does NOT call the default constructor as you might think, but will call the = operator. And since you did not define one the compiler should have complained (screamed loudly) to you.
•
•
•
•
Originally Posted by Ancient Dragon
the code you posted should not even compile because class A is missing the overloaded = operator. in main(), the line "a = 1" does NOT call the default constructor as you might think, but will call the = operator. And since you did not define one the compiler should have complained (screamed loudly) to you.
When compiler finds that there is wrong type on the right hand side of = it would look for a conversion function which can convert an int to A type. The argument constructor can meet this requirement. hence the compiler would decide to call it. This is an implicit conversion, one that you may not have intended to make possible......as far as = operator is concerned...compiler will supply that for you...don't worry about that
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by sunnypalsingh
becoz the initial value given by default constructor(i.e 0,0) is given to object a1....but when u write a statement
a temporary(unnamed) object is formed which calls ur parameterized contructor which initilizes only a and does not initilizes b...that is why b is having garbage value....C++ Syntax (Toggle Plain Text)
a1=1//implicit type conversion from int to user-defined type
Note: Read about explicit keyword
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; class A { public: int a; float b; public: A() { a=0; b=0; } A(int m) { a=m; cout<<"\n values are"<<a<<endl<<b; } }; void main() { A a1; a1=1; cout<<a1.b; }
When I make the members public, the output of a1.b is also a garbage value. And if the constructor is returning the value to the object then isn't it contradicting the concept I asked about in one of my previous threads..
See this thread
http://daniweb.com/techtalkforums/thread32689.html
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
•
•
•
•
Originally Posted by aminura
So is it like that the temporary object formed during the calling of parameterised constructor returns the value to the actual object?
C++ Syntax (Toggle Plain Text)
a1=1
Note:I didn't see the link of the thread u provided
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Solved Threads: 0
you answered that ...regarding returning value.
Now I am little confused...since constructors don't return any value how does type casting using parameterised constructors work. Like in the program a1=1; calls the constructor and value of a and b changes accordingly in the actual object..how does this happen?
Now I am little confused...since constructors don't return any value how does type casting using parameterised constructors work. Like in the program a1=1; calls the constructor and value of a and b changes accordingly in the actual object..how does this happen?
Last edited by aminura; Oct 19th, 2005 at 3:02 pm. Reason: one more question
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
![]() |
Similar Threads
- Problem while Type Casting (C++)
- Type casting and type conversions (C)
- type casting (C++)
- conversion type (C++)
Other Threads in the C++ Forum
- Previous Thread: Coin flipping program, so close to finishing
- Next Thread: What is wrong with my code?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






