c++类之外的虚拟函数实现

时间:2022-09-08 16:55:04

I am new to C++. While trying sample polymorphism code, I found that base class virtual function definition in derived class is possible only when defined within the derived class or outside with declaration in derived class.

我是c++的新手。在尝试示例多态性代码时,我发现派生类中的基类虚函数定义只有在派生类中定义或在派生类中定义声明时才可能实现。

Following code gives error:

以下代码给出错误:

class B
{
public:
    virtual void f();
};

void B::f() {
    std::cout<<"B::f";
}

class D : public B
{
public:
    void f2() {int b;}
};

// error: no "void D::f()" member function declared in class "D"
void D::f() {
    std::cout<<"D::F";
}

It works if I declare f() inside D. I was wondering why do I need to explicitly declare the function again when it is already declared in Base class. The compiler can get the signature from Base class right?

如果我在d中声明f(),它就会工作。编译器可以从基类中获取签名,对吗?

Thanks in advance..

提前谢谢. .

3 个解决方案

#1


9  

You can't add members to a class outside of the class definition. If you want D to have an override for B::f then you have to declare it inside the class definition. Those are the rules.

不能将成员添加到类定义之外的类。如果您想要对B进行重写::f,那么您必须在类定义中声明它。这些规则。

Declaring a member in a base class doesn't automatically give derived classes an identical member. Inheriting from the base gives the derived class all the members of the base class so you can choose whether to override, hide or add to the base classes members but you have to indicate a choice to override in the class definition by declaring the overriding function.

在基类中声明一个成员不会自动给派生类一个相同的成员。从基中继承派生类,它提供了基类的所有成员,因此您可以选择是否覆盖、隐藏或添加到基类成员,但您必须指出在类定义中通过声明重写函数来重写这个选项。

#2


3  

Even though D derives from B and therefore you can call f() on an instance of D, it does not mean you do not need to put the declaration into the header.

尽管D源于B,因此可以在D的实例上调用f(),但这并不意味着不需要将声明放入header中。

Any function you implement must be explicitly declared in the header.

您实现的任何函数必须在header中显式地声明。

You do not need, however, to put its implementation in there. Just

然而,您不需要将其实现放在那里。只是

class D : public B
{
public:
   /*virtual*/ void f();
};

and you can optionally choose whether to include the word "virtual" here

您还可以选择是否在这里包含“virtual”这个词

#3


1  

In C++, your class definition tells the compiler which functions the class implements. So if you want to write a function "D::f()", you must have f() in the class definition for D.

在c++中,类定义告诉编译器类实现了哪些函数。如果你想写一个函数D::f(),在D的类定义中必须有f()。

The fact that function "B::f()" has been defined in the base class is irrelevant. Each class definition must explicitly declare the functions that it implements.

函数“B: f()”在基类中定义的事实是不相关的。每个类定义必须显式声明它实现的函数。

#1


9  

You can't add members to a class outside of the class definition. If you want D to have an override for B::f then you have to declare it inside the class definition. Those are the rules.

不能将成员添加到类定义之外的类。如果您想要对B进行重写::f,那么您必须在类定义中声明它。这些规则。

Declaring a member in a base class doesn't automatically give derived classes an identical member. Inheriting from the base gives the derived class all the members of the base class so you can choose whether to override, hide or add to the base classes members but you have to indicate a choice to override in the class definition by declaring the overriding function.

在基类中声明一个成员不会自动给派生类一个相同的成员。从基中继承派生类,它提供了基类的所有成员,因此您可以选择是否覆盖、隐藏或添加到基类成员,但您必须指出在类定义中通过声明重写函数来重写这个选项。

#2


3  

Even though D derives from B and therefore you can call f() on an instance of D, it does not mean you do not need to put the declaration into the header.

尽管D源于B,因此可以在D的实例上调用f(),但这并不意味着不需要将声明放入header中。

Any function you implement must be explicitly declared in the header.

您实现的任何函数必须在header中显式地声明。

You do not need, however, to put its implementation in there. Just

然而,您不需要将其实现放在那里。只是

class D : public B
{
public:
   /*virtual*/ void f();
};

and you can optionally choose whether to include the word "virtual" here

您还可以选择是否在这里包含“virtual”这个词

#3


1  

In C++, your class definition tells the compiler which functions the class implements. So if you want to write a function "D::f()", you must have f() in the class definition for D.

在c++中,类定义告诉编译器类实现了哪些函数。如果你想写一个函数D::f(),在D的类定义中必须有f()。

The fact that function "B::f()" has been defined in the base class is irrelevant. Each class definition must explicitly declare the functions that it implements.

函数“B: f()”在基类中定义的事实是不相关的。每个类定义必须显式声明它实现的函数。