Skip to Content
C++ Cookbook
book

C++ Cookbook

by D. Ryan Stephens, Christopher Diggins, Jonathan Turkanis, Jeff Cogswell
November 2005
Beginner to intermediate
594 pages
16h 23m
English
O'Reilly Media, Inc.
Content preview from C++ Cookbook

8.2. Using a Function to Create Objects (a.k.a. Factory Pattern)

Problem

Instead of creating a heap object with new, you need a function (member or standalone) to do the creation for you so that the type of the object being created is decided dynamically. This sort of behavior is what the Abstract Factory design pattern achieves.

Solution

You have a couple of choices here. You can:

  • Have the function create an instance of the object on the heap, and return a pointer to the object (or update a pointer that was passed in with the new object’s address)

  • Have the function create and return a temporary object

Example 8-2 shows how to do both of these. The Session class in the example could be any class that you don’t want application code to create directly (i.e., with new), but rather you want creation managed by some other class; in this example, the managing class is SessionFactory.

Example 8-2. Functions that create objects

#include <iostream> class Session {}; class SessionFactory { public: Session Create(); Session* CreatePtr(); void Create(Session*& p); // ... }; // Return a copy of a stack object Session SessionFactory::Create() { Session s; return(s); } // Return a pointer to a heap object Session* SessionFactory::CreatePtr() { return(new Session()); } // Update the caller's pointer with the address // of a new object void SessionFactory::Create(Session*& p) { p = new Session(); } static SessionFactory f; // The one factory object int main() { Session* p1; Session* p2 = new Session(); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Embedded Programming with Modern C++ Cookbook

Embedded Programming with Modern C++ Cookbook

Igor Viarheichyk
C++ In a Nutshell

C++ In a Nutshell

Ray Lischner

Publisher Resources

ISBN: 0596007612Errata Page