Effectiv C++条款31 将文件间的编译依存关系降至最低 Handle Class和Interface Class完整实现

《Effective C++》学习总结(条款31- 40) 条款31:将文件间的编译依存关系(compilation dependencies)降至最低 对于C++类而言,如果它的头文件变了,那么所有这个类的对象所在的文件都要重编,但如果它的实现文件(cpp文件)变了,而头文件没有变(对外的接口不变),那么所有这个类的对象所在的文件都不会因之而重编 1.接口与实现分离(通过pimpl idiom的设计方式)——其实质在于以”声明的依存性“替换”定义的依存性“ 上述也是编译依存最小化的本质——现实中让头文件尽可能自我满足,万一做不到,则让它与其他文件内的声明式相 阅读详情
//条款31 将文件间的编译依存关系降至最低
/*
Handle class
简而言之,将类的接口和实现分离。
接口里面存储一个指针,指向实现类,然后客户调用接口。
这样当实现改变时候,客户不用重编译。

Handle Class (Person.h PersonImpl.h PersonImpl.cpp)
Person的客户就与Person的实现细目分离,Person实现的修改不需要Person客户端重新编译。
接口与实现分离。一个只负责提供接口,另一个负责实现该接口。main class(Person)内含有一个指针成员
shared_ptr<PersonImpl>pImpl.(pimpl pointer to implementation)。Person客户完全与Person实现细目分离。
这个分离的关键在于以“声明的依存性”替换“定义的依存性”。现实中让头文件尽可能自我满足,万一做不到,
则让它与其它文件内的声明式(而非定义式)相依。
*/

/*
Interface class
简而言之,将类抽象为接口,然后客户调用接口,这样当类的实现改变时不会受影响。
类的实现通过继承抽象基类完成。
客户调用的构造接口是static,因为它定义在抽象基类中,而抽象基类不能实例化,
只能在实例化派生类时部分实例化,所以定义成static。static的函数属于整个类,
可以通过类名::函数名的方式调用,具体构造函数在派生类中定义。


另一种制作Handle class的办法是,令Person称为抽象基类,称为:
Interface Class (Person2.h Person2.cpp RealPerson2.h)
Person类定义成抽象基类,成为接口。这个class的客户必须以Person的指针或引用来撰写应用程序。
Interface class的客户通常调用factory(工厂)函数或virtual构造函数。它们返回指针(或智能指针),指向
动态分配所得对象,而该对象支持Interface class的接口。这样的函数又往往在Interface class内被
声明为static。
支持Interface class接口的那个具象类(concrete classes)必须被定义出来,而且真正的构造函数必须被调用。
它提供继承而来的virtual函数的实现。

*/


Handle class

Person.h

#include <string>
#include <memory>
using namespace std;

class PersonImpl;						// 	Person实现类的前置声明

class Person
{
public:
	Person(const string& name);
	string getname() const;
	
private:
	
	tr1::shared_ptr<PersonImpl> pImpl;	// 指针,指向实现物,或者PersonImpl* pImpl
	string name;
};


Person.cpp

#include "stdafx.h"
#include "Person.h"
#include "PersonImpl.h"

Person::Person(const string& name) : pImpl(new PersonImpl(name))
{
	this->name = name;
}

string Person::getname() const
{
	return pImpl->getname();
}

PersonImpl.h

#include <string>
#include <iostream>
using namespace std;
class PersonImpl
{
public:
	PersonImpl(const string& name)
	{
		this->name = name;
		//cout << "Impl" << endl;
	}
	string getname() const;
private:
	string name;
};

PersonImpl.cpp

#include "stdafx.h"
#include "PersonImpl.h"
string PersonImpl::getname() const
{
	return this->name;
}

main.cpp

#include "stdafx.h"
#include "Person.h"
#include <memory>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	// Handle Class
	Person p("Yerasel");
	cout << p.getname() << endl;


	return 0;
}

以上方法是pImpl方法,它是微软的Herb Sutter提出来的,该方法是为了尽量减小接口和实现之间的耦合,以避免接口改动对程序重新编译等带来的影响。简单来说,如果你的大型程序因为复杂的头文件包含关系,使得你对某头文件的某小改动可能引起的巨大编译时间成本望而生畏,那么你需要用pImpl方法来改善这种处境。



Interface class

Person2.h

