473,320 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Explicit virtual interfaces

This does what I expect (as it should, it's from Hoffman and Kruger's C#
Unleashed):

public interface ISpeak
{
void Speak();
}

public abstract class Animal : ISpeak
{
public abstract void Speak();
}

public class Dog : Animal
{
public override void Speak()
{
Console.WriteLine( "Woof" );
}
}

public class Cat : Animal
{
public override void Speak()
{
Console.WriteLine( "Meow" );
}
}
that is, it allows me to define an interface, implement it in a base class
as an abstract method, and then override that method in an inherited class.
But I need to implement two interfaces with the same signature, and this
doesn't compile:

public interface ISpeak
{
void Speak();
}

public abstract class Animal : ISpeak
{
abstract void ISpeak.Speak(); // abstract not allowed
}

class Dog : Animal, ISpeak
{
override void ISpeak.Speak() // override not allowed
{
Console.WriteLine( "Woof" );
}
}

public class Cat : Animal
{
public override void Speak()
{
Console.WriteLine( "Meow" );
}
}

Is this simply not allowed (in which case why not?) or is there some arcane
syntax to allow this?

TIA

Andrew
Apr 10 '06 #1
4 1675
amaca wrote:

<snip>
But I need to implement two interfaces with the same signature, and this
doesn't compile:


Do you want to use the same implementation for both interfaces? If so,
I'd create a new abstract method in the base class (eg SpeakImpl) and
implement the two interfaces in a way which calls SpeakImpl.

Jon

Apr 10 '06 #2
>Do you want to use the same implementation for both interfaces? If so,
I'd create a new abstract method in the base class (eg SpeakImpl) and
implement the two interfaces in a way which calls SpeakImpl.


Actually no, but it does seem the obviously easy way to go, so I'll do that.
Thanks.

Andrew

Apr 10 '06 #3
Hi,

You will need to use explicit declaration:

public interface ISpeak{ void A();}

abstract class C1:ISpeak
{
public abstract void A();

void ISpeak.A()
{
}

}
class C2:C1, ISpeak
{
public override void A()
{

}

void ISpeak.A()
{
}

}

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Apr 10 '06 #4
You're missing the public keyword when defining your abstract method.

"amaca" <pu****@ajam.net> wrote in message
news:Od**************@TK2MSFTNGP02.phx.gbl...
This does what I expect (as it should, it's from Hoffman and Kruger's C#
Unleashed):

public interface ISpeak
{
void Speak();
}

public abstract class Animal : ISpeak
{
public abstract void Speak();
}

public class Dog : Animal
{
public override void Speak()
{
Console.WriteLine( "Woof" );
}
}

public class Cat : Animal
{
public override void Speak()
{
Console.WriteLine( "Meow" );
}
}
that is, it allows me to define an interface, implement it in a base class
as an abstract method, and then override that method in an inherited
class. But I need to implement two interfaces with the same signature, and
this doesn't compile:

public interface ISpeak
{
void Speak();
}

public abstract class Animal : ISpeak
{
abstract void ISpeak.Speak(); // abstract not allowed
}

class Dog : Animal, ISpeak
{
override void ISpeak.Speak() // override not allowed
{
Console.WriteLine( "Woof" );
}
}

public class Cat : Animal
{
public override void Speak()
{
Console.WriteLine( "Meow" );
}
}

Is this simply not allowed (in which case why not?) or is there some
arcane syntax to allow this?

TIA

Andrew

Apr 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Geiregat Jonas | last post by:
I've seen some c++ code where before a method there was vitual ex: class foo{ public: virtual bool method(); } what does virtual do ?
13
by: Just D | last post by:
All, What are advantages and disadvantages of these two different approaches? 1 . I create a base class with a few virtual methods, then I derive my classes from this class, override these...
14
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
3
by: Imre | last post by:
Hi! I've got some questions regarding heavy use of virtual inheritance. First, let's see a theoretical situation, where I might feel tempted to use a lot of virtual inheritance. Let's...
3
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
2
by: =?Utf-8?B?d2R1ZGVr?= | last post by:
I know that I can't declare a method in an interface as virtual and override it, when I am using an explicit interface, but I am wondering if anyone else has come across this problem and how they...
4
by: archimed7592 | last post by:
Hi. for example i have base class A and dirved class B: struct A { virtual void f() { std::cout << "A::f()" << std::endl; } }; struct B : public A {
2
by: puzzlecracker | last post by:
I don't see the purpose of explicit interface implementation other than to hide its signature in the class that implements it, and, instead, write your own, perhaps with a different signature,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.