Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么不能用';使用';指令?_C++_Interface_Using Directives - Fatal编程技术网

C++ 为什么不能用';使用';指令?

C++ 为什么不能用';使用';指令?,c++,interface,using-directives,C++,Interface,Using Directives,可能重复: 方法。为什么不可能这样做?在C++03中还有其他方法吗?使用只会将名称带入范围 它没有实现任何功能 如果希望通过继承实现类似Java的get,则必须显式添加与此相关的开销,即virtual继承,如下所示: #include <iostream> class Interface { public: virtual void yell() = 0; }; class Implementation : public virtual Interface {

可能重复:


方法。为什么不可能这样做?在C++03中还有其他方法吗?

使用
只会将名称带入范围

它没有实现任何功能

如果希望通过继承实现类似Java的get,则必须显式添加与此相关的开销,即
virtual
继承,如下所示:

#include <iostream>

class Interface
{
public:
    virtual void yell() = 0;
};

class Implementation
    : public virtual Interface
{
public:
    void yell()
    {
        std::cout << "hello world!" << std::endl;
    }
};

class Test: private Implementation, public virtual Interface
{
public:
    using Implementation::yell;
};

int main ()
{
    Test t;
    t.yell();
}
#包括
类接口
{
公众:
虚空yell()=0;
};
类实现
:公共虚拟接口
{
公众:
void yell()
{

是的,但是因为这个问题有很多不正确的答案(只是默认OP的断言是正确的)将另一个问题作为这个问题的重复投票会更好。谢谢你的回答。考虑到仅wtfpm,你认为使用虚拟继承更好吗,还是我应该让层次结构与公共继承线性化?我真正的层次结构是相同的,只是类更多complex@Dadam:一般规则一让我们先纠正错误,然后再快速(如果必要的话)。因此,一般的指导原则一直是从接口类中虚拟继承。这也有助于处理异常。在这种情况下,速度真的不是问题,我只是想让它可读(你知道,不是吗?).呵呵,我有,但不是那样acronym@Cheersandhth.-Alf-需要使用
,因为
实现的私有继承
,接口的公共继承
。接口的公共继承
要求您必须提供
yell()
的可见性为
public
以符合非抽象类的资格。您从
实现继承
yell()
,但没有使用
,它的可见性为
private
void Test::yell(void) { Implementation::yell(); }
#include <iostream>

class Interface
{
public:
    virtual void yell() = 0;
};

class Implementation
    : public virtual Interface
{
public:
    void yell()
    {
        std::cout << "hello world!" << std::endl;
    }
};

class Test: private Implementation, public virtual Interface
{
public:
    using Implementation::yell;
};

int main ()
{
    Test t;
    t.yell();
}