Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return different types from c++ functions

Ok, so I am struggling with the following:

I have a class (A), which holds a vector<B> (a vector for Qt) of some other classes (B). The class B has a property, let's say p and also has a name.

For one case, I would like to get a listing of the B objects that are to be found in class A which have the p property set, and for another case I would like to get a list of the names of the B objects that have the property set.

And most of all, I would like to have these two functions to be called the same :)

So, something like:

class A
{
public:
    QVector<B*> B_s_with_p() { ... }

    QStringList B_s_with_p() { ... }
};

but I don't want to have a helper parameter to overload the methods (yes, that would be easy), and the closest I have got is a method with a different name ... which works, but it's ugly. Templates don't seem to work either.

Is there a way using todays' C++ to achieve it?

like image 944
Ferenc Deak Avatar asked Dec 05 '25 17:12

Ferenc Deak


1 Answers

A cheat would be to use a reference to QVector<B*> or QStringList as an argument instead of a return type. That way you can overload B_s_with_p as normal. I guess you don't consider that a valid option.

like image 135
Jeff Dun Avatar answered Dec 08 '25 17:12

Jeff Dun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!