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?
Edit:
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. :)