`
standalone
  • 浏览: 611477 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Use Protected Constructors to Block Undesirable Object Instantiation

    博客分类:
  • c++
阅读更多
Use Protected Constructors to Block Undesirable Object Instantiation
In order to block creation of class instances, you can declare its constructor as protected.
 
class CommonRoot {
	protected: CommonRoot(){}//no objects of this class can be instantiated
};

class Derived: public CommonRoot {
public: Derived() {}
};

Derived d; // OK, constructor of d has access to any protected member in its base class
CommonRoot cr; //compilation error: attempt to access a protected member of CommonRoot
The same effect of blocking instantiation of a class can be achieved with pure virtual functions. However, these add runtime and space overhead. When pure virtual functions aren't needed, you can use a protected constructor instead.
Danny Kalev
分享到:
评论

相关推荐

    JavaScript Object Programming(Apress,2015)

    This brief book explains the advantages of the object model, inheritance, both classical and prototypical, and shows how these concepts can be ...How to use Constructors with JavaScript and more

    C 程序设计教学课件:Chapter 3 class and object.ppt

    Students learn about class syntax, access specifiers, data and function members, constructors and destructors, composition, static members, and the use of constants in object-oriented design....

    Object Oriented Programming with Swift 2

    Use state of inheritance, specialization, and the possibility to overload members Write high quality object-oriented code to build apps for iOS or Mac OS X In Detail Object-Oriented Programming (OOP)...

    The Principles of Object-Oriented JavaScript 1st Edition

    –How to define your own constructors –How to work with and understand prototypes –Inheritance patterns for types and objects The Principles of Object-Oriented JavaScript will leave even ...

    Copy Constructors and Assignment Operators终极解释

    在C++编程语言中,复制构造函数(Copy Constructor)和赋值运算符(Assignment Operator)是两个非常关键的概念,特别是在处理对象的拷贝和赋值时。它们默认由编译器提供,但通常需要根据具体需求进行自定义,以确保正确...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Classes Doing Work in Constructors Default Constructors Explicit Constructors Copy Constructors Structs vs. Classes Inheritance Multiple Inheritance Interfaces Operator Overloading Access Control ...

    Google C++ Style Guide_英文版.pdf

    - **Class Template Argument Deduction:** Use CTAD (Class Template Argument Deduction) to make template class instantiation more concise. - **Designated Initializers:** Use designated initializers for ...

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 31: Use bounded wildcards to increase API flexibility Item 32: Combine generics and varargs judiciously Item 33: Consider typesafe heterogeneous containers 6 Enums and Annotations Item 34: Use ...

    object-c基础教程

    ### Object-C基础教程知识点概述 #### 一、Object-C简介 - **定义**: Object-C是一种通用、面向对象的编程语言,基于C语言并扩展了许多面向对象的特性。 - **应用场景**: 主要用于开发iOS和macOS应用。 - **特点**...

    Selected.Topics.in.Cplusplus.15117

    When you know these core concepts C++ becomes a powerful language to use because you can control your program to do exactly what you want. Then you can unleash the power of C++. Table of Contents ...

    Intermediate Perl.pdf

    administrators to a robust object-oriented programming (OOP) language that runs on practically every computing platform known to mankind. Throughout its four editions, Learning Perl remained the ...

    PowerMock资料大全(E文)

    Developers familiar with the supported mock frameworks will find PowerMock easy to use, since the entire expectation API is the same, both for static methods and constructors. PowerMock aims to ...

    《深度探索C++对象模型》(Stanley B·Lippman[美] 著,侯捷 译)

    如果你是一位C++程序员,渴望对于底层知识获得一个完整的了解,那么Inside TheC++ Object Model正适合你。 目录: 本立道生(侯捷 译序) 前言(Stanley B.Lippman) 第0章 导读(译者的话) 第1章 关于对象...

    JSP Simple Examples

    To use the class inside the jsp page we need to create an object of the class by using the new operator. At last use the instance of the class to access the methods of the java file. Setting Colors ...

    Object-c学习文档

    ### Object-C 学习文档知识点总结 #### 开始学习Objective-C **下载教程:** - 所有的初学者指南原始代码可从`objc.tar.gz`下载。 - 许多示例来自Steve Kochan的《Objective-C 编程》一书。若需更深入的学习和示例...

Global site tag (gtag.js) - Google Analytics