Jump to content
  • Advertisement
Sign in to follow this  
disanti

Problem deleting a class.

This topic is 5427 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, Here is an example of what I'm having trouble with:
myclass1 class1 = new myclass1;
myclass1 class2;

class2.CopyObject(class1);
delete class1; //This exits the program for no reason at all, no message or anything.

myclass1:
myclass1::CopyObject(myclass1 m)
{
   x = m.x;
   y = m.y;
   z = m.z;
   ...
}

Why does this exit my application? Thanks~ John DiSanti

Share this post


Link to post
Share on other sites
Advertisement
wtf? is the "myclass1::CopyObject(myclass1 m)" taking a pointer as an argument ?!

that code shouldn't even compile.

(it is C++ isn't it?)

Share this post


Link to post
Share on other sites
Where did you get a pointer?
There is no asterick in the function parameter.
If there is a class, it is C++.

Share this post


Link to post
Share on other sites
indeed. the delete operator only works with pointers, as does the new operator. applying them the way you do is considered very unfortunate. instead, it should be implemented something like this :

myclass1* class1 = new myclass1(); // default constructor
myclass1* class2;

class2.CopyObject(class1);
delete class1;

//------------- in the other file
void myclass1::CopyObject(myclass1* m)
{
x = m->x;
y = m->y;
z = m->z;
}

the * indicates a pointer object (read the referenced tutorial). and make sure a return type is infront of your function (should be void). however, you would usually not be using a "CopyObject" method, implement it through either a copy constructor or overloading the '=' operator.
<edit :: fixed late-night grammatical mistakes>

Share this post


Link to post
Share on other sites
This code is incorrect:

Quote:
Original post by stormrunner

myclass1* class1 = new myclass1(); // default constructor
myclass1* class2;

class2.CopyObject(class1);
delete class1;

//------------- in the other file
void myclass1::CopyObject(myclass1* m)
{
x = m->x;
y = m->y;
z = m->z;
}



class2 should be a myclass1, not a myclass1*

Share this post


Link to post
Share on other sites
Quote:
Original post by silvermace
Quote:
Original post by disanti
If there is a class, it is C++.


Java isn't C++.


True. However, Java doesn't have the :: operator, does it?

Share this post


Link to post
Share on other sites
Quote:
Original post by daerid
Quote:
Original post by silvermace
Quote:
Original post by disanti
If there is a class, it is C++.


Java isn't C++.


True. However, Java doesn't have the :: operator, does it?


no, but thats not what you said, is it ;-)

Share this post


Link to post
Share on other sites
Quote:
Original post by stormrunner
indeed. the delete operator only works with pointers, as does the new operator. applying them the way you do is considered very unfortunate. instead, it should be implemented something like this :

myclass1* class1 = new myclass1(); // default constructor
myclass1* class2;

class2.CopyObject(class1);
delete class1;

//------------- in the other file
void myclass1::CopyObject(myclass1* m)
{
x = m->x;
y = m->y;
z = m->z;
}

the * indicates a pointer object (read the referenced tutorial). and make sure a return type is infront of your function (should be void). however, you would usually not be using a "CopyObject" method, implement it through either a copy constructor or overloading the '=' operator.
<edit :: fixed late-night grammatical mistakes>


...would result in a runtime-error

Where does class2 get its memory from ?

class2 = new myclass1();

Should be placed somewhere before calling CopyObject.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!