c语言中,覆盖虚拟函数与隐藏非虚拟函数有何本质不同?

2026-04-16 22:123阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1627个文字,预计阅读时间需要7分钟。

c语言中,覆盖虚拟函数与隐藏非虚拟函数有何本质不同?

给定的代码片段和函数调用有何不同?什么是隐藏功能?什么是功能重写?它们如何与函数加载相关?两者有什么区别?

在一个代码片段中,函数调用和代码片段本身通常有以下几个方面的不同:

1. 代码片段:通常是指一段独立存在的代码,它可以包含多个函数、变量等。代码片段可以单独运行或作为更大程序的一部分。

2. 函数调用:是指代码片段中的某个函数被调用的过程。函数调用通常是为了执行特定的功能,并可能返回结果。

关于隐藏功能、功能重写和它们与函数加载的关系:

- 隐藏功能:通常指代码中未被明确声明或文档化的功能。这些功能可能因为实现复杂、不常用或开发者有意隐藏而未被公开。

- 功能重写:指的是对原有功能进行修改或重构,以优化性能、增加新特性或修复错误。重写可能涉及函数的完全替换或部分修改。

- 与函数加载相关:函数加载通常指的是在程序运行时,将函数从磁盘加载到内存中的过程。隐藏功能和功能重写可能与函数加载相关,因为它们可能涉及修改或替换程序中的函数。

两者区别:

- 隐藏功能通常指的是未被明确提及的功能,而功能重写则是指对现有功能的修改或重构。

- 隐藏功能可能是有意为之,而功能重写则可能是出于优化或修复的考虑。

由于您提到在一个地方找不到这些概念的好描述,以下是一个简要的说明:

1. 隐藏功能:在代码中可能存在未被明确说明的功能,这些功能可能通过特定的函数调用或代码路径实现。

2. 功能重写:对现有功能进行修改或重构,可能涉及函数替换、优化或增加新功能。

3. 函数加载:程序运行时将函数从磁盘加载到内存中的过程,与隐藏功能和功能重写可能相关。

希望这个简短的描述对您有所帮助。如果您需要更详细的解释或有特定的问题,请随时提出。

给定以下代码片段,函数调用有何不同?什么是隐藏功能?什么是功能重写?它们如何与函数重载相关?两者有什么区别?我在一个地方找不到这些的好描述,所以我在这里问我所以我可以巩固这些信息.

class Parent { public: void doA() { cout << "doA in Parent" << endl; } virtual void doB() { cout << "doB in Parent" << endl; } }; class Child : public Parent { public: void doA() { cout << "doA in Child" << endl; } void doB() { cout << "doB in Child" << endl; } }; Parent* p1 = new Parent(); Parent* p2 = new Child(); Child* cp = new Child(); void testStuff() { p1->doA(); p2->doA(); cp->doA(); p1->doB(); p2->doB(); cp->doB(); } 什么是隐藏功能?

……是隐藏名称的一种形式.一个简单的例子:

c语言中,覆盖虚拟函数与隐藏非虚拟函数有何本质不同?

