简体   繁体   中英

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. Illustrator has a complicate and sometimes inconsistent 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. 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.

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).

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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