C++是否有可能使一个类扩展一个类并同时成为一个接口的实现?

发布于 2024-10-12 16:07:25 字数 55 浏览 7 评论 0原文

您能提供一个简单的代码示例吗? (对不起,C++ nube)以及如何从您正在扩展的类中调用函数?

Coluld you provide a simple code example? (sorry C++ nube) and how to call a function from the class you are extending?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猥琐帝 2024-10-19 16:07:25

一个有用的例子::-)

class CImplementation
{
public:
void doFoo();
};

void CImplementation::doFoo()
{
//implementation
}

class IInterface
{
public:
virtual void foo()=0;
};

class DerivedFromImplementationAndInterface : public CImplementation, public IInterface
{
virtual void foo();
};

void DerivedFromImplementationAndInterface::foo()
{
doFoo();
}


//possible usage:
void method(IInterface& aInterface)
{
aInterface.foo();
}

void test()
{
IInterface* d = new DerivedFromImplementationAndInterface;
method(*d);
}

A bit useful example: :-)

class CImplementation
{
public:
void doFoo();
};

void CImplementation::doFoo()
{
//implementation
}

class IInterface
{
public:
virtual void foo()=0;
};

class DerivedFromImplementationAndInterface : public CImplementation, public IInterface
{
virtual void foo();
};

void DerivedFromImplementationAndInterface::foo()
{
doFoo();
}


//possible usage:
void method(IInterface& aInterface)
{
aInterface.foo();
}

void test()
{
IInterface* d = new DerivedFromImplementationAndInterface;
method(*d);
}
秋叶绚丽 2024-10-19 16:07:25

在C++中,你可以扩展多个类,这称为多重继承。这很可能就是您正在寻找的。请阅读一本关于多重继承和 C++ 的好书(快速介绍:http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc% 2Flanguage%2Fref%2Fcplr134.htm),因为有很多陷阱和细节需要注意。

多重继承的示例:

class A { ... };
class B { ... };
class C: public A, public B {};  // C inherits from A and B.

In C++, you can extend multiple classes, it's called multiple inheritance. Most probably this is what you're looking for. Please read a good book about multiple inheritance and C++ (a quick introduction: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr134.htm), because there are many pitfalls and details to pay attention to.

Example for multiple inheritance:

class A { ... };
class B { ... };
class C: public A, public B {};  // C inherits from A and B.
十级心震 2024-10-19 16:07:25

C++ 没有显式地具有接口,相当于 Java 中的接口通常使用仅具有纯虚函数(加上构造函数、析构函数、复制赋值)的类来实现:

#include <iostream>

// interface
class Fooable {
  public:
    virtual int foo() = 0;
    virtual ~Fooable() {}
};

// base class
class Base {
  public:
    void non_virtual_function() { std::cout << "Base::non_virtual_function\n"; }
    virtual void virtual_function() { std::cout << "Base::virtual_function\n"; }
};

// derived class, inherits from both Base "class" and Fooable "interface"
class Derived: public Base, public Fooable {
  public:
    virtual int foo() {
      // call base class function
      Base::non_virtual_function();
      // virtual call to function defined in base class, overridden here
      virtual_function();
    }
    virtual void virtual_function() {
        // call base class implementation of virtual function directly (rare)
        Base::virtual_function();
        std::cout << "Derived::virtual_function\n";
    }
    void non_virtual_function() {
        // not called
        std::cout << "Derived::non_virtual_function\n";
    }
};

int main() {
    Derived d;
    d.foo();
}

C++ doesn't explicitly have interfaces, the equivalent of an interface in Java is usually implemented with a class having only pure virtual functions (plus constructors, destructor, copy assignment):

#include <iostream>

// interface
class Fooable {
  public:
    virtual int foo() = 0;
    virtual ~Fooable() {}
};

// base class
class Base {
  public:
    void non_virtual_function() { std::cout << "Base::non_virtual_function\n"; }
    virtual void virtual_function() { std::cout << "Base::virtual_function\n"; }
};

