Constructor and object declaration

Is the constructor called whenever an object is declared or is there an exception
when pointers are used such as below?

 
  Box *ptrBox;


Is the constructor called whenever an object is declared
1
2
3
Box box; //Default constructor called: constructor always called on object declaration
Box* box = new Box; //Default constructor called: we explicitely said to allocate memory and construct object
Box* box; //No constructors called: box is not an object of type Box. It is a pointer. Which is trivial type so it is not initialized at all by default 


is there an exception
Invalid pointer use does not lead to exception. It leads to UB (and most likely crash)
Make sure that pointer is pointing to something before you use it. Or make it null pointer if you need pointer which does not points to anything (you can easily test if pointer is null)
Thank you!
Topic archived. No new replies allowed.