answersLogoWhite

0

What is a type cast in c plus plus?

Updated: 8/10/2023
User Avatar

Wiki User

14y ago

Best Answer

A type cast is an override within an expression that causes the compiler to generate conversion code and to treat the item as if it had a different type.

int a = 13;
int b = 4;
float c;

c = a / b; /* result is 3, the integer value of 13 divided by 4 */

c = (float) a / b; /* result is 3.1, the floating value of 13 divided by 4 */

In this case, the (float) keyword was the typecast. Note also that it was not necessary to typecast b, because the compiler recognizes the mixed mode expression.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

In order to implement automatic type casting you must provide conversion operator overloads in your object's class definition. Generally it is best not to provide conversion operators unless absolutely necessary; it is always better to implement conversions using explicit member methods instead. However, if you should require a conversion operator, the operator should simply invoke the method.

For instance, if your object can be converted to a std::string object, then you should provide an as_string() method that returns a std::string by value. For automatic conversion, you should provide a conversion overload that invokes the as_string() method:

class foo{

//...

public:

std::string as_string()const;

operator std::string(void)const;

};

std::string foo::as_string()const{

//...

}

foo::operator std::string()const{

return(as_string());

}

Note that conversion operators have no return type since the return type is implied by the operator name.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

Automatic type casting is implicit, whereas (non-automatic) type casting is explicit.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a type cast in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the name of the structure type in C plus plus?

The same as in C, struct.


User defined data type in c plus plus?

Use "typedef" : both in C and C++.


How do you apply if in char type in c plus plus?

char x = "C"; if(char == 'C') { } else { }


What is explicit typecasting in C plus plus programming?

int i = 42; char c = ( char ) i; // explicit cast double d = i; // implicit cast


What are the features of C plus plus that surpass C?

Object-oriented programming and stricter type-safety.


How unary minus can be overload in c plus plus?

type operator- ();


What is a primitive type variable in c plus plus?

same the types used in C. that is int...char...float...


Casting in c plus plus?

Yes, you can cast in C++, both statically and dynamically. Objects can also be cast provided the class designer implemented the appropriate conversion operators.


Can an integer data type in c plus plus stores character value?

no


What use of void data type in c plus plus?

doesn't return the value.


Defference between c plus plus and c?

these are difference in between c and c++: a) C is a SPL and C++ is a OOP. b) C has not concept of object but C++ has this feature. c) C has not 'class' name data type but C++ has.


What is the difference between declaration and a definition in c plus plus?

A declaration is an incomplete type whereas a definition is a complete type.