Public Group

Class Pointer Problem

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

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 on other sites
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 on other sites
Quote:
 Original post by TheTrollYou are just making a copy of the pointer, not a copy of the SMeshBuffer struckSo 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 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 on other sites
Yeah guess I should not assume that people are going to allocate memory first.

theTroll

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>.

• 9
• 13
• 40
• 15
• 11