C++ 你决不会违背虚拟功能的目的。Gohan-如果u interface在这里起到了作用,我会感到惊讶。@Len这是不同的,如果您想直接将实现部分移出,u interface关键字将使其工作,并且使用仅包含纯虚拟函数的标准类或结构将从编译器中出错。@potato
C++ 你决不会违背虚拟功能的目的。Gohan-如果u interface在这里起到了作用,我会感到惊讶。@Len这是不同的,如果您想直接将实现部分移出,u interface关键字将使其工作,并且使用仅包含纯虚拟函数的标准类或结构将从编译器中出错。@potato,c++,visual-c++,gcc,multiple-inheritance,C++,Visual C++,Gcc,Multiple Inheritance,你决不会违背虚拟功能的目的。Gohan-如果u interface在这里起到了作用,我会感到惊讶。@Len这是不同的,如果您想直接将实现部分移出,u interface关键字将使其工作,并且使用仅包含纯虚拟函数的标准类或结构将从编译器中出错。@potatosatter“您只是在复制非虚拟语义。”“复制非虚拟语义”到底是什么意思?那么,Interface1::Name是一个不合格的名称吗?你思考和键入的速度非常快;v) +1你提供了提案的来源,真是太好了,虽然不太确定,但你为什么不把提案本身的内容
你决不会违背虚拟功能的目的。Gohan-如果u interface在这里起到了作用,我会感到惊讶。@Len这是不同的,如果您想直接将实现部分移出,u interface关键字将使其工作,并且使用仅包含纯虚拟函数的标准类或结构将从编译器中出错。@potatosatter“您只是在复制非虚拟语义。”“复制非虚拟语义”到底是什么意思?那么,
Interface1::Name
是一个不合格的名称吗?你思考和键入的速度非常快;v) +1你提供了提案的来源,真是太好了,虽然不太确定,但你为什么不把提案本身的内容作为一个“有趣的事实”包括在实际答案中,至少是这么简单。为了充实自己,我不得不在网上找这本书。读起来很有趣。对于未来的读者:被拒绝的提案引入了以下语法来“重命名”已实现接口的方法:virtualvoidname1()=Interface1::Name代码>Cheers@Tomalla对于未来的C++标准来说,这将是一个极好的补充。不需要使用编译器来查看名称(2)
不能是递归调用,因为此函数Name
不带参数!它不是一个递归调用。Name()的实际实现调用一个包含Id的版本,这样您就可以知道在基本对象中调用了哪个版本的Name()。然后将helper对象传递给碰巧需要实际接口的人。但正如所写的,它只能是一个递归调用。例如,您可以使用scope resolution操作符调用另一个Name
函数。
#include <cstdio>
class Interface1{
public:
virtual void Name() = 0;
};
class Interface2
{
public:
virtual void Name() = 0;
};
class RealClass: public Interface1, public Interface2
{
public:
virtual void Interface1::Name()
{
printf("Interface1 OK?\n");
}
virtual void Interface2::Name()
{
printf("Interface2 OK?\n");
}
};
int main()
{
Interface1 *p = new RealClass();
p->Name();
Interface2 *q = reinterpret_cast<RealClass*>(p);
q->Name();
}
#include <cstdio>
__interface Interface1{
virtual void Name() = 0;
};
__interface Interface2
{
virtual void Name() = 0;
};
class RealClass: public Interface1,
public Interface2
{
public:
virtual void Interface1::Name();
virtual void Interface2::Name();
};
void RealClass::Interface1::Name()
{
printf("Interface1 OK?\n");
}
void RealClass::Interface2::Name()
{
printf("Interface2 OK?\n");
}
int main()
{
Interface1 *p = new RealClass();
p->Name();
Interface2 *q = reinterpret_cast<RealClass*>(p);
q->Name();
}
struct Interface1 {
virtual void Name() = 0;
};
struct Interface2 {
virtual void Name() = 0;
};
struct RealClass : Interface1, Interface2 {
virtual void Name();
};
// and move it out of the class definition just like any other method:
void RealClass::Name() {
printf("Interface1 OK?\n");
printf("Interface2 OK?\n");
}
struct RealClass1 : Interface1 {
virtual void Name() {
printf("Interface1 OK?\n");
}
};
struct RealClass2 : Interface2 {
virtual void Name() {
printf("Interface2 OK?\n");
}
};
struct RealClass : RealClass1, RealClass2 {
virtual void Name() {
// you must still decide what to do here, which is likely calling both:
RealClass1::Name();
RealClass2::Name();
// or doing something else entirely
// but note: this is the function which will be called in all cases
// of *virtual dispatch* (for instances of this class), as it is the
// final overrider, the above separate definition is merely
// code-organization convenience
}
};
int main() {
RealClass rc; // no need for dynamic allocation in this example
Interface1& one = rc;
one.Name();
Interface2& two = dynamic_cast<Interface2&>(one);
two.Name();
return 0;
}
template<class Derived>
struct RealClass1 : Interface1 {
#define self (*static_cast<Derived*>(this))
virtual void Name() {
printf("Interface1 for %s\n", self.name.c_str());
}
#undef self
};
template<class Derived>
struct RealClass2 : Interface2 {
#define self (*static_cast<Derived*>(this))
virtual void Name() {
printf("Interface2 for %s\n", self.name.c_str());
}
#undef self
};
struct RealClass : RealClass1<RealClass>, RealClass2<RealClass> {
std::string name;
RealClass() : name("real code would have members you need to access") {}
};
template<class id>
class InterfaceHelper : public MyInterface
{
public :
virtual void Name()
{
Name(id);
}
virtual void Name(
const size_t id) = 0;
}
class InterfaceHelperBase
{
public :
virtual void Name(
const size_t id) = 0;
}
class InterfaceHelper1 : public MyInterface, protected InterfaceHelperBase
{
public :
using InterfaceHelperBase::Name;
virtual void Name()
{
Name(1);
}
}
class InterfaceHelper2 : public MyInterface, protected InterfaceHelperBase
{
public :
using InterfaceHelperBase::Name;
virtual void Name()
{
Name(2);
}
}
class MyClass : public InterfaceHelper1, public InterfaceHelper2
{
public :
virtual void Name(
const size_t id)
{
if (id == 1)
{
printf("Interface 1 OK?");
}
else if (id == 2)
{
printf("Interface 2 OK?");
}
}
}
class Interface1{
public:
virtual void Name() = 0;
};
class Interface2{
public:
virtual void Name() = 0;
};
class Interface1_helper : public Interface1{
public:
virtual void I1_Name() = 0;
void Name() override
{
I1_Name();
}
};
class Interface2_helper : public Interface2{
public:
virtual void I2_Name() = 0;
void Name() override
{
I2_Name();
}
};
class RealClass: public Interface1_helper, public Interface2_helper{
public:
void I1_Name() override
{
printf("Interface1 OK?\n");
}
void I2_Name() override
{
printf("Interface2 OK?\n");
}
};
int main()
{
RealClass rc;
Interface1* i1 = &rc;
Interface2* i2 = &rc;
i1->Name();
i2->Name();
rc.I1_Name();
rc.I2_Name();
}
class BaseX
{
public:
virtual void fun()
{
cout << "BaseX::fun\n";
}
};
class BaseY
{
public:
virtual void fun()
{
cout << "BaseY::fun\n";
}
};
class DerivedX : protected BaseX
{
public:
virtual void funX()
{
BaseX::fun();
}
};
class DerivedY : protected BaseY
{
public:
virtual void funY()
{
BaseY::fun();
}
};
class DerivedXY : public DerivedX, public DerivedY
{
};