Hello DaniWeb, I'm relatively new to DaniWeb, and recently decided to come back to an attempt at learning to C++. I just finished reading a particular section on OOP etc. and i have some questions about pointers to objects: like:

MyClass *Object;

Can someone please:

1) Explain why this is used - what are the pros/cons?
2) Perhaps provide a link to a good tutorial on it? Or provide some background information on how to use this correctly.

Any help would be greatly appreciated, as you can see, im a lil bit of a newb (:

Dani AI

Generated

Notes expanding on and :

Pointers are simply variables that hold addresses. They exist so objects can have dynamic lifetime, support polymorphism (pointer-to-base), express “maybe/no object” semantics, and enable certain data structures without copying. The trade-off is safety: raw pointers require manual lifetime management and can produce leaks, dangling pointers, and double-deletes if ownership is unclear.

Ownership and modern best practice:

  • Make ownership explicit and use RAII. Prefer automatic storage for simple objects and smart pointers for dynamic lifetime.
  • Use std::unique_ptr for exclusive ownership, std::shared_ptr only when ownership must be shared, and std::weak_ptr to break cycles or observe without owning.
  • Non-owning references or raw pointers are fine as observers, but document that they do not manage lifetime.

Minimal modern example:

#include <memory>

struct Base { virtual ~Base() = default; };
struct Derived : Base {};

std::unique_ptr<Base> p = std::make_unique<Derived>();

Practical rules and common pitfalls:

  • Prefer references (const&) for parameters that must exist; use pointers when nullability or reseating is required.
  • For polymorphic cleanup, ensure the base class has a virtual destructor.
  • Do not return the address of a local (automatic) variable — it becomes a dangling pointer.
  • Prefer std::vector/std::array/std::span for collections over manual new[]/delete[].

Quick checklist:

  • Prefer automatic variables and RAII.
  • Avoid naked new/delete.
  • Document ownership semantics.
  • Use tools: compile with -Wall -Wextra, run AddressSanitizer or similar, and use static analysis to catch leaks and UB.

Recommended Answers

All 4 Replies

>1) Explain why this is used - what are the pros/cons?
That's quite a broad question. Can you be more specific about what you learned and what part of it is confusing?

is a tutorial that I wrote. It's more of an overview of pointers in general, but it might answer some of your questions.

commented: put simply, that tut is all anyone will ever need for pointers :) +2

hi all,

I Asked my self this question last time ,
see,
our memory strategy as stack , or as heap , if you work as simple project you can use stack - , but it is slow when the project big or be as enterprise one,

so the heap strategy will be the best ,
so we use pointers
like

//HEAP
yclass *object = new Myclass(); //object is on the heap
//STACK
Myclass object; //object is on the stack

hi all,

I Asked my self this question last time ,
see,
our memory strategy as stack , or as heap , if you work as simple project you can use stack - , but it is slow when the project big or be as enterprise one,

so the heap strategy will be the best ,
so we use pointers
like

//HEAP
Myclass *object = new Myclass(); //object is on the heap
//STACK
Myclass object; //object is on the stack

commented: The exact answer i needed +2

Hmm thanks to both of you that was extremely helpful, especially that lengthy tutorial - exactly what i needed right now :) Also, palunix, ty that finally got my head round the issue in particular.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.