简体   繁体   English

使用模板调用类的函数

[英]Calling a function of a class using templates

I might be asking for something absurd here (and I am new with c++), but still I want to give it a try. 我可能会在这里要求一些荒谬的东西(我是c ++的新手),但我仍然想尝试一下。 I am trying to access a method of a class in a function that receives it's object, but, at the same time, I wanted to somehow not have to specify which class is that, that's why I am using templates. 我试图在接收它的对象的函数中访问类的方法,但同时,我想以某种方式不必指定哪个类,这就是我使用模板的原因。 This sounds incredibly stupid, but anyway, that's the code: 这听起来非常愚蠢,但无论如何,这就是代码:

class Dog;

class Animals{
public:
// initializes all protected variables
Animals(string fam, string gen, string espec, string subespec);
~Animals();

template <class A>
void Eat(A &);

protected:
    string family;
    string genre;
    string especies;
    string subespecies;
};

template <class A>
void Animals::Eat(A &obj)
{
    // this is where I don't know what I could do.
    std::cout << obj.methodOfA();
}

/--- / ---

int main()
{
    Dog *myDog = new Dog;
    myDog->Eat(myDog);

    //supposing I had class "Cats"
    Cat *myCat = new Cat;
    myCat->Eat(myCat);
}

This might be quite stupid, so feel free to point out my stupidity on the answers... if anyone ever answer this. 这可能是非常愚蠢的,所以请随意指出我对答案的愚蠢......如果有人回答这个问题。 I wanted this because I'll have many classes that are derived of class Animals, and independent of which of those classes, I wanted them to be able to use this method properly. 我想要这个,因为我会有许多派生类动物的类,并且独立于哪些类,我希望它们能够正确使用这种方法。

Thanks. 谢谢。

One way you could solve your problem is that you pass the function to be executed as a second argument. 解决问题的一种方法是将要执行的函数作为第二个参数传递。 Then you can invoke the function with the operator.* or operator->* . 然后,您可以使用operator.*调用该函数operator.*operator->*

This could look something like this: 这看起来像这样:

class Animals {
public:
// initializes all protected variables
Animals(std::string fam, std::string gen, std::string espec, std::string subespec);
~Animals();

template <class A, class Fun>
void Eat(A *&, Fun);

protected:
    std::string family;
    std::string genre;
    std::string especies;
    std::string subespecies;
};

template <class A, class Fun>
void Animals::Eat(A *& obj, Fun function)
{
    std::cout << ((obj->*function)()).c_str();
}


class Dog : public Animals {
public:
    std::string TestFunction() {
        return std::string("I am a dog");
    }
};


int main() {

    Dog *myDog = new Dog;
    myDog->Eat(myDog, &Dog::TestFunction);

    //...
}

The output of this program would be: 该计划的输出将是:

I am a dog

To clarify this line: 澄清这一行:

((obj->*function)())

You first apply the operator->* to obj and pass it the address of the member function function . 首先将operator->*应用于obj ,并将其传递给成员函数function的地址。 Note that you have to put parantheses around this statement. 请注意,您必须在此语句周围添加parantheses。 After that you invoke it.( () ). 之后你调用它。( () )。

If there are any questions concerning the code provided above feel free to ask. 如果对上面提供的代码有任何疑问,请随时提出。

Regards, 问候,

Philinator Philinator

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

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