Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic objects in c++ [closed]

I come to c++ from c# and I don't understand what dynamic object is. So imagine you have class A and to create object like A *a = new A() is Normal but what is object a? It's the same thing like an array or what? We can write like a[0] or a[1]? But what going on if we overload operator [] and want to create dynamic object if we have data there and want to get data normally?

like image 353
icegas Avatar asked Jun 21 '17 14:06

icegas


2 Answers

A pointer is just about what it sounds like, it's something which points somewhere else.

Take your example

A* a = new A;

That declares the variable a as a pointer. It points to an instance (an object) of the A class.

Somewhat graphically it can be seen as

+---+      +---------------+
| a | ---> | instance of A |
+---+      +---------------+

In C# (and Java) this is basically the only way to create instances of classes. All variables are (a little simplified) pointers.

In C++ you have other alternatives as well:

A a;

The definition above says that the variable a is an instance of the A class. It's not a reference, it's not a pointers, it is an A object.

Now for another difference between the two above definitions: In the first case the instance of A is created at run-time. The memory is allocated for the instance when the program is running. In the second case, the space for the instance is allocated by the compiler, at the time of compilation. However in both cases the constructor is called at run-time.

like image 156
Some programmer dude Avatar answered Oct 17 '22 11:10

Some programmer dude


If you write A * a = new A() the default constructor of the class A is called and it dynamically allocates memory for one object of the class A and the address of the memory allocated is assigned to the pointer a. So a points an object of the class A and not an array. However, if you want an array of A objects dynamically, then you'll have to write something like this A * a = new A[10]. This will allocate memory of 10 objects of A. And you can write a[0], a[1], ... a[9] to access the objects of the array.

Now, what would happen if we overload the [] operator? If we overload [] operator, then it should mean that the class A has some sort of array inside, and by writing a[1], where a is an object of class A, we want to get the element situated at second index. (Speaking generally, you can mean anything else using the subscript operator, but you should try to stick to the original meaning of the subscript operator). But we cannot invoke [] operator on pointers as it would mean to dereference the pointer. So to invoke the subscript operator on a pointer of an object, we have to explicitly call the subscript operator. Like this:

A * a = new A;
cout << a->operator[](0) << endl;

It creates one object of the class A, and writing a->operator[](0). It explicitly calls the overloaded subscript operator. What if we create an array dynamically?

A * a = new A[6];
cout << a[0][0] << endl;

The first subscript operator is for getting the first object in the array of objects. The second subscript operator is calling the overloaded subscript operator of the object a[0]

EDIT: I was wrong about invoking subscript operator of a class A. Thanks to jschultz410, who corrected me.

like image 43
Redwanul Haque Sourave Avatar answered Oct 17 '22 11:10

Redwanul Haque Sourave