[英]c++ : Universal getter for class
我需要帮助! 我想为我的类定义一个模板方法来访问它的私有字段。 这是我的代码:
#include <string>
#include <vector>
using namespace std;
class ex
{
public:
ex(string pegah_,int amin_):pegah(pegah_),amin(amin_){}
template<typename T>
T get_field(){
if(is_same<T,string>::value)
return pegah;
else if(is_same<T,int> ::value)
return amin;
}
private:
string pegah;
int amin;
};
int main(void)
{
string i = "salam";
int o=10;
ex y(i,o);
y.get_field<string>();
}
如你所见,我只想使用一个功能。 但我一直收到这个错误:
test.cpp: In instantiation of ‘T ex::get_field() [with T = std::basic_string<char>]’:
test.cpp:30:21: required from here
test.cpp:15:8: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
return amin;
^
In file included from /usr/include/c++/4.8/string:52:0,
from test.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:490:7: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ [-fpermissive]
basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
有人可以帮忙吗?
相反,您可以像这样放置代码:
template<typename T> T get_field();
// outside the class:
template<> inline int ex::get_field<int>() { return amin; }
template<> inline string ex::get_field<string>() { return pegah; }
正如你现在所拥有的那样, if..else
所有分支都必须编译。
基本上你有三种选择。
首先使用模板成员函数的显式特化。
class Foo {
public:
template <typename T>
T get () const;
private:
std::string str {"XXX"};
int value {42};
};
template <>
inline std::string Foo::get () const {
return str;
}
template <>
inline int Foo::get () const {
return value;
}
第二个是使用具有不同参数类型的辅助函数。
class Foo2 {
public:
template <typename T>
T get () const {
return get_helper (typename std::is_same<T, std::string>::type {});
}
private:
std::string get_helper (std::true_type) const {
return str;
}
int get_helper (std::false_type) const {
return value;
}
private:
std::string str {"YYY"};
int value {44};
};
第三种选择是使用SFINAE。
class Foo3 {
public:
template <typename T>
typename std::enable_if<std::is_same<T, std::string>::value, T>::type get () const {
return str;
}
template <typename T>
typename std::enable_if<std::is_same<T, int>::value, T>::type get () const {
return value;
}
private:
std::string str {"ZZZ"};
int value {45};
};
用法如下:
template <typename T>
void show (T v) {
std::cout << v << std::endl;
}
Foo f1;
show (f1.get<std::string> ());
show (f1.get<int> ());
Foo2 f2;
show (f2.get<std::string> ());
show (f2.get<int> ());
Foo3 f3;
show (f3.get<std::string> ());
show (f3.get<int> ());
当您想要区分两种类型时,第二个选项很有用。 如果你有更多的吸气剂,那么你可能需要使用第一个或第三个选项。
我认为最好为每个字段定义一个getter和setter。 这是一种更好的方法。 它更容易阅读和理解,你实现与模板技术相同。
您的代码说明:
由于类型检查,它无法编译。 在C ++ 11中使用时会生成模板函数。 您将它与模板参数string
一起使用,以便生成该函数。 问题是你生成一个函数,它返回T
作为一个string
,但你的函数中有代码返回int
(变量amin
)。 如果T
等于string
,则在脑海中生成函数:
string get_field(){
if(is_same<string,string>::value)
return pegah; // OK
else if(is_same<string,int> ::value)
return amin; // NOT OK, amin is of type int
}
一种解决方案是MM ,它被称为专业化。 您专门为(a)特定参数设置模板。 还有其他答案即将出现。
我不建议这样做,因为除了为专用模板中的每个变量生成getter函数之外,你最后什么都不做。 你也可以写一下:
string get_pegah(){ return pegah; }
int get_amin() { return amin; }
更易于阅读,维护和直接。 我觉得效率更高。
如你所见,我只想使用一个功能
你真的不是。 您可以调用get_field<string>
或get_field<int>
并在调用时生成相应的函数; 使用T=string
, T=int
或两者(取决于您的用例)。 虽然你现在已经学会了,但在这种情况下这样做是错误的。
您可能意味着您希望有一个函数定义来执行您想要的操作。 我不认为这是可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.