如何在c++中的重载函数之间切换
how do I switch between overload functions in c++
我正在寻找一种方法来切换我通过int成员变量的值在类中调用的函数重载。喜欢。
class someclass
{
private:
int tag;
SomeArithmeticType val;
// some class members
public:
void setvalue (int I){val = I; tag = 0}
void setvalue (float f) {val = f; tag = 1}
void setvalue(other o){val =o; tag=2}
int method(){}
float method(){}
other method(){}
// rest of class
}
main
{
// stuff
some class s;
s.setvalue(9.7654);
cout << s << endl;
}
如果标签是0,我想使用Int版本的方法,但如果它是另一个数字,我想要相应的方法重载
同一个函数不能有不同的返回类型。可以通过在每个定义中使用不同的形参列表来重载函数名。我建议使用不同的函数名来获得不同的返回类型。
你不想在这里重载,你想要一个模板化的类:
template <typename T = int> /* defaulting to int */
class SomeClass
{
private:
T tag;
public:
T method();
};
不能重载函数的返回类型,必须是相同的返回类型,但参数可以不同
相关文章:
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- 填充上编译器生成的复制构造函数之间的不一致
- 为什么不允许成员函数和非成员函数之间的函数重载?
- 如何在"push_*()"和"emplace_*()"函数之间进行选择?
- 仅具有运算符()的结构和普通函数之间的实际区别
- 当两者都调用时,删除和析构函数之间的区别?
- Release() 和析构函数之间的区别?
- 调用 "project" 函数和调用 DLL 函数之间的区别
- C++模板和非模板函数之间的重载解析
- std::shared_ptr 和 std::unique_ptr 构造函数之间的不对称
- C++ 和 Lua 函数之间的交互与 3D 矢量参数
- 在结构函数之间传递文件路径 C++ 编辑:修复LNK2019错误
- 在C++同名的顶级函数之间进行选择
- 在成员函数之间传递const变量为数组的索引
- Helgrind 报告了使用 Singleton 及其构造函数之间可能存在的竞争
- 函数模板和函数之间奇怪的不一致"normal"
- 在c#和c函数之间传递值和指针
- 在C++中调用 malloc() 与"operator new"函数之间的实现差异
- 在函数之间传递相同的数组
- C++编译错误是由于使用 std::move 时运动构造函数与其他非运动构造函数之间的冲突