Question

I'm using class to declare interface. I just want to define method signature. This method must be implemented in any non-abstract subclass. I don't need method to be virtual. This is default behaviour in C# BTW (i came from C#/Java world)

However it seems in C++ it is not possible. I either declare method in regular way

void Foo::Method()

and then it is not mandatory to implement it or declare method as "pure virtual"

void virtual Foo::Method() = 0;

and then method become virtual, but I want to avoid this to save performance a little bit.

It seems I want to have something like that

void Foo::Method() = 0;

but that would be compilation error

Was it helpful?

Solution

if you're planning on using the derived class from template code, i.e. compile time polymorphism, then you only need to document the expected signature

the code using a derived class simply won't compile and link if the used function isn't implemented

otherwise, for runtime polymorphism it needs to be virtual, or else it won't be called

OTHER TIPS

I believe that you might be confused with regard to how C# version works:

  class A {
    public void NonVirt() { Console.Out.WriteLine("A:NonVirt"); }
    public virtual void Virt() { Console.Out.WriteLine("A:Virt"); }
  }

  class B : A {
    public void NonVirt() { Console.Out.WriteLine("B:NonVirt"); }
    public override void Virt() { Console.Out.WriteLine("B:Virt"); }
  }

  class Program {
    static void Main(string[] args) {
      A x = new B();
      x.NonVirt();
      x.Virt();
    }
  }

This will output

A:NonVirt 
B:Virt

So even in C#, you need to make method virtual if you want to call the derived implementation.

If method must be implemented in all non-abstract subclasses this means that you need to call them through base class pointer. This in turn means that you need to make them virtual, same as in C# (and likely in Java, but I am not sure)

Btw, price of virtual call is a few nanoseconds on modern CPUs, so I am not sure if it is worth it but lets say that it is.

If you want to avoid the cost of virtual call, you should use compile time polymorphism via templates

There is no notion of interface in C++. The only way to achieve your goal is to create a base class defining as virtual and = 0 all the methods which must be actually defined in subclasses.

class IBase {
      // ...
        virtual void f1() = 0;
      // ....
}

That class will be virtual pure if all methods are defined like f1, which is the closest to an interface you can get.

The concept of interface in Java is a bit like a contract with regard to classes implementing it. The compiler enforces the constraints of the contract by checking the content of the implementors. This notion of contract or explicit structural subtyping does not exist formally in C++.

However, you can manually verify that such constraints are respected by defining a template wich will expect as a parameter a class with the defined methods or attributes, and using that template on the classes to be verified. This could be considered a form of unit testing I suppose.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top