C++ polymorphism Virtual Function 多态 虚函数
Polymorphism in C++ https://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm
https://github.com/mongodb/mongo/blob/410656e971aff8f491a87337a17d04bd866389ba/src/mongo/base/initializer.cpp
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/ #include "mongo/platform/basic.h" #include "mongo/base/initializer.h" #include <iostream> #include "mongo/base/deinitializer_context.h"
#include "mongo/base/global_initializer.h"
#include "mongo/base/initializer_context.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/quick_exit.h" namespace mongo { Initializer::Initializer() {}
Initializer::~Initializer() {} Status Initializer::executeInitializers(const InitializerContext::ArgumentVector& args,
const InitializerContext::EnvironmentMap& env) {
std::vector<std::string> sortedNodes;
Status status = _graph.topSort(&sortedNodes);
if (Status::OK() != status)
return status; InitializerContext context(args, env); for (size_t i = 0; i < sortedNodes.size(); ++i) {
InitializerDependencyNode* node = _graph.getInitializerNode(sortedNodes[i]); // If already initialized then this node is a legacy initializer without re-initialization
// support.
if (node->isInitialized())
continue; auto const& fn = node->getInitializerFunction();
if (!fn) {
return Status(ErrorCodes::InternalError,
"topSort returned a node that has no associated function: \"" +
sortedNodes[i] + '"');
}
try {
status = fn(&context);
} catch (const DBException& xcp) {
return xcp.toStatus();
} if (Status::OK() != status)
return status; node->setInitialized(true);
}
return Status::OK();
} Status Initializer::executeDeinitializers() {
std::vector<std::string> sortedNodes;
Status status = _graph.topSort(&sortedNodes);
if (Status::OK() != status)
return status; DeinitializerContext context{}; // Execute deinitialization in reverse order from initialization.
for (auto it = sortedNodes.rbegin(), end = sortedNodes.rend(); it != end; ++it) {
InitializerDependencyNode* node = _graph.getInitializerNode(*it);
auto const& fn = node->getDeinitializerFunction();
if (fn) {
try {
status = fn(&context);
} catch (const DBException& xcp) {
return xcp.toStatus();
} if (Status::OK() != status)
return status; node->setInitialized(false);
}
}
return Status::OK();
} Status runGlobalInitializers(const InitializerContext::ArgumentVector& args,
const InitializerContext::EnvironmentMap& env) {
return getGlobalInitializer().executeInitializers(args, env);
} Status runGlobalInitializers(int argc, const char* const* argv, const char* const* envp) {
InitializerContext::ArgumentVector args(argc);
std::copy(argv, argv + argc, args.begin()); InitializerContext::EnvironmentMap env; if (envp) {
for (; *envp; ++envp) {
const char* firstEqualSign = strchr(*envp, '=');
if (!firstEqualSign) {
return Status(ErrorCodes::BadValue, "malformed environment block");
}
env[std::string(*envp, firstEqualSign)] = std::string(firstEqualSign + 1);
}
} return runGlobalInitializers(args, env);
} Status runGlobalDeinitializers() {
return getGlobalInitializer().executeDeinitializers();
} void runGlobalInitializersOrDie(int argc, const char* const* argv, const char* const* envp) {
Status status = runGlobalInitializers(argc, argv, envp);
if (!status.isOK()) {
std::cerr << "Failed global initialization: " << status << std::endl;
quickExit(1);
}
} } // namespace mongo
#include <iostream>
using namespace std; class Shape {
protected:
int width, height; public:
Shape( int a = 0, int b = 0){
width = a;
height = b;
}
int area() {
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public Shape {
public:
Rectangle( int a = 0, int b = 0):Shape(a, b) { } int area () {
cout << "Rectangle class area :" <<endl;
return (width * height);
}
}; class Triangle: public Shape {
public:
Triangle( int a = 0, int b = 0):Shape(a, b) { } int area () {
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
}; // Main function for the program
int main() {
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5); // store the address of Rectangle
shape = &rec; // call rectangle area.
shape->area(); // store the address of Triangle
shape = &tri; // call triangle area.
shape->area(); return 0;
}
Polymorphism in C++ https://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm
C++ 多态 | 菜鸟教程 http://www.runoob.com/cplusplus/cpp-polymorphism.html
C++ polymorphism Virtual Function 多态 虚函数的更多相关文章
- Virtual Function(虚函数)in c++
Virtual Function(虚函数)in c++ 用法: virtual void log() { std::cout << "hello world!" < ...
- 应该用bind+function取代虚函数吗?
用bind+function取代虚函数在好几年前就有人提出了,曾引起广泛的讨论,有支持的有反对的,可能赞成的人占大多数.这个话题挺有趣,本来是作为技术沙龙的开放性话题来讨论的,由于时间关系并没有讨论. ...
- C++继承-重载-多态-虚函数
C++ 继承 基类 & 派生类 一个类可以派生自多个类,这意味着,它可以从多个基类继承数据和函数.定义一个派生类,我们使用一个类派生列表来指定基类.类派生列表以一个或多个基类命名,形式如下: ...
- OOP 多态/虚函数
// main.cpp // OOP // 虚函数允许继承层次结构中绝大多数特定版本的成员函数被选择执行,虚函数使多态成为可能. // Created by mac on 2019/4/8. // C ...
- C++ (P199—P211)多态 虚函数 抽象类
在介绍多态之前,先回忆:赋值兼容原则.虚基类.二义性.派生类如何给基类赋值等知识. 在赋值兼容原则中:父类对象的指针赋给基类的指针或者父类的对象赋给基类的引用,可以通过强转基类的指针或者引用变为父类的 ...
- 看懂下面C++代码才说你理解了C++多态虚函数!
#include <iostream> using namespace std ; class Father { private : virtual void Say() //只有添加 ...
- 多态&虚函数
(1).对象类型: a.静态类型:对象声明时的类型,编译的时候确定 b.动态类型:对象的类型是运行时才能确定的 class A {}; class B:pub ...
- C++虚函数virtual,纯虚函数pure virtual和Java抽象函数abstract,接口interface与抽象类abstract class的比较
由于C++和Java都是面向对象的编程语言,它们的多态性就分别靠虚函数和抽象函数来实现. C++的虚函数可以在子类中重写,调用是根据实际的对象来判别的,而不是通过指针类型(普通函数的调用是根据当前指针 ...
- c++学习之多态(虚函数和纯虚函数)
c++是面向对象语言,面向对象有个重要特点,就是继承和多态.继承之前学过了,就是一种重用类的设计方式.原有的类叫父类,或者基类,继承父类的类叫子类.在设计模式中,我们总是要避免继承,推荐用组合.因为继 ...
随机推荐
- swift--使用URLSession异步加载图片
NSURLConnection,在ios9.0以后被废弃,以后使用URLSession类,如下图 具体样例: self.imageV.frame = CGRect(x:,y:,width:kScree ...
- windows C 設置控制臺文本輸出的顏色(可用作調試使用)
#include <windows.h> #define RED 0x0004 #define GREEN 0x0002 #define BLUE 0x0001 #define WHITE ...
- {"errorCode":50} 的解决办法
# 无反爬 import urllib.parse import urllib.request import json content = input('请输入需要翻译的词语:') # url = ' ...
- SpringBoot thymeleaf模板版本,thymeleaf模板更换版本
SpringBoot thymeleaf模板版本 thymeleaf模板更换版本 修改thymeleaf模板版本 ================================ ©Copyright ...
- PostgreSQL逻辑复制之slony篇
Slony是PostgreSQL领域中最广泛的复制解决方案之一.它不仅是最古老的复制实现之一,它也是一个拥有最广泛的外部工具支持的工具,比如pgAdmin3.多年来,Slony是在PostgreSQL ...
- PostgreSQL主备切换
备库如何激活 在PostgreSQL(HOT-Standby)如主库出现异常.备库如何激活:来替换主库工作.有下列2种方式 备库在recovery.conf文件中有个配置项trigger_file.它 ...
- Django 添加应用
一个项目可以添加多个应用,可以使用以下两种方法来添加应用: [root@localhost web]$ python manage.py startapp blog [root@localhost w ...
- Java中过滤器和拦截器的区别
1.拦截器是基于java反射机制的,而过滤器是基于函数回调的. 2.过滤器依赖于servlet容器,而拦截器不依赖于servlet容器. 3.拦截器只对action起作用,而过滤器几乎可以对所有请求起 ...
- 转:ANDROID音频系统散记之四:4.0音频系统HAL初探
昨天(2011-11-15)发布了Android4.0的源码,今天download下来,开始挺进4.0时代.简单看了一下,发现音频系统方面与2.3的有较多地方不同,下面逐一描述. 一.代码模块位置 1 ...
- 【技术分享会】 @第六期 iOS开发基础
前言 iOS之前被称为 iPhone OS,是一个由苹果公司开发的移动操作系统. iOS的第一个版本是在2007年发布的,其中包括iPhone和iPod Touch. iOS开发工具:Xcode 运行 ...