Sign in to follow this  
noatom

Is it still called overloading?

Recommended Posts

noatom    927

If I create a function that has the same name and arguments as another function,but a different return type,is it still called overloading?

Share this post


Link to post
Share on other sites
Ravyne    14300

Yep, you can't do that -- the compiler has no good way of knowing which function you intended to call without any difference in arguments. The work around would be to append "AsInt", "AsDouble" or somesuch to the end of your function name. Another option would be to create the function as a template function, throw a static assert in the generic function body, and then specialize the template function on the return types you want to support -- then you call the function like foo<int>(arg1, arg2).

Share this post


Link to post
Share on other sites
SiCrane    11839
They can in C++ when derived class member functions overriding base class member functions with covariant return types. I believe Java introduced covariant return types a while ago, but I don't remember with which version.

Share this post


Link to post
Share on other sites
phil_t    8084

C# also supports this when a class inherits from interfaces that have the same methods (e.g. IEnumerator GetEnumerator() and IEnumerator<T> GetEnumerator(), when something inherits from IEnumerable and IEnumerable<T>) . You have to cast to go through the interface before you make the method call.

Edited by phil_t

Share this post


Link to post
Share on other sites
noatom    927

They can in C++ when derived class member functions overriding base class member functions with covariant return types. I believe Java introduced covariant return types a while ago, but I don't remember with which version.

 

That's what i'm reffering to.

 

So is it still called overloading?

Share this post


Link to post
Share on other sites
Brother Bob    10344

In that case, it has nothing to do with the return type, or even the parameters for the matter. Just having a function in a derived class with the same name as one or more functions in the base class is enough to override the functions in the base class.

Share this post


Link to post
Share on other sites
SiCrane    11839
Yes and no. Covariant return types are a special case when overriding virtual functions. In that case even though the return type differs, it still is an override of the base virtual function. It's not exactly the same as regular overrides.

Share this post


Link to post
Share on other sites
noatom    927

Yes and no. Covariant return types are a special case when overriding virtual functions. In that case even though the return type differs, it still is an override of the base virtual function. It's not exactly the same as regular overrides.

 

 

What do you mean with regular overrides? Isn't overriding a term used just for giving another definition for a virtual function?

Share this post


Link to post
Share on other sites
Krohm    5030


Isn't overriding a term used just for giving another definition for a virtual function?
Yes, it is. But since we're talking about "covariant-return-type overrides", it makes sense to talk about overrides whose return type is exactly as originally specified, and that would be the regular override, an override like it always used to be.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this