简体   繁体   English

在 c++ 中同时处理 static 和非 static 函数

[英]Handling both static and non static functions in c++

I have a c++ class, part of its defintion is:我有一个 c++ class,它的部分定义是:

template <class InputClass>
class MyClass

And InputClass is defined as a child class of MyClass: InputClass被定义为 MyClass 的子 class:

class InputClass: public MyClass<InputClass>

there are many places in MyClass that has this call: MyClass 中有很多地方都有这个调用:

InputClass::AttributeName()

Where AttributeName() is defined on different child classes of InputClass as:其中AttributeName()在 InputClass 的不同子类上定义为:

static std::string AttributeName

However, recently, there's specific child class of InputClass in which we have to define attributeName as non static, because we want different instances of this new child class to NOT share AttributeName ):然而,最近,有 InputClass 的特定子 class,我们必须将属性名称定义为非 static,因为我们希望这个新子 class 的不同实例不共享AttributeName名称):

std::string AttributeName

What modifications can I make such that I can still AttributeName() either as a static or non-static variable from inside MyClass ?我可以进行哪些修改,以便我仍然可以AttributeName()作为 static 或MyClass内部的非静态变量?

To be honest I can't see how to make a static and a non-static member functions with the same name in derived classes and differentiate between them in the base class.老实说,我看不出如何在派生类中创建 static 和具有相同名称的非静态成员函数,并在基础 class 中区分它们。 Also, I don't know if there is a way to call a non-static member function which is declared in the derived class and is not declared in the base class.另外,我不知道是否有办法调用非静态成员 function,该成员在派生的 class 中声明,但未在基础 class 中声明。

If we omit those two requirements, then you could check whether the static function exists and call it, or fallback to a non-static (virtual) function otherwise (in my example I replaced your AttributeName function with static_name and name for static and non-static functions correspondingly): If we omit those two requirements, then you could check whether the static function exists and call it, or fallback to a non-static (virtual) function otherwise (in my example I replaced your AttributeName function with static_name and name for static and non-相应的静态函数):

#include <string>
#include <iostream>
#include <utility>
#include <experimental/type_traits>

template<class InputClass>
class Item {

    virtual std::string name() {
        return "none";
    };
    ...
public:
    virtual ~Item() = default;
    
    ...   
};

class DerivedNonStatic: public Item<DerivedNonStatic> {
    std::string name() final {
        return "non-static item";
    }
};

class DerivedStatic: public Item<DerivedStatic> {
public:
    static std::string static_name() {
        return "static item";
    }
};

Now, in the base Item class you can decide on which function to use like this:现在,在基本Item class 中,您可以决定使用哪个 function,如下所示:

class Item {
    
    template<typename T>
    using static_name_t = decltype(T::static_name); // static member function type
    static constexpr bool has_static_name = std::experimental::is_detected_v<static_name_t, InputClass>;

    void printName() {
        if constexpr(has_static_name) {
            std::cout << InputClass::static_name() << std::endl;
        } else {
            std::cout << this->name() << std::endl;
        }
    };
    ...
}

And how it can be used in the client code:以及如何在客户端代码中使用它:

int main() {
    DerivedNonStatic dns;
    dns.printName(); // prints "non-static item"

    DerivedStatic ds;
    ds.printName(); // prints "static item"
}

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

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