C ++:类的通用吸气剂 [英] c++ : Universal getter for class

查看:65
本文介绍了C ++:类的通用吸气剂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点需要帮助!我想为我的班级定义一个模板方法来访问其私有字段。这是我的代码:

I kind of need help! I want to define a template method for my class to access its private fields. Here is my code:

#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>();
}

如您所见,我只想使用一个功能。但是我一直收到这个错误:

as you see I want to use just one function. But I keep getting this error:

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());

有人可以帮忙吗?

推荐答案

我认为最好为每个字段定义一个getter和setter方法。那是一个更好的方法。与模板技术一样,它更易于阅读和理解。

I think it is better you define a getter and setter for each field. That is a better approach. It's easier to read and to understand and you achieve the same as with the template technique.

代码说明:

由于类型检查而无法编译。在C ++ 11中使用时会生成模板函数。您可以将其与模板参数 string 一起使用,以便生成函数。问题是您生成了一个函数,该函数以 string 的形式返回 T ,但是函数中有返回的代码 int (变量 amin )。像这样生成 T 等于 string

It does not compile because of type checking. Template functions are generated when used in C++11. You use it with template parameter string so the function is generated. The problem is that you generate a function that returns T as a string, but you have code in your function that returns int (variable amin). Generate the function in your mind like so for T equals 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 的解决方案,称为专业化。您专门针对一个或多个特定参数的模板。还有其他答案。


One solution is that of M.M, it's called specialization. You specialize a template for (a) specific argument(s). And there are also other answers coming up.

我不建议这样做,因为您最终除了产生吸气剂外什么也不做专用模板中每个变量的函数。您也可以只写:

I do not recommend that, because you finally do nothing else but generating getter functions for each variable in a specialized template. You could as well just have written:

string get_pegah(){ return pegah; }
int get_amin() { return amin; }

更易于阅读,维护和直截了当。

Easier to read, to maintain and straight forward. And more efficient I think.


如您所见,我只想使用一个功能

as you see I want to use just one function

您不是真的。您可以调用 get_field< string> get_field< int> 并在调用时会生成适当的函数;或者使用 T = string T = int 或同时使用两者(取决于您的用例)。尽管如您现在所了解,在这种情况下这样做是错误的。

You don't really. You either call get_field<string> or get_field<int> and when called the appropriate function would be generated; either with T=string, T=int or both (depending on your use case). Though as you have learned by now, it's an error to do so in that case.

您可能的意思是您想拥有一个功能定义做你想做的。我认为这是不可能的。

What you probably meant was that you want to have one function definition to do what you want. I don't think that is possible.

这篇关于C ++:类的通用吸气剂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