#ifndef PER2_H
#define PER2_H
#include <string>
#include <memory>
using namespace std;

class Person2
{
public:

	Person2(const string& name)
	{
		this->thename = name;
	}
	virtual ~Person2(){};
	
	static tr1::shared_ptr<Person2> create(const string& name);
	virtual string getname() const = 0;
private:
	string thename;
};
#endif

Person2.cpp
#include "stdafx.h"

#include "Person2.h"
#include "RealPerson2.h"
#include <memory>


tr1::shared_ptr<Person2> Person2::create(const string& name)
{
	return tr1::shared_ptr<Person2>(new RealPerson2(name));
}

RealPerson2.h

#ifndef REAL2_H
#define REAL2_H
#include "Person2.h"
#include <iostream>
using namespace std;

class RealPerson2 : public Person2
{
public:
	RealPerson2(const string& name)
		:Person2(name), thename(name)
	{
	}
	virtual ~RealPerson2(){}
	string getname() const
	{
		return this->thename;
	}
private:
	string thename;
};

#endif

main.cpp
#include "stdafx.h"
#include "Person.h"
#include <memory>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// Interface Class
	tr1::shared_ptr<Person2>p2 = (Person2::create("jandosim"));
	cout << p2->getname() << endl;
	return 0;
}

附注,shared_ptr示例


//工厂模式
class abstract
{
public:
	virtual void f()=0;
	virtual void g()=0;
protected:
	virtual ~abstract(){}
};

class impl:public abstract
{
public:
	virtual void f()
	{
		cout << "class impl f" << endl;
	}
	virtual void g()
	{
		cout << "class impl g" << endl;
	}
};

tr1::shared_ptr<abstract> create()
{
	return tr1::shared_ptr<abstract>(new impl);
}

int _tmain(int argc, _TCHAR* argv[])
{
	tr1::shared_ptr<abstract> a = create();
	a->f();
	a->g();

	return 0;
}

运行环境VS2008,参考书籍《Effective C++ 3rd》







        
2025年一篇解决Windows11安装医学图像处理工具-FSL(各种问题解决,过程详细,小白轻松安装) 2025年一篇帮你解决FSl安装所有问题,包括环境安装、使用说明、常见问题,过程简单易懂,小白轻松安装。 阅读详情

相关推荐

LSTM量化选股实战:从数据清洗到回测归因的工程化闭环

LSTM(长短期记忆网络)是一种专为建模长程依赖而设计的循环神经网络,其门控机制能有效应对金融时序数据的非线性、高噪声与动态演化特性。相比传统统计模型或简单RNN,LSTM在股价趋势建模中展现出更强的鲁棒性与泛化能力,尤其适合处理日频多因子时序信号。其技术价值不仅在于提升预测精度,更在于支撑可归因、可审计的量化策略工程实践——从特征构建、标签定义到滚动回测,每一步都需严防未来信息泄露与过拟合风险。典型应用场景包括A股多因子选股、行业轮动信号生成及Alpha组合优化。本文聚焦LSTM在真实量化场景中的落地难点

weixin_33158887的博客 406

c++ 的interface

先要说明的是,c++的关键字中并没有interface,但java和C#中有interface关键字,即接口。interface和class不同,interface仅有接口声明,而且所有的声明默认的访问权限是public而非private(是不是想到了c++中的struct?)。 对于c++来说,这相当于抽象类的概念,即其中的成员函数都是纯虚函数,只有声明,没有实现。如: class abs...

sinat_39370511的博客 5119

Oracle PLM Agile936 单点登录配置方式

Oracle PLM Agile936 单点登录配置方式

Jay的个人博客 828

C++接口实现总结

网上的例子,稍微有点错误。我给更改一下,附件上有源码!如有错误,请指正。 总结一下C++实现接口的技巧。       面向对象的语言诸如JAVA提供了Interface来实现接口,但C++却没有这样一个东西,尽管C++ 通过纯虚基类实现接口,譬如COM的C++实现就是通过纯虚基类实现的(当然MFC的COM实现用了嵌套类),但我们更愿意看到一个诸如 Interface的东西。下面就介绍一种解决办...

qimo601的专栏 1312

SAP PO开发-端到端配置

