简体   繁体   English

带有重载的模板中的c ++静态方法和继承

[英]c++ static methods and inheritance in templates with overloading

It's my first post here, I have looked for an answer for days, so I have ask. 这是我在这里的第一篇文章,几天来我一直在寻找答案,所以我要提问。 It is a complex construction. 这是一个复杂的结构。 I embed SpiderMonkey javascript engine in an Illustrator plugin. 我将SpiderMonkey JavaScript引擎嵌入到Illustrator插件中。 Illustrator has a complicate and sometimes inconsistent API. Illustrator具有复杂的API,有时甚至不一致。 In this case I have to call a method having a short type as parameter: 在这种情况下,我必须调用一个具有短类型作为参数的方法:

namespace ai
{
...
typedef signed short    int16;
...
};

The method is referred as a pointer in a struct: 该方法在结构中称为指针:

struct AIDocumentSuite {
...
    AIAPI AIErr (*GetDocumentRulerUnits) ( ai::int16 *units );
...
};

But, an enum is the source of the parameter: 但是,枚举是参数的来源:

enum AIDocumentRulerUnitValue {
    kUnknownUnits = 0,
    /** inches */
    kInchesUnits,
    /** centimeters */
    kCentimetersUnits,
    /** points */
    kPointsUnits,
    /** picas */
    kPicasUnits,
    /** millimeters */
    kMillimetersUnits,
    /** pixels */
    kPixelsUnits,
    /** Q units */
    kQUnits
};

I have wrapped all these in classes in order to have javascript objects managed by SpiderMonkey. 我将所有这些包装在类中,以使SpiderMonkey管理javascript对象。 I have the following working call somewhere in the code (a bit simplified, cleaned up of tests an verifications): 我在代码中的某个地方有以下工作调用(有点简化,清理了测试和验证):

ai::int16 aiDocumentRulerUnitValue;
AIErr aiErr = sAIDocument->GetDocumentRulerUnits(&aiDocumentRulerUnitValue);

...

            if (!jsAIDocumentRulerUnitValue::FromAIObject<jsAIDocumentRulerUnitValue>(cx, &aiDocumentRulerUnitValue, value))
                return false;

...
            jsProperty::SetProperty(cx, &obj, "value", value, true);

...

My issue is FromAIObject. 我的问题是FromAIObject。

I have a template base class: 我有一个模板基类:

template<typename T> class jsBaseDataClass : public jsBaseClass
{
public:
...

    template <typename jsT> static bool FromAIObject(JSContext *cx, T* aiObj, JSObject*& obj)
    {
...
return true;
    }
}

and a derived class: 和派生类:

class jsAIDocumentRulerUnitValue : public jsBaseDataClass<AIDocumentRulerUnitValue>
{
public:
...
    template <typename jsT> static bool FromAIObject(JSContext *cx, ai::int16* aiint16, JSObject*& obj)
    {
        AIDocumentRulerUnitValue aiDocumentRulerUnitValue = static_cast<AIDocumentRulerUnitValue>(*aiint16);

        return jsEnumDataClass<jsBaseClass>::FromAIObject<jsAIDocumentRulerUnitValue>(cx, &aiDocumentRulerUnitValue, obj);
    }

...
}

Now, the issue: if I write in the latter one: 现在的问题是:如果我写后者:

jsAIDocumentRulerUnitValue::FromAIObject<jsAIDocumentRulerUnitValue>(cx, &aiDocumentRulerUnitValue, obj);

it will try to call the method in the derived class (with the parameter of type ai::int16 = signed short). 它将尝试在派生类中调用方法(参数类型为ai :: int16 = signed short)。

If the method in the derived class wouldn't exist, it will call the method from the parent class, regardless the type of the second parameter. 如果派生类中的方法不存在,则它将从父类中调用该方法,而不管第二个参数的类型如何。

I wish to be able to look for the suitable method based on its parameters. 我希望能够根据其参数寻找合适的方法。 This would make me having a copy in each derived class of both methods (if necessary, or at least a copy of the default one). 这将使我在两种方法的每个派生类中都有一个副本(如有必要,或者至少是默认方法的一个副本)。

What am I doing wrong or how could I make them behave as in non static method overloading with inheritance? 我在做错什么,或者如何使它们的行为像在继承的非静态方法重载中一样?

Thank you. 谢谢。

You are doing nothing wrong. 你什么都没做错。 Your question looks like a duplicate of stack overflow: inheritance and method overloading 您的问题看起来像是堆栈溢出的重复:继承和方法重载

Overloaded methods get lost unless you using XXXX 除非您using XXXX否则重载的方法会丢失

Do something like that. 做这样的事情。

class Base {
public:
    static void method(std::string someParam);
}

class Derived : public Base {
public:
    using Base::method;
    static void method(int param);
}

The point is using the using keyword for making base class's functions visible. 关键是使用using关键字使基类的功能可见。

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

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