constructor
Sep 9, 2013 at 12:49am UTC
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.
}
Sep 9, 2013 at 2:13am UTC
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.
Sep 9, 2013 at 2:45am UTC
but even then there are no values in p, the values are in the ctor and after line 9 the valuse will be removed
Sep 9, 2013 at 3:31am UTC
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 Sep 9, 2013 at 3:32am UTC
Topic archived. No new replies allowed.