Define an interface hierarchy?

  • Thread starter Anton Bredikhin
  • Start date
A

Anton Bredikhin

Hello!

I have a class hierarchy that consists mainly of one base class
CtxBase, and two children: LoginCtx and DisconnectCtx.

Now, there's a need to provide an interfaces for this hierarchy. I'd
like to export this classes from a DLL.
And I'm in doubt how can I achieve this?

My first option was to define a base interface (ICtxBase) and then
derive both ILoginCtx and IDisconnectCtx from it. But using this
approach I get ambiguous calls inside derived classes, cause the
inherit from both their specific interfaces and the base classs,
making the base interface ICtxBase to appear in the hierarchy twice.

Can anyone tell me, is there a standard approach for such situation?

Thanks!
 
S

Salt_Peter

Hello!

I have a class hierarchy that consists mainly of one base class
CtxBase, and two children: LoginCtx and DisconnectCtx.

Now, there's a need to provide an interfaces for this hierarchy. I'd
like to export this classes from a DLL.
And I'm in doubt how can I achieve this?

My first option was to define a base interface (ICtxBase) and then
derive both ILoginCtx and IDisconnectCtx from it. But using this
approach I get ambiguous calls inside derived classes, cause the
inherit from both their specific interfaces and the base classs,
making the base interface ICtxBase to appear in the hierarchy twice.

Why would you inherit from an interface to blueprint your derived
classes?
Ask yourself if DisconnectCtx is_a interface?

Sshow a simple reconstruction of the problem you have.
How difficult could that be?

#include <iostream>

class ICtx
{
public:
virtual void foo() const { std::cout << "ICtx::foo()\n"; }
};

class ILoginCtx : public ICtx { };

class Ctx
{
public:
virtual void login(const ILoginCtx& ri) = 0;
};

void Ctx::login(const ILoginCtx& ri)
{
std::cout << "Ctx::login()\n";
ri.foo();
}

class LoginCtx : public Ctx
{
public:
virtual void login(const ILoginCtx& ri)
{
std::cout << "LoginCtx::login()\n";
Ctx::login(ri);
}
};

int main()
{
LoginCtx ctx;
ILoginCtx interface;
ctx.login( interface );
}
Can anyone tell me, is there a standard approach for such situation?

Thanks!

First thought that comes to mind is that you have a potential design
issue. One class appears to be responsible for Logins and another for
Disconnects? Wouldn't it make more sense that a SessionCtx class and
its derivatives handle both connects and disconnects, perhaps each
derivative does that in a different way?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top