// derived class, inherits from both Base "class" and Fooable "interface"
class Derived: public Base, public Fooable {
  public:
    virtual int foo() {
      // call base class function
      Base::non_virtual_function();
      // virtual call to function defined in base class, overridden here
      virtual_function();
    }
    virtual void virtual_function() {
        // call base class implementation of virtual function directly (rare)
        Base::virtual_function();
        std::cout << "Derived::virtual_function\n";
    }
    void non_virtual_function() {
        // not called
        std::cout << "Derived::non_virtual_function\n";
    }
};

int main() {
    Derived d;
    d.foo();
}
还不是爱你 2024-10-19 16:07:25

不确定您在问什么:

class A
{
  public:
    void method();
};

class B
{
  public:
    void method();
};

class C : public A, public B
{
  public:
    void callingMethod();
};

void C::callingMethod()
{
  // Here you can just call A::method() or B::method() directly.
  A::method();
  B::method();
}

请注意,多重继承可能会导致真正难以解决的问题,我建议仅在必要时使用它。

Not sure what you're asking:

class A
{
  public:
    void method();
};

class B
{
  public:
    void method();
};

class C : public A, public B
{
  public:
    void callingMethod();
};

void C::callingMethod()
{
  // Here you can just call A::method() or B::method() directly.
  A::method();
  B::method();
}

Note that multiple inheritance can lead to really hard-to-solve problems and I would recommend to only use it when necessary.

梦太阳 2024-10-19 16:07:25

如所提出的问题,

C++ 是否可以让一个类扩展一个类并实现另一个类?

没有多大意义。答案就是“是”。您可以从任意数量的类派生:C++ 完全支持多重继承。

因此,鉴于所提出的问题并没有真正的意义,您至少有可能想要

C++ 是否可以让一个类扩展一个类,从而实现另一个类?

这个问题的答案也是肯定的,但这并不是微不足道的。它涉及虚拟继承。这是相当棘手的。

下面是一个示例:

#include <iostream>

void say( char const s[] ) { std::cout << s << std::endl; }

class TalkerInterface
{
public:
    virtual void saySomething() const = 0;
};

class TalkerImpl
    : public virtual TalkerInterface
{
public:
    void saySomething() const
    {
        say( "TalkerImpl!" );
    }
};

class MyAbstractClass
    : public virtual TalkerInterface
{
public:
    void foo() const { saySomething(); }
};

class MyClass
    : public MyAbstractClass
    , public TalkerImpl
{};

int main()
{
    MyClass().foo();
}

虚拟继承确保在 MyClass 实例中只有 一个 类型为 TalkerInterface 的子对象。这会产生一些违反直觉的后果。一是“在实现中继承”有效,二是基类子对象的构造发生在每个 MyClass 构造函数中,更一般地发生在最派生的类中。

干杯&呵呵,

The question as stated,

C++ is it possible to make a class extend one class and implement another?

does not make much sense. The answer to that is just "yes". You can derive from any number of classes: C++ fully support multiple inheritance.

So, given that the question as stated isn't really meaningful, it's at least possible that you meant to ask

C++ is it possible to make a class extend one class and thereby implement another?

The answer to this question is also yes, but it's not trivial. It involves virtual inheritance. Which is quite tricky.

Here's an example:

#include <iostream>

void say( char const s[] ) { std::cout << s << std::endl; }

class TalkerInterface
{
public:
    virtual void saySomething() const = 0;
};

class TalkerImpl
    : public virtual TalkerInterface
{
public:
    void saySomething() const
    {
        say( "TalkerImpl!" );
    }
};

class MyAbstractClass
    : public virtual TalkerInterface
{
public:
    void foo() const { saySomething(); }
};

class MyClass
    : public MyAbstractClass
    , public TalkerImpl
{};

int main()
{
    MyClass().foo();
}

The virtual inheritance ensures that there is only one sub-object of type TalkerInterface in a MyClass instance. This has some counter-intuitive consequences. One is that "inheriting in an implementation" works, and another is that construction of that base class sub-object happens down in each MyClass constructor, and more generally down in the most derived class.

Cheers & hth.,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文