Reputation: 113
I am trying to define interfaces for a library which will be using pimpl-idiom. Following is a typical interface class which I define.
struct A {
public:
void func1();
void func2();
virtual void notif1();
virtual void notif2();
private:
class AImpl;
AImpl *impl;
}
The functions func1(), func2() are the interface functions. And notif1(), notif2() are the notification functions which the application has to implement (in the subclass of A).
Is this the right way to define an interface for a library? Are there any disadvantages with this method or is there any better solution?
Thanks for all the answers. So from all the answers I gather that the following is a good way of representing an interface for a library.
// Forward declaration
class AImpl;
struct A {
public:
void func1();
void func2();
private:
virtual void notif1();
virtual void notif2();
AImpl *impl;
}
The interface library will implement the interface functions and application will implement the notification functions in the derived class. Is there any good example of a library which follows this pattern?
Upvotes: 1
Views: 532
Reputation: 58677
I don't think it's a good idea.
Upvotes: 3
Reputation: 734
You are not required to declare the AImpl class inside struct A.
I generally do a forward declaraion:
//Forward Declaraion.
class AImpl;
struct A {
public:
void func1();
void func2();
virtual void notif1();
virtual void notif2();
private:
AImpl *impl;
}
Upvotes: 1