void foo(int); namespace X { void foo(); void bar() { foo(42); // will not find `::foo` // because `X::foo` hides it } }

这也适用于基类中的名称查找:

class Base { public: void foo(int); }; class Derived : public Base { public: void foo(); void bar() { foo(42); // will not find `Base::foo` // because `Derived::foo` hides it } };

什么是功能重写?

这与虚函数的概念有关. [class.virtual] / 2

If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list, cv-qualification, and ref-qualifier (or absence of same) as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf.

class Base { private: virtual void vf(int) const &&; virtual void vf2(int); virtual Base* vf3(int); }; class Derived : public Base { public: // accessibility doesn't matter! void vf(int) const &&; // overrides `Base::vf(int) const &&` void vf2(/*int*/); // does NOT override `Base::vf2` Derived* vf3(int); // DOES override `Base::vf3` (covariant return type) };

调用虚函数时,最终的覆盖变得相关:[class.virtual] / 2

A virtual member function C::vf of a class object S is a final overrider unless the most derived class of which S is a base class subobject (if any) declares or inherits another member function that overrides vf.

即如果你有一个S类型的对象,那么最终的覆盖是你在遍历S的类层次结构回到它的基类时看到的第一个覆盖.重要的是,函数调用表达式的动态类型用于确定最终的覆盖:

Base* p = new Derived; p -> vf(); // dynamic type of `*p` is `Derived` Base& b = *p; b . vf(); // dynamic type of `b` is `Derived`

覆盖和隐藏有什么区别?

实质上,基类中的函数总是被派生类中的同名函数隐藏;无论派生类中的函数是否覆盖基类的虚函数:

class Base { private: virtual void vf(int); virtual void vf2(int); }; class Derived : public Base { public: void vf(); // doesn't override, but hides `Base::vf(int)` void vf2(int); // overrides and hides `Base::vf2(int)` };

要查找函数名称,请使用表达式的静态类型:

Derived d; d.vf(42); // `vf` is found as `Derived::vf()`, this call is ill-formed // (too many arguments)

它们如何与函数重载相关?

由于“函数隐藏”是一种名称隐藏形式,如果隐藏了函数的名称,则所有重载都会受到影响:

class Base { private: virtual void vf(int); virtual void vf(double); }; class Derived : public Base { public: void vf(); // hides `Base::vf(int)` and `Base::vf(double)` };

对于函数重写,只覆盖具有相同参数的基类中的函数;你当然可以重载一个虚函数:

class Base { private: virtual void vf(int); virtual void vf(double); void vf(char); // will be hidden by overrides in a derived class }; class Derived : public Base { public: void vf(int); // overrides `Base::vf(int)` void vf(double); // overrides `Base::vf(double)` };

本文共计1627个文字,预计阅读时间需要7分钟。

c语言中,覆盖虚拟函数与隐藏非虚拟函数有何本质不同?

给定的代码片段和函数调用有何不同?什么是隐藏功能?什么是功能重写?它们如何与函数加载相关?两者有什么区别?

在一个代码片段中,函数调用和代码片段本身通常有以下几个方面的不同:

1. 代码片段:通常是指一段独立存在的代码,它可以包含多个函数、变量等。代码片段可以单独运行或作为更大程序的一部分。

2. 函数调用:是指代码片段中的某个函数被调用的过程。函数调用通常是为了执行特定的功能,并可能返回结果。

关于隐藏功能、功能重写和它们与函数加载的关系:

- 隐藏功能:通常指代码中未被明确声明或文档化的功能。这些功能可能因为实现复杂、不常用或开发者有意隐藏而未被公开。

- 功能重写:指的是对原有功能进行修改或重构,以优化性能、增加新特性或修复错误。重写可能涉及函数的完全替换或部分修改。

- 与函数加载相关:函数加载通常指的是在程序运行时,将函数从磁盘加载到内存中的过程。隐藏功能和功能重写可能与函数加载相关,因为它们可能涉及修改或替换程序中的函数。

两者区别:

- 隐藏功能通常指的是未被明确提及的功能,而功能重写则是指对现有功能的修改或重构。

- 隐藏功能可能是有意为之,而功能重写则可能是出于优化或修复的考虑。

由于您提到在一个地方找不到这些概念的好描述,以下是一个简要的说明:

1. 隐藏功能:在代码中可能存在未被明确说明的功能,这些功能可能通过特定的函数调用或代码路径实现。

2. 功能重写:对现有功能进行修改或重构,可能涉及函数替换、优化或增加新功能。

3. 函数加载:程序运行时将函数从磁盘加载到内存中的过程,与隐藏功能和功能重写可能相关。

希望这个简短的描述对您有所帮助。如果您需要更详细的解释或有特定的问题,请随时提出。

给定以下代码片段,函数调用有何不同?什么是隐藏功能?什么是功能重写?它们如何与函数重载相关?两者有什么区别?我在一个地方找不到这些的好描述,所以我在这里问我所以我可以巩固这些信息.

class Parent { public: void doA() { cout << "doA in Parent" << endl; } virtual void doB() { cout << "doB in Parent" << endl; } }; class Child : public Parent { public: void doA() { cout << "doA in Child" << endl; } void doB() { cout << "doB in Child" << endl; } }; Parent* p1 = new Parent(); Parent* p2 = new Child(); Child* cp = new Child(); void testStuff() { p1->doA(); p2->doA(); cp->doA(); p1->doB(); p2->doB(); cp->doB(); } 什么是隐藏功能?

……是隐藏名称的一种形式.一个简单的例子:

c语言中,覆盖虚拟函数与隐藏非虚拟函数有何本质不同?

void foo(int); namespace X { void foo(); void bar() { foo(42); // will not find `::foo` // because `X::foo` hides it } }

这也适用于基类中的名称查找:

class Base { public: void foo(int); }; class Derived : public Base { public: void foo(); void bar() { foo(42); // will not find `Base::foo` // because `Derived::foo` hides it } };

什么是功能重写?

这与虚函数的概念有关. [class.virtual] / 2

If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list, cv-qualification, and ref-qualifier (or absence of same) as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf.

class Base { private: virtual void vf(int) const &&; virtual void vf2(int); virtual Base* vf3(int); }; class Derived : public Base { public: // accessibility doesn't matter! void vf(int) const &&; // overrides `Base::vf(int) const &&` void vf2(/*int*/); // does NOT override `Base::vf2` Derived* vf3(int); // DOES override `Base::vf3` (covariant return type) };

调用虚函数时,最终的覆盖变得相关:[class.virtual] / 2

A virtual member function C::vf of a class object S is a final overrider unless the most derived class of which S is a base class subobject (if any) declares or inherits another member function that overrides vf.

即如果你有一个S类型的对象,那么最终的覆盖是你在遍历S的类层次结构回到它的基类时看到的第一个覆盖.重要的是,函数调用表达式的动态类型用于确定最终的覆盖:

Base* p = new Derived; p -> vf(); // dynamic type of `*p` is `Derived` Base& b = *p; b . vf(); // dynamic type of `b` is `Derived`

覆盖和隐藏有什么区别?

实质上,基类中的函数总是被派生类中的同名函数隐藏;无论派生类中的函数是否覆盖基类的虚函数:

class Base { private: virtual void vf(int); virtual void vf2(int); }; class Derived : public Base { public: void vf(); // doesn't override, but hides `Base::vf(int)` void vf2(int); // overrides and hides `Base::vf2(int)` };

要查找函数名称,请使用表达式的静态类型:

Derived d; d.vf(42); // `vf` is found as `Derived::vf()`, this call is ill-formed // (too many arguments)

它们如何与函数重载相关?

由于“函数隐藏”是一种名称隐藏形式,如果隐藏了函数的名称,则所有重载都会受到影响:

class Base { private: virtual void vf(int); virtual void vf(double); }; class Derived : public Base { public: void vf(); // hides `Base::vf(int)` and `Base::vf(double)` };

对于函数重写,只覆盖具有相同参数的基类中的函数;你当然可以重载一个虚函数:

class Base { private: virtual void vf(int); virtual void vf(double); void vf(char); // will be hidden by overrides in a derived class }; class Derived : public Base { public: void vf(int); // overrides `Base::vf(int)` void vf(double); // overrides `Base::vf(double)` };