locked
Signature of member contains native type 'NativeClass' - Baffled!

    Question

  • Very basic question indeed. Consider the following code snippet in a C++/Cx Metro Style App.

    I cannot pass a native C++ argument into public Ctor or public function. Why not?

    Is there a way I can pass a Native object pointer to a Cx object, so, I can have access to the former from the latter.

    -Shafiq.

     

    //-------------------------------------------------------------------
    // Sample for Microsoft
    //-------------------------------------------------------------------
    class NativeClass{};

    public ref class CxClass sealed
    {
    public:                                         // If private, then compiles.
        CxClass(){}
        // CxClass(NativeClass* aNc){}              // Does not compile.
        // void foo(NativeClass* aNc){}             // Does not compile.

    private:
        NativeClass* aNc_;
    };

    Here is the error message:

     error C3986: '{ctor}': signature of member contains native type 'NativeClass'

     error C3986: 'foo': signature of member contains native type 'NativeClass'

     

     

     
    Saturday, May 19, 2012 4:21 AM

Answers

  • Why not pass an integer? Meaning have a constructor like this:

    CxClass(int nativeClassPtr)
    {
       aNc_ = (NativeClass*)nativeClassPtr;
    }
    

    And from instantation point you would write something like:

    NativeClass* myKlass = ... ;
    
    CxClass^ klass = ref new CxClass((int)myKlass);

    • Marked as answer by Shafiq2012 Saturday, May 19, 2012 7:09 PM
    Saturday, May 19, 2012 8:32 AM
  • This indeed does the job and answers my question. I am now baffled why the design would stop to pass a native type.

    Public types on an RT class have to be RT types since they may be consumed by other RT callers (including C# and JS).


    http://blog.voidnish.com


    Saturday, May 19, 2012 8:30 PM

All replies

  • Why not pass an integer? Meaning have a constructor like this:

    CxClass(int nativeClassPtr)
    {
       aNc_ = (NativeClass*)nativeClassPtr;
    }
    

    And from instantation point you would write something like:

    NativeClass* myKlass = ... ;
    
    CxClass^ klass = ref new CxClass((int)myKlass);

    • Marked as answer by Shafiq2012 Saturday, May 19, 2012 7:09 PM
    Saturday, May 19, 2012 8:32 AM
  • Your public interface cannot contain non-RT  types. So I believe this is by design.

    http://blog.voidnish.com

    Saturday, May 19, 2012 2:53 PM
  • This indeed does the job and answers my question. I am now baffled why the design would stop to pass a native type. Passing long would eradicate the confusion of 32bit vs 64bit pointer.

    The error message could give some indication of passing native pointer.

    -Shafiq.


    class NativeClass
    {
    public:
        int NativeAdd(int x, int y)
        {
            return x + y;
        }
    };

    public ref class CxClass sealed
    {
    public:                            // If private, then compiles.
        CxClass(int aNcPtr)            // native type
        {
            aNc_ = (NativeClass*) aNcPtr;
        }             
        int RtAdd(int x, int y)
        {
            return aNc_->NativeAdd(x, y);
        }

    private:
        NativeClass* aNc_;
    };


    from main or a Metro App function:
    ==================================
    //-----------------------------------------------------
    NativeClass aNc;
    CxClass^ aCx = ref new CxClass((int)(&aNc));
    int sum = aCx->RtAdd(10, 20);
    String^ test = "[sum=](" + sum + ")";
    //-----------------------------------------------------

    Saturday, May 19, 2012 7:17 PM
  • This indeed does the job and answers my question. I am now baffled why the design would stop to pass a native type.

    Public types on an RT class have to be RT types since they may be consumed by other RT callers (including C# and JS).


    http://blog.voidnish.com


    Saturday, May 19, 2012 8:30 PM
  • Make those constructors internal:

    class NativeClass{};

    public ref class CxClass sealed
    {
    public:                                         // If private, then compiles.
        CxClass(){}

    internal:

       CxClass(NativeClass* aNc){}              // Should compile.
       void foo(NativeClass* aNc){}             // Should compile.

    private:
        NativeClass* aNc_;
    }

    That way the public part (from the point of view of WinRT) of the class is properly exposed via WinRT types, and the internal part (which is the only part that reasonably knows about NativeClass) can still be used (with public visibility from the point of view of C++) from within your assembly (similar to C#).


    http://www.iconstructions.be

    • Proposed as answer by naiveCoder Sunday, August 12, 2012 10:00 AM
    Monday, May 21, 2012 3:43 PM
  • I don't think this is the way to do this. It's like smuggling interface pointers in the old COM days. Please take a look at my answer about using 'internal', this solves the problem without any blind casts.

    http://www.iconstructions.be

    Monday, May 21, 2012 3:48 PM