3

I have two questions.

1) What does a constructor really do? What happens if we don't use constructors while declaring an instance?

2) Can you tell me the difference between these two?

A a(1,2)

A *a = new A(1,2)

Best regards.

platypus
  • 723
  • 2
  • 16
  • 36
  • 1
    possible duplicate of [What is difference between instantiating an object using new vs. without](http://stackoverflow.com/questions/3673998/what-is-difference-between-instantiating-an-object-using-new-vs-without) – fredoverflow Dec 18 '10 at 08:36

5 Answers5

8

A constructor initializes the member variables of the class so that it is ready for use. The result of not using a constructor when declaring an instance varies on context.

If you are allocating a variable on the heap, like so:

A *a;

a will point to a random address in memory until it is assigned to NULL or 0, or an existing or new instance of a class, e.g.:

A *a = new A(1, 2);

If you are allocating a variable on the stack, the following syntax is used:

A a(1, 2); // if parameters are used
A a; // if no parameters are used

Both of the above call the constructor of class A, allocating an instance of it on the stack. So, that answers both your questions - your first example allocates an instance of A on the stack, and the second allocates an instance of A on the heap.

Jake Petroules
  • 21,796
  • 34
  • 136
  • 218
3

A constructor is a function with the same name as the class. The main purpose of a constructor is to initialize member variables of a newly-created object to some default values. It might also call other initialization functions.
Just like other functions in C++, constructors can be overloaded. (An overloaded function is a new version of an existing function with different arguments). For example, consider the following Point class with three constructors:

class Point
{
public:
    Point() : x_(0), y_(0) { }
    Point(int x, int y) : x_(x), y_(y) { }
    Point(const Point& p) : x_(p.x), y_(p.y) { }
private:
    int x_;
    int y_;
};

The first constructor initializes the co-ordinates to zero, while the second allows the user to specify their default values:

void create_and_destroy_points ()
{
   Point p1; // => x_ = 0, y_ = 0
   Point p2(100, 200); // x_ = 100, y_ = 200
}

When declared that way, those Point objects are allocated on the stack. That means the memory allocated to them will be automatically released when the create_and_destroy_points function returns. In other words, p1 and p2 are of no use outside the function.
Objects can also be allocated on the heap. This allocates the objects at run time, and they can then survive across various function calls. They are allocated with the new keyword and continue to live until they are deallocated with delete. Failing to delete a heap allocated object after its use will lead to a memory leak.

Point* create_point ()
{
   Point* p = new Point(100, 200);
   return p;
}

Point* p = create_point();
// use p 
delete p;

The third constructor is a copy constructor. It is invoked when a new object is constructed by copying another object:

Point p1;
Point p2 = p1; // calls the copy constructor for p2 with p1 as argument.
Point p3(p1);  // alternate syntax
mskfisher
  • 3,028
  • 3
  • 31
  • 47
Vijay Mathew
  • 25,329
  • 3
  • 54
  • 90
2

To answer #2, your first example allocates a variable 'a' of type A on the stack, while your second one allocates a pointer '*a' to type A. The pointer is given a starting value, an address that points to dynamic memory. In both cases, A::A() constructor is called taking 2 parameters

greatwolf
  • 18,899
  • 13
  • 64
  • 102
2

1) What does a constructor really do?

Construction of an object consists of two parts:

  1. Allocating the required amount of memory

  2. Execution of the code in constructor

What happens if we don't use constructors while declaring an instance?

Constructors are always executed, even if you don't explicitly invoke them. For example:

std::string a;

Here the default constructor of the string class will be called.

2) Can you tell me the difference between these two?

A a(1, 2);

This code calls a user-defined constructor.

A a;

This code calls the default constructor.

StackedCrooked
  • 32,392
  • 40
  • 137
  • 267
2

1) Constructors should be used solely for the purpose of the member variables initialization.

class A {
public:
    A() { a = 0; b = 0; }
    A(int a, int b) { this->a = a; this->b = b; }

private:
    int a;
    int b;
};

In the class above, we have two constructors, each initializes member variables, one with the zero's and other with the given arguments.

Class can have any number of constructors. When creating class instance you must always call one of them, for example:

A a1;    // uses first constructor, i.e. A::A() 
A a2();  // also uses first constructor

A* a3 = new A(1, 2); // uses second constructor, i.e. A::A(int a, int b) 
A a4(1, 2);          // also uses second constructor

2) Declaration:

A a(1, 2)

declares variable which holds instance bound to the scope, which means that instance will be automatically deleted when program exits that scope. For example:

void fn() {
    A a(1, 2);
    ...
    ...
}

at the begging of the function instance a is constructed which will be automatically deleted when we exit that function.

In case of :

A *a = new A(1,2)

variable a is declared and it points to the newly created instance of A. You must manually delete instance on which a points with "delete a", but your instance can survive the scope in which a is declared. For example:

A* fn()
{
    A *a = new A(1,2)
    return a;
}

here, function fn returns an instance created inside its body, i.e. instance survives function scope.

mvladic
  • 1,139
  • 2
  • 12
  • 15