1.登录PO系统,选择system landscape,将外围系统的系统信息(Software Component以下简称SC)注册到PO系统中。2.点击products注册外围系统信息。3.选择New 新建 按钮:创建一个新的系统和版本。4.输入产品名称、供应商、版本号等信息。然后点击:Next。5.设置instance 名称,然后点击Next。6.最后设置软件组件的名称,然后点击Finish。

P9QCer的博客 1594

silvaco例子库学习—mos1ex02

mos1ex02相比于第一个例子,主要进行了mos的输出特性仿真,工艺结构是一样的,所以就只简单讨论一下atlas特性仿真的部分。

weixin_61913839的博客 855

句柄类(handle class)(一)

问题:对于某些类来说,能够避免复制其对象是很有好处的。因为有可能对象太大,复制起来消耗太大。也有可能每个对象代表一种不能被轻易复制的资源,譬如文件等等情况。需要有一种方法,让我们在避免某些缺点(缺乏安全性)的同时能够获取指针的某些优点。尤其是在保持多态性的前提下,避免复制对象的代价,因此我们引入句柄类来解决这一问题。以一个简单的类作为例子。关于这个类有必要做三点说明1.如果使用 Point(int...

Logan155的博客 4558

opencode 完全指南:终端AI编程助手的安装、模型配置与实战

在AI编程助手快速迭代的当下,开发者对终端环境下的自动化编码需求愈发强烈。以自然语言驱动代码修改、命令执行与测试验证,已成为提升工程效率的重要方向。这类工具普遍遵循模型无关的设计原则,通过兼容OpenAI格式的接口接入多种大模型,并借助多会话管理、Skills、Memory等机制实现可审查、可干预的闭环流程。从安装配置到日常使用,再到apihub网关与免费模型的接入,终端AI助手在代码审查、跨文件重构、浏览器自动化验证等场景中展现出独特价值。本文聚焦opencode这一开源CLI工具,详细拆解其核心定位、模

weixin_33868027的博客 423

Virtual and Abstract in C++ - C++ 抽象类 (abstract class) - 接口类 (interface class) - 纯虚函数 (pure virtual)

Virtual and Abstract in C++ - C++ 抽象类 (abstract class) - 接口类 (interface class) - pure virtual (纯虚函数) Before going to virtual function, let’s first have a look at early binding and late binding. 在使用虚函数之前,我们先来看一下早期绑定和后期绑定。 Binding means matching the function

程永强 1262

手写中间件之——并行框架(1 并行框架的应用场景和需求)

我们为什么会需要一个带任务顺序编排的并行框架 1 复杂的微服务系统间调用 经常会有这样的调用场景:app(或web前端)调用后台的一个接口,该接口接到该请求后,需要调用其他多个微服务来获取数据,最终汇总一个最终结果返回给用户。 譬如用户请求“我的订单”,后台在收到请求后,就需要去调用用户详情rpc、商品详情rpc、库存rpc、优惠券rpc等等很多个服务。有些服务是可以并行去请求的,但有些服务...

tianyaleixiaowu的专栏 1万+

RK3568笔记六十一:MIPI摄像头应用程序编写

V4L2设备驱动框架向应用层提供了一套统一、标准的接口规范,应用程序按照该接口规范来进行应用编程,从而使用摄像头。对于摄像头设备来说,其编程模式如下所示:1.首先是打开摄像头设备;2.查询设备的属性或功能;3.设置设备的参数,譬如像素格式、帧大小、帧率;4.申请帧缓冲、内存映射;5.帧缓冲入队;6.开启视频采集;7.帧缓冲出队、对采集的数据进行处理;8.处理完后,再次将帧缓冲入队,往复;9.结束采集。

1009

C++ handle classes

C++中,使用handle classes(句柄类)的目的,是为了降低文件之间的编译依存关系。 摘自网上的一段话:        需要句柄类的背景:  1)在对安全要求很高的领域,即使核心实现已经封闭在库中不可见,但头文件中变量定义仍可能曝露一些内部信息 2)在设计初期、实现部分会经常变动,甚至头文件中变量定义也需要经常变动,因此在重编译的时候头文件也需要编译,有时候导致编

Never too late to study 1372

Linux iperf 进行跑流测试时,为啥单条流会有速率上不去的问题?

iperf

1042484520@qq.com(邮箱) 3098

C++8种常见类类型

