Independent interface implementation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Armin Zingler

    Independent interface implementation

    Hi,

    sorry for this C++ noob question... I'm coming from VB.Net actually and I
    am trying to do the same in C++ (/CLR) as I do here (pseudo code partial):

    class C
    implements I1
    implements I2

    private sub I1_f1() implements I1.f1
    end sub

    private sub I2_f1(args) implements I2.f1
    end sub

    public sub M1
    end sub
    public sub M2()
    end sub
    end class

    So, in VB.Net it is possible to write an object that has 3 different
    (inter)faces:

    a) class ("Dim o As C"): public methods M1, M2
    b) implementing interface I1 ("Dim o as I1 = New C"): method f1 implemented
    by I1_f1
    c) implementing interface I2 ("Dim o as I2 = New C"): method f1 implemented
    by I2_f1

    For me it is especially very important to consider each of the three as an
    independent and individual context. A reference of type C is a different
    context(/contract) than a reference of type I1 or than type I2, even if all
    references reference the same object.

    I haven't figured out how to realize the same in C++. It seems, the "wiring"
    of interface members to class members is only done by the member name. As a
    consequence, the independence is lost. Therefore, I don't know how to write
    different implementations for function f1 that only happens to have the same
    name in both interfaces but might have a completely different meaning. I
    also don't know how to avoid that a function implementing an interface is
    added to the "class interface" (a reference of type C).

    I hope you can help me with this or just tell me that it is not possible.

    Thanks in advance.


    Armin

  • Adelle Hartley

    #2
    Re: Independent interface implementation

    Armin Zingler wrote:
    Hi,
    >
    sorry for this C++ noob question... I'm coming from VB.Net actually and I
    am trying to do the same in C++ (/CLR) as I do here (pseudo code partial):
    >
    I could be making this up, but I think the following is correct:

    class C : public I1, I2
    {
    private:
    I1::f1()
    {
    ...
    }

    I2::f1()
    {
    ...
    }
    };


    A useful resource for getting started with C++/CLI:


    Adelle.

    Comment

    • =?Utf-8?B?RGF2aWQgQW50b24=?=

      #3
      RE: Independent interface implementation

      This is the equivalent produced by Instant C++ - note that the method name
      does not have to be the same as the interface name if you use the explicit
      CLI implementing syntax (e.g., "sealed = I1::f1"):

      private ref class C : I1, I2
      {

      private:
      virtual void I1_f1() sealed = I1::f1
      {
      }

      virtual void I2_f1(System::O bject ^args) sealed = I2::f1
      {
      }

      public:
      void M1()
      {
      }
      void M2()
      {
      }
      };
      --
      Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

      C++ to C#
      C++ to VB
      C++ to Java
      VB & C# to Java
      Java to VB & C#
      Instant C#: VB to C#
      Instant VB: C# to VB
      Instant C++: VB, C#, or Java to C++/CLI


      "Armin Zingler" wrote:
      Hi,
      >
      sorry for this C++ noob question... I'm coming from VB.Net actually and I
      am trying to do the same in C++ (/CLR) as I do here (pseudo code partial):
      >
      class C
      implements I1
      implements I2
      >
      private sub I1_f1() implements I1.f1
      end sub
      >
      private sub I2_f1(args) implements I2.f1
      end sub
      >
      public sub M1
      end sub
      public sub M2()
      end sub
      end class
      >
      So, in VB.Net it is possible to write an object that has 3 different
      (inter)faces:
      >
      a) class ("Dim o As C"): public methods M1, M2
      b) implementing interface I1 ("Dim o as I1 = New C"): method f1 implemented
      by I1_f1
      c) implementing interface I2 ("Dim o as I2 = New C"): method f1 implemented
      by I2_f1
      >
      For me it is especially very important to consider each of the three as an
      independent and individual context. A reference of type C is a different
      context(/contract) than a reference of type I1 or than type I2, even if all
      references reference the same object.
      >
      I haven't figured out how to realize the same in C++. It seems, the "wiring"
      of interface members to class members is only done by the member name. As a
      consequence, the independence is lost. Therefore, I don't know how to write
      different implementations for function f1 that only happens to have the same
      name in both interfaces but might have a completely different meaning. I
      also don't know how to avoid that a function implementing an interface is
      added to the "class interface" (a reference of type C).
      >
      I hope you can help me with this or just tell me that it is not possible.
      >
      Thanks in advance.
      >
      >
      Armin
      >
      >

      Comment

      • Armin Zingler

        #4
        Re: Independent interface implementation

        "David Anton" <DavidAnton@dis cussions.micros oft.comschrieb
        This is the equivalent produced by Instant C++ - note that the
        method name does not have to be the same as the interface name if
        you use the explicit CLI implementing syntax (e.g., "sealed =
        I1::f1"):
        >
        private ref class C : I1, I2
        {
        >
        private:
        virtual void I1_f1() sealed = I1::f1
        {
        }
        >
        virtual void I2_f1(System::O bject ^args) sealed = I2::f1
        {
        }
        >
        public:
        void M1()
        {
        }
        void M2()
        {
        }
        };


        Aaah! :-) Thanks a lot for this. Also thanks to Adelle. Don't know why I
        missed the decisive part. (thought I've read the language reference
        thoroughly...). I'll clean my glasses next time before....

        Now knowing what to look for, if anybody will search the archives, here's
        the link:



        Armin

        Comment

        Working...