Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing interfaces in C++

I usually program in C# but am trying to do a bit of C++ and am struggling somewhat trying to implement interfaces in C++.

In C# I'd do something like this:

class Base<T>
{
    public void DoSomething(T value)
    {
        // Do something here
    }
}

interface IDoubleDoSomething
{
    void DoSomething(double value);
}

class Foo : Base<double>, IDoubleDoSomething
{
}

In C++ I've implemented it like this:

template <class T>
class Base
{
public:
    virtual void DoSomething(T value)
    {
        // Do something here
    }
};

class IDoubleDoSomething
{
public:
    virtual void DoSomething(double value) = 0;
};

class Foo : public Base<double>, public IDoubleDoSomething
{
};

The problem is that I cannot instantiate Foo because it is abstract (doesn't implement DoSomething). I realise I can implement DoSomething and just call the method on Base but I was hoping there is a better way of doing this. I have other classes which inherit from base with different data types and I have other classes which inherit from IDoubleDoSomething which don't use Base.

Any help appreciated.

like image 889
Clueless Avatar asked Jan 03 '12 16:01

Clueless


People also ask

What are interfaces in C?

What Does Interface Mean? Interface, in C#, is a code structure that defines a contract between an object and its user. It contains a collection of semantically similar properties and methods that can be implemented by a class or a struct that adheres to the contract.

What is implementation in C?

Basically, an implementation is the combination of a compiler and the C library it supports. – Jonathan Leffler. Apr 15, 2019 at 14:20. 4. C language = what you type into a text editor; implementation = the thing that actually does something with what you typed.

What is the purpose of implementing an interface?

The purpose of interfaces is to allow the computer to enforce these properties and to know that an object of TYPE T (whatever the interface is ) must have functions called X,Y,Z, etc.

What is the difference between interface and implementation in C?

An interface is an empty shell, there are only the signatures of the methods, which implies that the methods do not have a body. The interface can't do anything. It's just a pattern. The implementation is the actual substance behind the idea, the actual definition of how the interface will do what we expect it to.

Does C have interface?

Notes on Interfaces:Interface methods do not have a body - the body is provided by the "implement" class. On implementation of an interface, you must override all of its methods. Interfaces can contain properties and methods, but not fields/variables. Interface members are by default abstract and public.


2 Answers

Consider the following c++ code

class Foo
{
public:
    void DoSomething1(){}
};

template<typename t>
void MethodExpectsDosomething1( t f )
{
    f.DoSomething1();
}

template<typename t>
void MethodExpectsDosomething2( t f )
{
    f.DoSomething2();
}

int main()
{
    Foo f;
    MethodExpectsDosomething1<Foo>( f );

    MethodExpectsDosomething2<Foo>( f );

    return 0;
}

In C++ you can use Foo without it implementing a IDoSomething1 and IDoSomething2. The second method MethodExpectsDosomething2 will simply fail to compile as Foo doesn't have the DoSomething2 method.

In C# such construct is not possible and forces you to have IDoSomething1 and IDoSomething2 interface and specify that as a type constraint.

So maybe you need to look at your code and see if such interfaces are needed at all ?

like image 144
parapura rajkumar Avatar answered Oct 16 '22 14:10

parapura rajkumar


The thing is, Base::DoSomething and IWhatever::DoSomething are two unrelated functions (even if there weren't any pure virtual functions in this, you wouldn't be able to call DoSomething on a Foo object, anyway). Base needs to inherit from IWhatever for this to work.

That said, ask yourself if you actually need this. Generic programming with templates (and concepts, which are sort of like interfaces — see Boost.ConceptCheck) is usually a better solution in C++ than runtime subtype polymorphism.

like image 25
Cat Plus Plus Avatar answered Oct 16 '22 13:10

Cat Plus Plus