热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

*你*使用C++ABC构造函数做什么?-Whatdo*you*useC++ABCconstructorsfor?

WhatdopeoplehereuseC++AbstractBaseClassconstructorsforinthefield?Iamtalkingaboutpu

What do people here use C++ Abstract Base Class constructors for in the field? I am talking about pure interface classes having no data members and no non-pure virtual members.

人们在这里使用C ++抽象基类构造函数?我说的是没有数据成员且没有非纯虚拟成员的纯接口类。

Can anyone demonstrate any idioms which use ABC constructors in a useful way? Or is it just intrinsic to the nature of using ABCs to implement interfaces that they remain empty, inline and protected?

任何人都可以用有用的方式展示任何使用ABC构造函数的习语吗?或者它只是固有的使用ABCs实现接口,它们保持空,内联和保护?

6 个解决方案

#1


Can anyone demonstrate any idioms which use ABC constructors in a useful way?

任何人都可以用有用的方式展示任何使用ABC构造函数的习语吗?

Here's an example, although it's a contrived, uncommon example.

这是一个例子,虽然它是一个人为的,不常见的例子。

You might use it to keep a list of all instances:

您可以使用它来保留所有实例的列表:

class IFoo
{
private:
  //static members to keep a list of all constructed instances
  typedef std::set Set;
  static Set s_set;

protected:
  //new instance being created
  IFoo()
  {
    s_set.insert(this);
  }

public:
  //instance being destroyed
  virtual ~IFoo()
  {
    s_set.remove(this);
  }

  ... plus some other static method and/or property
      which accesses the set of all instances ...
};

Or is it just intrinsic to the nature of using ABCs to implement interfaces that they remain empty, inline and protected?

或者它只是固有的使用ABCs实现接口,它们保持空,内联和保护?

More usually they're just not declared at all! There's no reason to declare them:

更常见的是,他们根本就没有宣布!没有理由声明它们:

  • Empty and inline => why bother to declare it?
  • 空和内联=>为什么懒得申报呢?

  • Protected => the ABC probably already has some pure virtual methods and therefore already can't be instantiated except as a subclass.
  • Protected => ABC可能已经有一些纯虚方法,因此除了作为子类之外,已经无法实例化。

#2


Suppose that there is some behavior that is common for all the derived classes. Such as registering itself in some external registry, or checking validity of something.

假设存在一些对所有派生类都很常见的行为。比如在某些外部注册表中注册,或检查某些内容的有效性。

All this common code can be placed in base class's constructor, and it will be called implicitly from the constructors of each of the derived classes.

所有这些公共代码都可以放在基类的构造函数中,并且它将从每个派生类的构造函数中隐式调用。

#3


How could an abstract base class's constructor be used for anything?

如何将抽象基类的构造函数用于任何事情?

Suppose you have an abstract base class B and a derived class D. When an object of type D is created, B's constructor is called first, but at that point, the object "is" still of type B (see here) -- in particular, calling any virtual functions from the body of B's constructor will call B's own implementations of those functions. But if B is a pure abstract class, none of those virtual functions are defined, so the program will crash immediately.

假设你有一个抽象基类B和一个派生类D.当创建一个D类型的对象时,首先调用B的构造函数,但此时,对象“仍然”仍然是类型B(参见此处) - 在特别是,从B的构造函数的主体调用任何虚函数将调用B自己的那些函数的实现。但是如果B是纯抽象类,那么这些虚函数都没有定义,所以程序会立即崩溃。

I'm guessing that you intended for B's constructor to call down to the most-derived-class's (e.g. D's) implementation of a virtual function, right? That would be a bad idea in general because D's object is not fully constructed yet, so any accesses to member variables in D from inside D's implementation of the virtual function would access uninitialised memory.

我猜你打算让B的构造函数调用虚函数的最派生类(例如D')实现,对吧?这通常是一个坏主意,因为D的对象尚未完全构造,因此从D的虚函数实现中对D中的成员变量的任何访问都将访问未初始化的内存。

#4


Remember: "Resource acquisition is initialization".

请记住:“资源获取是初始化”。

Sometimes we use abstract base classes as some kind of locking mechanism. For example, in a multi-threaded environment, where several threads need to share a single resource, then a thread can use the constructor as a way to acquire the resource, and the destructor to release the resource

有时我们使用抽象基类作为某种锁定机制。例如,在多线程环境中,多个线程需要共享单个资源,然后线程可以使用构造函数作为获取资源的方法,并使用析构函数来释放资源

void PlayWithPaintBallGun(Target &target)
{
    PaintBallGun paintBallGun;    // constructor waits until the gun is free,
                                  // then picks it up.

    paintBallGun.Aim(target);     // Shoot something
    paintBallGun.Fire();          //

                                  // Clever! The destructor is automatically
                                  // called when it goes out of scope. So we
                                  // can't forget to put the gun down.
}

Hugo

#5


I can't think of many useful examples. A class without data-members has no state and thus can't initialize anything. You can have the constructor/destructor do logging for you, though. For example, to log the creation/destruction of all Visitor objects:

我想不出很多有用的例子。没有数据成员的类没有状态,因此无法初始化任何内容。但是,您可以让构造函数/析构函数为您执行日志记录。例如,要记录所有Visitor对象的创建/销毁:

class Visitor {
public:
    Visitor() {
        std::cout <<"Visitor@" <

#6


usually its solely to initialise members to sensible values.

通常只是将成员初始化为合理的价值观。


推荐阅读
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
  • 1Lock与ReadWriteLock1.1LockpublicinterfaceLock{voidlock();voidlockInterruptibl ... [详细]
  • Java编程思想一书中第21章并发中关于线程间协作的一节中有个关于汽车打蜡与抛光的小例子(原书的704页)。这个例子主要展示的是两个线程如何通过wait ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 本文讨论了在数据库打开和关闭状态下,重新命名或移动数据文件和日志文件的情况。针对性能和维护原因,需要将数据库文件移动到不同的磁盘上或重新分配到新的磁盘上的情况,以及在操作系统级别移动或重命名数据文件但未在数据库层进行重命名导致报错的情况。通过三个方面进行讨论。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • MySQL语句大全:创建、授权、查询、修改等【MySQL】的使用方法详解
    本文详细介绍了MySQL语句的使用方法,包括创建用户、授权、查询、修改等操作。通过连接MySQL数据库,可以使用命令创建用户,并指定该用户在哪个主机上可以登录。同时,还可以设置用户的登录密码。通过本文,您可以全面了解MySQL语句的使用方法。 ... [详细]
  • 本文介绍了在使用Laravel和sqlsrv连接到SQL Server 2016时,如何在插入查询中使用输出子句,并返回所需的值。同时讨论了使用CreatedOn字段返回最近创建的行的解决方法以及使用Eloquent模型创建后,值正确插入数据库但没有返回uniqueidentifier字段的问题。最后给出了一个示例代码。 ... [详细]
  • 这个问题困扰了我两天,卸载Dr.COM客户端(我们学校上网要装这个客户端登陆服务器,以后只能在网页里输入用户名和密码了),问题解决了。问题的现象:在实验室机台式机上安装openfire和sp ... [详细]
author-avatar
手机用户2602901471
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有