`
yaojingguo
  • 浏览: 209025 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Call virtual functions during construction or destruction

阅读更多

For c++, virutal funciton invocation during construction or destruction use virtual funcitons defined in base classes. Here is an example.

#include <iostream>
using namespace std;

class Transaction {
  public:
    Transaction() {
      logTransaction();
    }
    virtual void logTransaction() {
     cout << "Transaction logTransaction" << endl;
    } 
};


class BuyTransaction: public Transaction {
  public:
    virtual void logTransaction() const;
};

void BuyTransaction::logTransaction() const {
  cout << "BuyTransaction logTransaction" << endl;
}

int main(int argc, const char *argv[])
{
  BuyTransaction b; 
  return 0;
}

 For Java, overridable method invocation during construction use method defined in sub class.

import java.util.*;

class Super {
  // Broken - constructor invokes an overrided method
  public Super() {
    overrideMe();
  }
  public void overrideMe() {
  }
}

final class Sub extends Super {
  private final Date date; // Blank final, set by constructor

  Sub() {
    date = new Date();
  }

  // Overriding method invoked by superclass constructor
  @Override
  public void overrideMe() {
    System.out.println(date);  
  }
}

public class Foo {
  public static void main (String [] args)  {
    Sub s = new Sub();
  }
}
 

 

分享到:
评论

相关推荐

    Effective C++(第三版)

    never call virtual functions during construction or destruction. 条款10:令operator= 返回一个reference to *this have assignment operators return a reference to *this. 条款11:在operator= 中处理“自我...

    深度探索C++对象模型 超清版

    6.1 对象的构造和解构(Object Construction and Destruction) 全局对象(Global Objects) 局部静态对象(Local Static Objects) 对象数组(Array of Objects) Default Constructors和数组 6.2 new和delete...

    unity资源[游物理破坏系统] DestroyIt-Destruction System v1.15.unitypackage

    unity资源[游物理破坏系统] DestroyIt-Destruction System v1.15.unitypackage

    DestroyIt - Destruction System v1.12.rar

    DestroyIt - Destruction System v1.12插件用来处理物体或建筑的的摧毁和破坏效果。效果演示:http://www.modelshark.com/destroyitmaindemowebgl/index.html 本资源仅供大家学习参考。正版:...

    Fracturing_Destruction_1.04

    "Fracturing_Destruction_1.04"是一款专为Unity3D引擎设计的扩展插件,主要用于实现游戏或互动体验中的物体破碎和破坏效果。Unity3D是一款广泛应用于游戏开发、虚拟现实(VR)、增强现实(AR)等领域的三维游戏引擎,以...

    Unity3D特效粒子 墙体爆破爆炸特效包 Fracturing_Destruction_1.11

    在这个"Fracturing_Destruction_1.11"特效包中,重点是实现墙体爆破和爆炸效果,这在游戏和互动媒体中是非常重要的元素,可以增加场景的真实感和戏剧性。 首先,我们要理解Unity3D中的粒子系统。粒子系统是一种模拟...

    编辑器扩展 [插件] DestroyIt - Destruction System 1.9_摧毁_破碎插件

    编辑器扩展 [插件] DestroyIt - Destruction System 1.9_摧毁_破碎插件,链接:https://pan.baidu.com/s/1aeemywzuhCgA8xi_2OviFA,购买之前请先确认链接可用,文件里是链接密码。资源仅供大家学习交流,下载的文件...

    DestroyIt - Destruction System 1.9.unitypackage

    DestroyIt - Destruction System 1.9.unitypackage

    Fracturing Destruction v1.15

    unity物体打碎插件

    Fracturing & Destruction 1.11.unitypackage

    暴力破坏特效插件 CG软件: unity3d 系统版本: windows-32bit windows-64bit 适用版本: Unity 3.5.5及以上版本

    DestroyIt - Destruction System v1.12.unitypackage

    DestroyIt - Destruction System v1.12.unitypackage

    Derivatives are financial weapons of mass destruction

    ### 衍生品:金融大规模杀伤性武器? #### 导言 在当前全球经济陷入前所未有的金融危机之中时,人们很容易将衍生品视为罪魁祸首,指责其为造成金融混乱的“金融大规模杀伤性武器”。然而,《全球衍生品灾难:从...

    GObject Reference Manual

    Object Construction Object Destruction Object methods Non-virtual public methods Virtual public methods Virtual private Methods Chaining up How To define and implement Interfaces? How To ...

    Guns N' Roses - Appetite For Destruction - 09 - Sweet Child O' Mine

    Guns N' Roses - Appetite For Destruction - 09 - Sweet Child O' Mine

    Fracturing & Destruction v1.15.unitypackage

    十分真实的爆破、爆炸效果。It is a set of powerful and well-proven extensions that help you build better games saving time and cost. All have been thoroughly tested and used in commercial games and ...

    深入理解c++中virtual关键字

    virtual void fun() { cout &lt;&lt; "GrandFather call function!" ; } }; class Father : public GrandFather { public: Father() {} void fun() { cout &lt;&lt; "Father call function!" ; } }; class Son : public ...

    DestroyIt – Destruction System v1.14

    最新推出!现在版本 1.10 具有创建可破坏地形树木的能力了!使用 Unity 的地形系统来放置树木,然后再用 DestroyIt 来让它们变得完全可破坏。 注意:由于在 Unity 2018.3 中预制件系统的更改,因此 DestroyIt 1.10 ...

    Fracturing+Destruction

    购买的的爆裂、炸裂、碎裂效果特效插件,很好用,分享给大家学习; 插件仅用于学习,禁止商用,为了维护您的个人权益,请支持正版; 官网地址https://www.assetstore.unity3d.com/en/#!/content/9411

    CImg Reference

    **Image Construction/Destruction/Copy:** - Images can be constructed from files, memory buffers, or initialized with specific dimensions. - The destructor frees any allocated memory. - Copies of ...

Global site tag (gtag.js) - Google Analytics