Reputation: 1862
I'm new to C++ and i have a little experience with C and different dynamic scripting languages, such as JavaScript, PHP, and a little Ruby and Python. I've worked, even if not for long, with Object Oriented (OO) principles, but the thing that is new to me is that C++ does use pointers to classes. Pointers in C are not a mystery for me anymore (I hope so at least) and if I get it right - never use pointers except you have no other option. Exceptions would be if you want to keep track of a special variable or if you want to pass it to a function to change its value. And since I have to have a motivation to use pointers which one could I have to use them in C++. Could you please refer to an example? I have studied on Wikipedia. It is an example of a Builder Design Pattern in C++. They use Pizza
class and PizzaBuilder
, which uses a m_pizza
pointer to Pizza
class. What is the Reason to use a pointer instead of an normal instance here?
Just to avoid misconceptions
What I meant with my statement: "Pointers in C are not a mystery for me anymore", is that I know how they stored in memory etc. It does not mean I'm a C-ninja :)
And when i say: "I've worked, even if not for long, with Object Oriented (OO) principles", I mean not for long as if not very experienced but I get it at the first glance, and I worked only with high-level scripting languages and had no experience with any other low-level languages except for C and C has no classes. :)
Upvotes: 2
Views: 856
Reputation: 6005
The m_pizza = new Pizza; line in the constructPizza() method allocates memory and makes the pointer-member variable "m_pizza" to point to it. By using getPizza(), you are getting that pointer to the object's allocated memory.
Apart from that, each builder object created has its own Pizza pointer member variable. Therefore, in main, Pizza objects pointed by hawaiian and spicy after each getPizza() invoke, are different objects.
Additionally, please notice that if we have 2 consequent calls of same builder's instance createNewPizzaProduct() somewhere, 2 objects will be created, but only the last one's memory will be set to be pointed by member variable pointer, causing a memory leak. (This could happen for example if m_pizzaBuilder->createNewPizzaProduct(); is called 2 times in Cook's constructPizza() method by mistake. My point is that the code here could be more memory-leak secure, although it does not seem to have any leaks the way it is written).
Upvotes: 1
Reputation: 81
The reason they use pointers in this example is that its an abstract factory. Its purpose is to build pizza objects from different pizza classes.
The abstract factory needs to hold these objects without knowing how much space they take in memory. That's why you can't use plain instances as member variables here. Their size would differ with every implementation of the pizza builder.
Well i think that's the idea here anyway. In this example they always use the same class for pizza, but you could implement a factory that produces pizzas with whole different features by deriving from the base pizza. And then you need a pointer in the abstract pizza builder to hold the pizza.
Upvotes: 3