Reputation: 113976
Say I have a C# interface called IMyInterface
defined as follows:
// C# code
public interface IMyInterface
{
string MyProperty { get; }
}
Assume I also have a C++/CLI class, MyConcreteClass
, that implements this interface and whose header is declared as follows:
public ref class MyConcreteClass : public IMyInterface
{
public:
virtual property String^ MyProperty
{
String^ get() sealed { return String::Empty; };
void set( String^ s ) sealed { };
}
};
Obviously when you access a virtual member through the interface, the runtime must look for an implementation in the class, and will be slower than if the member was not virtual.
IMyInterface obj;
obj->MyProperty = "HELLO";
I'm asking specifically about the performance of using virtual members directly on the concrete object type. Is this slower if MyProperty
was a virtual member?
MyConcreteClass obj;
obj->MyProperty = "HELLO";
Upvotes: 1
Views: 795
Reputation: 13420
Virtual methods are slower because the runtime has to check for an actual implementation of the method. So it is 1 additional check. You can still hundreds of thousands of these per second. So don't stress about it. In Java every method is virtual by default.
UPDATE: I am not sure about how introducing c++ changes things. My guess is that is would be similar because you are still accessing a virtual method. I am not sure how it would change it. But once again this is just my guess. Hopefully someone else can help out more.
Upvotes: 4