Do I need to delete?
-
Hi all, my question is if I define a class myClass with destructor, assign operator (=) and copy constructor, etc etc. If I later use my class like this: myClass &a=*new myClass(whatever); myClass &b=a; Do I have to explicitly delete b? I've read you should only delete a class if you have instantiated it with a new. I know that behind the curtains b is created with a new call inside the dll, but do I have to delete it explicitly from my app? thanks
-
Hi all, my question is if I define a class myClass with destructor, assign operator (=) and copy constructor, etc etc. If I later use my class like this: myClass &a=*new myClass(whatever); myClass &b=a; Do I have to explicitly delete b? I've read you should only delete a class if you have instantiated it with a new. I know that behind the curtains b is created with a new call inside the dll, but do I have to delete it explicitly from my app? thanks
gumi_r@msn.com wrote:
myClass &a=*new myClass(whatever); myClass &b=a;
since you've dynamically allocated memory for an istance of
myClass
, then you have to delete it somewhere. You can call eitherdelete &b
ordelete &a
(of course don't call both). Cheers :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
gumi_r@msn.com wrote:
myClass &a=*new myClass(whatever); myClass &b=a;
since you've dynamically allocated memory for an istance of
myClass
, then you have to delete it somewhere. You can call eitherdelete &b
ordelete &a
(of course don't call both). Cheers :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
Ok thanks. That makes sense as I'm already deleting any pointers I need to inside my copy constructor. Next step would be the assignment operator. Say for example y have: myClass &a=*new myClass(whatever); myClass &b=*new myClass(whatever); myClass &c=a+b; do I have to delete &c? thanks for any replies.
-
Ok thanks. That makes sense as I'm already deleting any pointers I need to inside my copy constructor. Next step would be the assignment operator. Say for example y have: myClass &a=*new myClass(whatever); myClass &b=*new myClass(whatever); myClass &c=a+b; do I have to delete &c? thanks for any replies.
gumi_r@msn.com wrote:
myClass &c=a+b;
IMHO you should not do this, because you're trying to assign a reference to a temporary object. For instance, the following is noy allowed(the compiler complains about):
int &i= *new int(5);
int &j=i;
int &k=i+j;// ERRORBTW: Doing some experimentation? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
gumi_r@msn.com wrote:
myClass &c=a+b;
IMHO you should not do this, because you're trying to assign a reference to a temporary object. For instance, the following is noy allowed(the compiler complains about):
int &i= *new int(5);
int &j=i;
int &k=i+j;// ERRORBTW: Doing some experimentation? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
It is not illegal if you use a
const
reference.Steve
-
It is not illegal if you use a
const
reference.Steve