Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function/Method Overloading C++: Data type confusion?

I'm having some trouble overloading methods in C++. As an example of the problem, I have a class with a number of methods being overloaded, and each method having one parameter with a different data type. My question: is there a particular order in the class these methods should appear in, to make sure the correct method is called depending on its parameters data type?

class SomeClass{
    public:
    ...
    void Method(bool paramater);
    void Method(std::string paramater);
    void Method(uint64_t paramater);
    void Method(int64_t paramater);
    void Method(uint8_t paramater);
    void Method(int8_t paramater);
    void Method(float paramater);
    void Method(double paramater);
    void Method(ClassXYZ paramater);
}

I noticed there was problem because when running:

Method("string");

it was calling:

Method(bool paramater);
like image 869
Tom Avatar asked Oct 28 '09 09:10

Tom


People also ask

Can we change data type in overloading?

Changing the Data Type of ParametersAnother way to do method overloading is by changing the data types of method parameters. The below example shows how we can implement method overloading with the different data types of parameters in method declaration and definition.

How do you remove ambiguity from function overloading?

There are two ways to resolve this ambiguity: Typecast char to float. Remove either one of the ambiguity generating functions float or double and add overloaded function with an int type parameter.

Why function overloading is not possible in C?

This feature is present in most of the Object Oriented Languages such as C++ and Java. But C doesn't support this feature not because of OOP, but rather because the compiler doesn't support it (except you can use _Generic).

Can overloaded functions return different types?

Overloading with same arguments and different return type −No, you cannot overload a method based on different return type but same argument type and number in java.


2 Answers

The order makes no difference. The method to call is selected by analyzing the types of arguments and matching them to the types of parameters. In case there's no exact match, the best-matching method is selected. In your case it happens to be the bool method.

You are supplying an argument of type const char[7]. According to the C++ overloading rules, the best path here is to let const char[7] decay to const char * and then convert it to bool using a standard conversion. The path with converting to std::string is considered worse, since it would involve a user-defined conversion from const char * to std::string. Generally, user-defined conversions lose overload resolution process to standard conversions. This is what happens in your case as well.

If you need std::string version to be called here, provide an explicit overload for const char * type, and delegate the call to std::string version by converting the argument to std::string type explicitly

void Method(const char *paramater /* sic! */)
{
  Method(std::string(paramater));
}
like image 62
AnT Avatar answered Oct 26 '22 02:10

AnT


The string literal "string" has type const char[] which can be implicity converted to bool. This is the best conversion candidate to one of your overloaded functions although it's not likely to be the most useful one.

If your intention was to have string literals be handled by the overload taking a std::string, then you need to add an overload taking a const char* and make the implementation call the std::string version.

like image 42
CB Bailey Avatar answered Oct 26 '22 01:10

CB Bailey