-1

Please consider the code below:

class IMyInterface
{
private:
    IMyInterface();
    ~IMyInterface();
public:
    virtual void func1();
    virtual void func2(const SerialPortParameters);
    virtual int func3(unsigned char *, int bufferSize, int);
};

Is that Ok for declaring an interface in c++? I put the constructor in private section so nobody could create an object from it and all methods are virtual. Is it a standard way of building interfaces in c++?

And It has only a header file. As it is an interface there is no implementation so I think there is no need to create a cpp file. Am I right?

Thanks for your help.

a.toraby
  • 3,232
  • 5
  • 41
  • 73
  • 1
    Did you try to write classes that implement this interface? Did it work out? – Mat Mar 08 '15 at 09:35
  • It's pretty obvious that you didn't even try to use that definition. Asking questions here without the least attempt to solve them is a pretty lazy way of getting one's homework done. – Ulrich Eckhardt Mar 08 '15 at 09:44

3 Answers3

2

Is that Ok for declaring an interface in c++? ... Is it a standard way of building interfaces in c++?

No, it isn't. You should fix several things

class IMyInterface
{
public:
    virtual ~IMyInterface() {}
    virtual void func1() = 0;
    virtual void func2(const SerialPortParameters) = 0;
    virtual int func3(unsigned char *, int bufferSize, int) = 0;
};
  • You don't need a constructor at all
  • Make a empty public virtual destructor (a private constructor/destructor is a bad idea, since it prevents inheritance)
  • Make the interface functions pure virtual
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • @πάνταῥεῖ Could you possibly answer the second question too? About cpp file. Do I need to create cpp file? – a.toraby Mar 08 '15 at 09:44
  • 1
    @a.toraby If you make the functions pure virtual as shown ( `= 0;`), you don't need an implementation file, otherwise you would need one. The destructor in my sample is implemented inline. – πάντα ῥεῖ Mar 08 '15 at 09:45
2

This will not work, for two reasons:

  • Making the constructor private ensures that nobody would be able to instantiate your interface. Not even the derived class.
  • You did not make the destructor virtual - a must for an interface.

Rather than making your constructor private, mark all functions pure virtual, except for the destructor.

In addition, make sure that your classes inherit from your "interface" using public virtual. Otherwise, some of the classes deep in the hierarchy might end up inheriting the same members through multiple paths.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • You don't need a virtual destructor, though it is useful in many cases. Making it protected will avoid the false, non-polymorphic delete, too. – Ulrich Eckhardt Mar 08 '15 at 09:41
  • 1
    @UlrichEckhardt If one plans to use a class in a way similar to Java/C# interface, it's a near certainty that the destruction would be performed through the interface-typed pointer. Lack of `virtual` at that level would make it hard to do proper clean-up in situations like that. – Sergey Kalinichenko Mar 08 '15 at 09:48
  • Smart pointers can solve this and who uses raw pointers anyway? Still, I disagree, being allowed to use an object does not give you the right to destroy it, so those things are different. Yes, the often occur together, I'm not questioning it, but it's not "a must for an interface". – Ulrich Eckhardt Mar 08 '15 at 09:52
1

You don't need a constructor at all as an interface will not be instantiated. You should also make those methods (except destructor) pure virtual in following way. Making those function pure vitual, you are creating an abstract class, which can't be instantiated.

public:
    virtual ~IMyInterface(){}
    virtual void func1()=0;
    virtual void func2(const SerialPortParameters)=0;
    virtual int func3(unsigned char *, int bufferSize, int)=0;
Surajeet Bharati
  • 1,363
  • 1
  • 18
  • 36
  • So in this way the derived class can implement the destructor and must implement func1, func2 and func3. Am I right? – a.toraby Mar 08 '15 at 10:04