C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
In C++, the parentheses operator ()
cannot be overloaded directly as a cast operator. Instead, you should use a type conversion operator for overloading the casting operation. The type conversion operator is a member function that has the following syntax:
operator type() { // implementation }
This tutorial will demonstrate how to overload a type conversion operator for a custom class, allowing it to be converted to another type.
Let's create a simple Temperature
class that stores a temperature value in Celsius:
#include <iostream> class Temperature { float celsius; public: // Constructor Temperature(float c) : celsius(c) {} // Function to display the temperature void display() const { std::cout << celsius << "��C"; } };
Suppose we want to convert the Temperature
object to Fahrenheit. We can overload a type conversion operator that returns a float
representing the temperature in Fahrenheit:
// Type conversion operator to convert the object to Fahrenheit (float) operator float() const { return celsius * 9.0 / 5.0 + 32; }
Add the above code inside the Temperature
class to overload the conversion operator.
With the type conversion operator in place, we can now convert a Temperature
object to a float
representing the temperature in Fahrenheit:
int main() { Temperature temp(25); // 25��C float fahrenheit = temp; // Convert to Fahrenheit using the type conversion operator std::cout << "The temperature is " << fahrenheit << "��F" << std::endl; return 0; }
In this example, we create a Temperature
object representing 25��C. We then convert the object to a float
using the type conversion operator we defined earlier, which gives us the temperature in Fahrenheit.
That's it for our tutorial on overloading a type conversion operator in C++. By using a type conversion operator, you can enable your custom class objects to be converted to other types in a natural and intuitive way.
How to Overload the () Operator in C++:
operator()
function is used for overloading the function call operator.Example:
class MyFunction { public: int operator()(int x, int y) { return x + y; } }; int main() { MyFunction add; int result = add(3, 4); // Calls operator() return 0; }
Using the () Operator for Custom Type Conversions in C++:
operator()
can be used for custom type conversions.Example:
class TemperatureConverter { public: double operator()(double celsius) { return (celsius * 9.0 / 5.0) + 32.0; } }; int main() { TemperatureConverter celsiusToFahrenheit; double result = celsiusToFahrenheit(25.0); // Calls operator() return 0; }
Function Call Operator Overloading in C++:
operator()
allows instances of a class to be used as functions.Example:
class MyFunction { public: void operator()() { // Define function-like behavior } }; int main() { MyFunction func; func(); // Calls operator() return 0; }
Implicit and Explicit Type Conversions with () Operator in C++:
operator()
can be used for both implicit and explicit type conversions.Example:
class DistanceConverter { public: explicit operator int() const { return static_cast<int>(meters); } private: double meters; }; int main() { DistanceConverter converter; int result = converter(); // Explicit conversion return 0; }
C++ Function Object and () Operator Overloading:
operator()
.Example:
class Multiply { public: int operator()(int x, int y) { return x * y; } }; int main() { Multiply multiply; int result = multiply(3, 4); // Calls operator() return 0; }
Overloading () for User-Defined Types in C++:
operator()
to provide a callable interface.Example:
class Square { public: int operator()(int x) { return x * x; } }; int main() { Square square; int result = square(5); // Calls operator() return 0; }
Combining () Operator Overloading with Other Operators in C++:
operator()
can be combined with other operators for more complex behavior.Example:
class ComplexOperation { public: double operator()(double x, double y) { return x * y + x - y; } }; int main() { ComplexOperation op; double result = op(3.0, 2.0); // Calls operator() return 0; }