new operator not understood
-
Hello, If I have a class class A { int *myint; }; I can initialize the above member variable like this int *myint = new int; so that a pointer is initialized on the heap and I will have to delete it. But if I have to initialize the member variable in the constructor of a class how should I do it using new? Thanks Prithaa
-
Hello, If I have a class class A { int *myint; }; I can initialize the above member variable like this int *myint = new int; so that a pointer is initialized on the heap and I will have to delete it. But if I have to initialize the member variable in the constructor of a class how should I do it using new? Thanks Prithaa
Exactly the same but in the constructor:
A::A()
{
myint = new int;
}Don't forget to destroy it in the destructor of your class then.
Cédric Moonen Software developer
Charting control [v1.1] -
Hello, If I have a class class A { int *myint; }; I can initialize the above member variable like this int *myint = new int; so that a pointer is initialized on the heap and I will have to delete it. But if I have to initialize the member variable in the constructor of a class how should I do it using new? Thanks Prithaa
prithaa wrote:
int *myint;
you can try auto_ptr too .. http://visualcpp.org/template_permalink.asp?id=126[^]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
-
prithaa wrote:
int *myint;
you can try auto_ptr too .. http://visualcpp.org/template_permalink.asp?id=126[^]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
You could use a
std::auto_ptr
but should beware of the transfer of ownership on copy semantics. This could mean, for example, that an assignment breaks the object on the right hand side.Steve
-
You could use a
std::auto_ptr
but should beware of the transfer of ownership on copy semantics. This could mean, for example, that an assignment breaks the object on the right hand side.Steve
-
Exactly the same but in the constructor:
A::A()
{
myint = new int;
}Don't forget to destroy it in the destructor of your class then.
Cédric Moonen Software developer
Charting control [v1.1] -
Although you can simply write,
myint = new int;
Be aware while using assignment operator or copy constructor. If you are not cautious, will end up with dangling pointers.
:confused: WTF ?? This was posted more than 3 years ago !! Furthermore, you are replying to me and not to the OP.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++