Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ pointer to class

Tags:

Can anyone tell me what the difference is between:

Display *disp = new Display(); 

and

Display *disp; disp = new Display(); 

and

Display* disp = new Display(); 

and

Display* disp(new Display()); 
like image 439
Zeeshan Rang Avatar asked Sep 03 '09 05:09

Zeeshan Rang


2 Answers

The first case:

Display *disp = new Display(); 

Does three things:

  1. It creates a new variable disp, with the type Display*, that is, a pointer to an object of type Display, and then
  2. It allocates a new Display object on the heap, and
  3. It sets the disp variable to point to the new Display object.

In the second case:

Display *disp; disp = new GzDisplay(); 

You create a variable disp with type Display*, and then create an object of a different type, GzDisplay, on the heap, and assign its pointer to the disp variable.

This will only work if GzDisplay is a subclass of Display. In this case, it looks like an example of polymorphism.

Also, to address your comment, there is no difference between the declarations:

Display* disp; 

and

Display *disp; 

However, because of the way C type rules work, there is a difference between:

Display *disp1; Display* disp2; 

and

Display *disp1, disp2; 

Because in that last case disp1 is a pointer to a Display object, probably allocated on the heap, while disp2 is an actual object, probably allocated on the stack. That is, while the pointer is arguably part of the type, the parser will associate it with the variable instead.

like image 131
Daniel Pryden Avatar answered Sep 25 '22 20:09

Daniel Pryden


// implicit form // 1) creates Display instance on the heap (allocates memory and call constructor with no arguments) // 2) creates disp variable on the stack initialized with pointer to Display's instance Display *disp = new Display();  // explicit form // 1) creates Display instance on the heap (allocates memory and call constructor with no arguments) // 2) creates disp variable on the stack initialized with pointer to Display's instance Display* disp(new Display());  // 1) creates uninitialized disp variable on the stack // 2) creates Display instance on the heap (allocates memory and call constructor with no arguments) // 3) assigns disp with pointer to Display's instance Display *disp; disp = new Display(); 

Difference between explicit and implicit forms of initialization will be seen only for complex types with constructors. For pointer type (Display*) there is no difference.

To see the difference between explicit and implicit forms check out the following sample:

#include <iostream>  class sss { public:   explicit sss( int ) { std::cout << "int" << std::endl; };   sss( double ) { std::cout << "double" << std::endl; };   // Do not write such classes. It is here only for teaching purposes. };  int main() {  sss ddd( 7 ); // prints int  sss xxx = 7;  // prints double, because constructor with int is not accessible in implicit form   return 0; } 
like image 21
Kirill V. Lyadvinsky Avatar answered Sep 25 '22 20:09

Kirill V. Lyadvinsky