C++8种常见类类型 前言 大部分面向对象开发工作中都应用了以下部分或者全部的基本类别的类: 1、具体类(concrete class) 2、抽象类(abstract class) 3、接口类(interface class) 4、节点类(node class) 5、支持类(support class) 6、域类(domain class) 7、应用类(utility class) 8、集合和容器类(collection and container class) 这些类并不是特定的语言结构,而是用于实现逻辑

TwcatL 5269

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

c++ 接口类、纯虚类、抽象类的杂谈

这些东西都是思想层面的东西,不是语言层面的。 实际上,C++中并没有明确的接口的定义,与之等价的是纯虚类,既只有纯虚函数的类,而c++中抽象类的概念是,包含至少一个纯虚函数的类。由于java只支持单继承,所以出现了interface的定义,从而用来模拟多继承。 可以这样理解,按抽象程度递增的顺序说就是:普通类->抽象类(java中由abstract修饰的类)->接口(纯虚类)(java中interface修饰的类)。 参考《Effective C++》条款31和条款34: c++中int

flying_ant2018的博客 761

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Handle Class 和 Interface Class

<br />这两者都是为了降低文件间的编译依存<br /><br />1.编译依存#include"file1.h"<br />#include"file2.h"<br /><br />class class_name<br />{<br /> member1 m_m1;<br /> member2 m_m2;<br />public:<br /> member1 get_member_1()const{};<br /> member2 get_member_2()const{};<br />};<br /

train的专栏 1170

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【C++面向对象编程】接口和类详解

在C++面向对象编程的广阔天地中,类(Class)和接口(Interface)是构建复杂软件系统的基石。类封装了数据和操作数据的方法,而接口则定义了对象的行为规范。本文将深入探讨C++中类和接口的概念、实现方式、它们之间的关系,以及在实际编程中的应用。

byte轻骑兵的技术小窝 3159

handle class和interface class

降低文件间的依存关系:第一种方法: handle class 把一个类变为一个接口类和一个实现类 实现类就是原来的这个类,接口类就是原来这个类的接口函数(public)和 一个private的智能指针对象(一般为shared_ptr)指向实现类,接口类里面需要用到的类,我们提供给它的都是类的前向声明,所以为了严格执行这样的准则,我们把每一个类(包括接口类和实现类)都提供两个头文件,一个是类的声明 ...

speargod的博客 1474

c++ 编写接口类(interface)举例

接口类简介: 接口是一系列抽象方法的声明,是一些方法特征的集合,这些方法都应该是抽象的,需要由具体的类去实现,然后第三方就可以通过这组抽象方法调用,让具体的类执行具体的方法。 用c++实现接口类时需要注意一下几点: 1、接口类中不应该声明成员变量,静态变量。 2、可以声明静态常量作为接口的返回值状态,这些静态变量需要初始化,访问时需要使用"接口类型::静态常量名"访问 2、定义的接口方法使用vir...

斧冰 6062

Handle class && Interface class

这两者都是为了降低文件之间的编译依存 1编译依存 class class_name{ member1 m_m1; member2 m_m2; public: member1 get_member_1() const{}; member2 get_member_2() const{]; }; 假设上面的头文件是file.h,当file1.h发生变化,或者file中的class_na...

weixin_30457065的博客 156

handle classes 和interface classes

是一种设计模式,通过将类的实现细节隐藏在一个指针或句柄后面,从而将接口与实现分离。它通常使用。

m0_57699573的博客 1106

【C++】接口类(Interface Class)与抽象类 (Abstract Class)

抽象类 (Abstract Class)定义:包含至少一个 纯虚函数(pure virtual function) 的类。

weixin_43510208的博客 1246

Effective C++ chapter_5

5 实现 条款 26 尽可能延后变量定义式出现的时间 ​ 我们写代码的时候,可能会定义一些没有使用的变量,特别是当你过早的定义一个变量,从定义到使用中间如果出现异常且该异常被丢出,那么你就要承担不必要的构造和析构成本。 ​ 所以尽量在你使用到变量的前一刻再定义它。 请记住 : ​ 1. 尽可能延后定义式的出现。这样做可以增加程序的清晰度并改善程序的效率。 条款 27 尽量少做转型动作 ​ C++ 规则的设计目标之一是,保证“类型错误”绝不可能发生。 C++ 提供了四种转型方法 : const_cast 通

weixin_43599089的博客 246
上一篇: 程序计时
下一篇: Override和Overload的区别
Yerasel
博客等级 码龄19年 42粉丝 85原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值