简体   繁体   English

从c ++函数返回不同的类型

[英]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 ). 我有一个类( A ),它包含一些其他类( B )的vector<B> (Qt的向量)。 The class B has a property, let's say p and also has a name. B级有一个属性,比方说p ,也有一个名字。

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. 对于一个情况下,我想获得的上市B是在课堂上被找到的对象A它们具有p属性集,并为另一种情况,我想获得的名称列表B有对象物业集。

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? 有没有办法使用今天的C ++来实现它?

A cheat would be to use a reference to QVector<B*> or QStringList as an argument instead of a return type. 作弊是使用对QVector<B*>QStringList的引用作为参数而不是返回类型。 That way you can overload B_s_with_p as normal. 这样你可以正常重载B_s_with_p I guess you don't consider that a valid option. 我想你不认为这是一个有效的选择。

Only the signature is used to overload methods. 只有签名用于重载方法。 That means the return type can't be use to overload. 这意味着返回类型不能用于重载。

You have to use different methods names or manage your return value with a parameter : 您必须使用不同的方法名称或使用参数管理返回值:

class A
{
public:
    void B_s_with_p(QVector<B*>&);
    void B_s_with_p(QStringList&);
};

B_s_with_p() could use a template type as parameter. B_s_with_p()可以使用模板类型作为参数。

Why do you think that having two different names for two different sets of functionality is ugly? 为什么你认为两个不同的功能集有两个不同的名称是丑陋的? That's exactly what you should be doing to delineate the different return types here. 正是你应该做的,在这里描述不同的返回类型。

Another alternative is to have just the function that returns an object, and then create a names_of method that you pass the list of B objects and it returns the list of names. 另一种方法是使用返回对象的函数,然后创建一个names_of方法,传递B对象列表并返回名称列表。

Check your design .. Overloaded functions are one which have similar funcionality but differnt argument types .. So check why you want to 检查你的设计..重载函数是一个具有类似功能但参数类型不同的函数。所以检查你想要的原因

I would like to have these two functions to be called the same

refer to : What is the use/advantage of function overloading? 参考: 函数重载的用途/优点是什么?

The benefit of overloading is consistency in the naming of functions which perform the same task, just with different parameters. 重载的好处是执行相同任务的函数命名的一致性,只是使用不同的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM