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++是面向对象语言,面向对象有个重要特点,就是继承和多态.继承之前学过了,就是一种重用类的设计方式.原有的类叫父类,或者基类,继承父类的类叫子类.在设计模式中,我们总是要避免继承,推荐用组合.因为继 ...
随机推荐
- ios开发之--调整UISearchBar的输入框的背景颜色
遍历UISearchBar的子视图,找到输入框坐在的view,添加背景颜色即可. 代码如下: UISearchBar *searchBar = [[UISearchBar alloc] initWit ...
- iOS 图片加载速度极限优化—FastImageCache解析
FastImageCache是Path团队开发的一个开源库,用于提升图片的加载和渲染速度,让基于图片的列表滑动起来更顺畅,来看看它是怎么做的.优化点iOS从磁盘加载一张图片,使用UIImageVIew ...
- 转换python脚本为可执行程序的方式
背景: 部分工具使用python脚本编写,而目标服务器,没有安装python包,导致使用工具不方便,还需要另外安装python. 目前主要有2个主流软件,可做此类转换,把对应工具脚本转换为exe: p ...
- SpringBoot application.properties (application.yml)优先级从高到低
SpringBoot application.properties(application.yml) 优先级从高到低 SpringBoot配置文件优先级从高到低 =================== ...
- nessus 激活码
nessus激活码的申请 nessus屏蔽了中国的激活码申请,中国IP申请的时候会直接跳转到购买商业版的页面. 解决方法: 使用IE代理或者VPN,用美国的IP最好,然后访问网址: http://ww ...
- Ansible 使用 Playbook 管理 Nginx 配置文件
前面我们已经安装完 Nginx,但是在日常维护中经常需要修改配置文件,并重新加载配置文件,因此来写一个管理 Nginx 配置文件的 Playbook: [root@localhost ~]$ mkdi ...
- Kafka producer介绍
Kafka 0.9版本正式使用Java版本的producer替换了原Scala版本的producer.本文着重讨论新版本producer的设计原理以及基本的使用方法. 新版本Producer 首先明确 ...
- Windows下POSIX线程编程(pThread)环境搭建
系统: Windows 编辑器:codeblocks13.12 1. 简介: Windows有一个叫 POSIX Threads for Win32 的开源项目给出了一个功能比较完善的Windows下 ...
- css媒体查询来书写二倍图三倍图设置
@media (-webkit-min-device-pixel-ratio: 2){} @media (-webkit-min-device-pixel-ratio: 3){}
- Xcode 7.3 cannot create __weak reference in file using manual reference counting
原帖地址 http://stackoverflow.com/questions/36147625/xcode-7-3-cannot-create-weak-reference-in-file-us ...