In a derived class I am trying to redefine the implementation of a interface
method defined in a base class - the base class interface method was not
declared virtual.
I have yet to actually test this approach, but it looks ugly. Is there any
better way?
public interface ITest
{
void Func();
}
public class A : ITest
{
void ITest.Func( )
{
// some implementation
}
}
public class B : A, ITest
{
void ITest.Func( )
{
// want to call A's ITest interface
( ( ITest )( A ) this ).Func( );
}
} 2 6121
As class B extends class A, and class A has already implemented ITest,
you do not need to implement it again in B. You can simply call B.Func()
as follows:
public interface ITest
{
void Func();
}
public class A : ITest
{
// Implement ITest.Func() methods
public void Func()
{
Console.Out.Wri teLine("In A.Func()...");
}
}
public class B : A
{
}
class Program
{
static void Main(string[] args)
{
B b = new B();
b.Func(); // Prints In A.Func()...
}
}
If I've misunderstood, and you actually want to redefine the
implementation of the non-virtual Func() method in class B, then you can
hide A's implementation using the 'new' keyword:
public new void Func()
{
Console.Out.Wri teLine("In B.Func()...");
}
Regards
Dave
Kevin Frey wrote:
In a derived class I am trying to redefine the implementation of a interface
method defined in a base class - the base class interface method was not
declared virtual.
I have yet to actually test this approach, but it looks ugly. Is there any
better way?
public interface ITest
{
void Func();
}
public class A : ITest
{
void ITest.Func( )
{
// some implementation
}
}
public class B : A, ITest
{
void ITest.Func( )
{
// want to call A's ITest interface
( ( ITest )( A ) this ).Func( );
}
}
True, but beware that if you use the "new" modifier then a caller can
cast a derived-class object back to the base class and access the base
class implementation, thus:
B bObject = new B();
A aObject = bObject;
aObject.Func();
will invoke A.Func(), not B.Func().
If A has not declared Func() virtual then this is the best you can do,
unfortunately.
Dave Shooter wrote:
As class B extends class A, and class A has already implemented ITest,
you do not need to implement it again in B. You can simply call B.Func()
as follows:
public interface ITest
{
void Func();
}
public class A : ITest
{
// Implement ITest.Func() methods
public void Func()
{
Console.Out.Wri teLine("In A.Func()...");
}
}
public class B : A
{
}
class Program
{
static void Main(string[] args)
{
B b = new B();
b.Func(); // Prints In A.Func()...
}
}
If I've misunderstood, and you actually want to redefine the
implementation of the non-virtual Func() method in class B, then you can
hide A's implementation using the 'new' keyword:
public new void Func()
{
Console.Out.Wri teLine("In B.Func()...");
}
Regards
Dave
Kevin Frey wrote:
In a derived class I am trying to redefine the implementation of a interface
method defined in a base class - the base class interface method was not
declared virtual.
I have yet to actually test this approach, but it looks ugly. Is there any
better way?
public interface ITest
{
void Func();
}
public class A : ITest
{
void ITest.Func( )
{
// some implementation
}
}
public class B : A, ITest
{
void ITest.Func( )
{
// want to call A's ITest interface
( ( ITest )( A ) this ).Func( );
}
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tron Thomas |
last post by:
Under the right compiler the following code:
class Base
{
public:
virtual void Method(int){}
};
class Derived: public Base
{
|
by: Daniel Kay |
last post by:
Hello!
I have written two template classes which implement the observerpattern
in C++. I hope I manage to desribe the problem I have.
template<class T> class Observer {
/* ... */
};
template<class T> classSubject {
|
by: Elder Hyde |
last post by:
Hey all,
A class of mine needs to tell the outside world when its buffer is not
empty. The problem is that C# seems to force you to put the
event-raising code in the base class. To illustrate, consider what I'll
do in Java:
public interface DataAvailabilityListener extends java.util.EventListener {
void dataArrived(DataAvailabilityEvent event);
}
|
by: Mark McDonald |
last post by:
This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t
understand why you can’t declare an implicit operator to convert a base class
to a derived class.
The text books say “neither the source nor the target types of a conversion
can be a base type of the other, since a conversion would then already
exist”. But this is not really true, whilst automatic (implicit) conversions
do occur from the derived...
|
by: Edward Diener |
last post by:
Since a C++ using declaration isn't allowed in MC++, is there a way to
specify that a property, method, event, or field's access can be changed in
a derived class, ie. is protected in one class and is made public in a
derived class ?
| |
by: Frans Bouma |
last post by:
Hi,
I have a serious problem with VB.NET and a DataTable derived class and I
can't figure out how to solve it. I have implemented it in C# where it
works perfectly, but I can't port one statement to VB.NET and this is
crucial: re-implementing ISerializable.GetObjectData() in the DataTable
derived class so serializing the datatable derived class will call this
method and not the DataTable version (which is private, so overriding is
also...
|
by: developereo |
last post by:
Hi folks,
Can anybody shed some light on this problem?
class Interface {
public:
Interface() { ...}
virtual ~Interface() { ...}
virtual method() = 0;
};
|
by: Naomi |
last post by:
I need to make software engineering decision to do with using a
derived data type in a container class. So for example, if I have an
Edge class, and I want to make a Edge object which contains two
NumberVertices instead of Vertices, I can either just use a non-
templated Edge class which stores pointers to my NumberVertices and
leave it up to the programmer to cast back again to NumberVertex (this
type of casting only works with...
|
by: Lothar Behrens |
last post by:
Hi,
I have created a base class implementing an interface and a derived
class also implementing
the same interface. While figuring out that I am unable to call the
derived method of an instance
or the derived class I have added the keyword Overloads to the
function.
But this does not help. Why do I get these troubles ?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |