interfaces
-
i have two interfaces i1 and i2; both of them have got a function with the same name i1- function_some(); i2- function_some(); now i am gonna implement both the interfaces to a class how do i define the functions after implementing.... of i1 and i2 respectively thanks bye
-
i have two interfaces i1 and i2; both of them have got a function with the same name i1- function_some(); i2- function_some(); now i am gonna implement both the interfaces to a class how do i define the functions after implementing.... of i1 and i2 respectively thanks bye
You will scope them explicitly. void i1.function_some() { // body } void i2.function_some() { // body } Now, if you try to call function_some on your class instance, you'll get an error, you'll need to scope it there, too, I am not sure if there's a way to do this, apart from casting to the right interface.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
You will scope them explicitly. void i1.function_some() { // body } void i2.function_some() { // body } Now, if you try to call function_some on your class instance, you'll get an error, you'll need to scope it there, too, I am not sure if there's a way to do this, apart from casting to the right interface.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
class c { void i1.function_some() { // body } void i2.function_some() { // body } } class c1:c { c dd =new c(); dd.//how do i call particular funcion } if i inherit the class where i implemented how do i call these in the inherited class
-
class c { void i1.function_some() { // body } void i2.function_some() { // body } } class c1:c { c dd =new c(); dd.//how do i call particular funcion } if i inherit the class where i implemented how do i call these in the inherited class
-
As already mentioned - cast it to the correct interface: ((i2)dd).function_some() If you need to do this your OO design is most likely wrong though.
thnx i have used things in the similar manner but it didnt strike me thanx
-
You will scope them explicitly. void i1.function_some() { // body } void i2.function_some() { // body } Now, if you try to call function_some on your class instance, you'll get an error, you'll need to scope it there, too, I am not sure if there's a way to do this, apart from casting to the right interface.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Christian Graus wrote:
You will scope them explicitly.
Not necessarily:
interface I1
{
void TheMethod();
}interface I2
{
void TheMethod();
}class C : I1, I2
{
public void TheMethod()
{
Console.WriteLine("Did you ask for me?");
}
}public static int Main(string[] args)
{
C c = new C();
I1 i1 = new C();
I2 i2 = new C();
c.TheMethod();
i1.TheMethod();
i2.TheMethod();
return 0;
}However, I agree that it's bad design.
Cheers, Vıkram.
After all is said and done, much is said and little is done.
-
Christian Graus wrote:
You will scope them explicitly.
Not necessarily:
interface I1
{
void TheMethod();
}interface I2
{
void TheMethod();
}class C : I1, I2
{
public void TheMethod()
{
Console.WriteLine("Did you ask for me?");
}
}public static int Main(string[] args)
{
C c = new C();
I1 i1 = new C();
I2 i2 = new C();
c.TheMethod();
i1.TheMethod();
i2.TheMethod();
return 0;
}However, I agree that it's bad design.
Cheers, Vıkram.
After all is said and done, much is said and little is done.
Right, if the contracts of both interfaces can be satisfied with one method, then things are simple. Until I read this thread I didn't know any other way existed. A class that implements more than one interface may need to provide a separate implementation for each.