constructor

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  class A {
private:
int* a;
public:
A(){
a=new int[3]; 
for (int i=0; i<3; i++) 
a[i] = i;
}
};

int main() {
A p;
//What changes should be added in order to p[0] will be 0 after the constructor has been called. 
}
p[0] is not valid syntax since you haven't defined operator[]. Did you maybe mean to do that and have it access the value of a?

You'll also want to define a destructor that frees the array you're allocating in the constructor.
but even then there are no values in p, the values are in the ctor and after line 9 the valuse will be removed
First : It is better to implement a destructor that can delete the data you've been borrowed?

second :
there are no values in p, the values are in the ctor and after line 9 the valuse will be removed


What are you mean by that ?

Last edited on
Topic archived. No new replies allowed.