Independent interface implementation

  • Thread starter Thread starter Armin Zingler
  • Start date Start date
A

Armin Zingler

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
 
Armin said:
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:
http://www.functionx.com/cppcli/index.htm

Adelle.
 
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::Object ^args) sealed = I2::f1
{
}

public:
void M1()
{
}
void M2()
{
}
};
--
http://www.tangiblesoftwaresolutions.com
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
 
David Anton said:
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::Object ^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:
http://msdn.microsoft.com/en-us/library/fw0bbh51.aspx


Armin
 
Back
Top