From the course: C++ Development: Advanced Concepts, Lambda Expressions, and Best Practices
Conversion operators - C++ Tutorial
From the course: C++ Development: Advanced Concepts, Lambda Expressions, and Best Practices
Conversion operators
- [Instructor] The conversion operator is somewhat of a special case, sometimes called a conversion function. You can think of this as overloading a cast operator for casting your class to another type. Let's take a look at an example. This is rational2.cpp from chapter one of the exercise files, and you'll notice that I've included the string class. I'm using STL strings throughout this course. And you'll also notice that this version of the rational class, I've taken all of the operators, and I've implemented them as non-member functions so they work with implicit conversions. See the video Non-member Operators earlier in this chapter for more explanation. So what happens if we want to use our class with an STL string? I'm going to come down here into main, and we'll declare a string, and I'll give it a value here. I'll say "Rational is" with a space. And then I'm going to concatenate a rational number. And so I'll say s += a. What we want this to do is we want this to take the string representation of a and concatenate it to the end of our STL string so it says, "Rational is," or whatever it is. I believe that's 1 1/3 for the value of a. And you'll notice that we get this error. No viable overload +=. And that's because we have a string object on the left-hand side and a rational object on the right-hand side of our plus-equal operator, and there is no overload for that. So we solve this by adding a conversion operator to our class for a string value. And that'll allow the rational object to operate as if it were a string, and to return a viable string value that can be concatenated to our string object. So I'm going to come back up here into our class. And this is actually extremely simple to do. All I do is I come up here and I say, operator string return str like that. And now we come back down into our main. You'll notice that our error has gone away, and I can come out and I can print our string. Or, why don't I give it a new line? I'll just say like that. And when I build and run this, you'll notice that our string says, "Rational is 1 1/3," just as we expected. And how's it happening? We have the string s, "Rational is." And we can concatenate the rational value, but because this expression is expecting a string, the compiler looks for a string conversion operator. And this works as expected. This works anywhere a conversion can happen. For example, I can come up here and I can create a function that expects a string, and I can print the string. Give it a capital T for consistency there. And down here, instead of all of this, I can simply call p a like that. And when I build and run, notice it says, "The string is." So what's happening is that the a, our rational object, a, is being passed to this function, and it sees that it requires an s. And so it says, "Okay, do we have a conversion operator for that?" And it converts it to the string, and then it prints the string. So just a little extra little detail here. Where it says operator string, we can actually say auto. And we can use the auto type for this. And because it's returning a string object, it knows at compile time it will create a string conversion operator. And I can build and run this, and you'll see that it works exactly as we expect. The conversion operator overload is a common and useful technique that allows you to use your class as a first-class type, fully controlling how it is cast to other types.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.