Jump to content
  • Advertisement
Sign in to follow this  
Helderash

Class Pointer Problem

This topic is 4150 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

Hey folks! I'm having a bit of trouble making a copy of a class. I'm using the Irrlicht 3D graphics engine in a project, and have declared pointers to one of the Irrlicht classes as follows:
SMeshBuffer *buffer;
SMeshBuffer *bufferOrig;
I've then filled the variables of "buffer" accordingly, and want "bufferOrig" to be a copy of "buffer" in it's original form, that won't change as I change "buffer". I tried doing the following after initialising "buffer":
bufferOrig = buffer;
This seems to pass buffer's pointer to bufferOrig however, and changing buffer also changes bufferOrig. How would you go about passing a copy of buffer to bufferOrig in such a way that altering buffer does not alter bufferOrig? Thankyou very much for the help, it is really appreciated!

Share this post


Link to post
Share on other sites
Advertisement
You are just making a copy of the pointer, not a copy of the SMeshBuffer struck

So dereference it and do the copy.

*bufferOrig = *buffer;

theTroll

Share this post


Link to post
Share on other sites
Quote:
Original post by TheTroll
You are just making a copy of the pointer, not a copy of the SMeshBuffer struck

So dereference it and do the copy.

*bufferOrig = *buffer;

theTroll


Before you do what TheTroll said, make sure bufferOrig is valid.

ie:
buffer = new SMeshBuffer;
...setup buffer...
bufferOrig = new SMeshBuffer;
*bufferOrig = *buffer;

~zix

Share this post


Link to post
Share on other sites
Thankyou both very much, I had tried the *bufferOrig = *buffer, but had forgotten bufferOrig = new SMeshBuffer. It's all working perfectly now though.

Thankyou both again! :D

Share this post


Link to post
Share on other sites
If you're going to create a copy you might as well use the copy constructor for the class when you allocate the new object.

bufferOrig = new SMeshBuffer(*buffer);

Also <insert standard lecture about using smart pointers here>.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!