pointers

Started by
19 comments, last by BloodLust666 17 years, 7 months ago
I have an instance where i need to take a pointer, create a new pointer using "new" and copy the values from the passed pointer into the new one so the old one can be deleted and the new one will retain the values from the old one.
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Advertisement
Assuming C++. Implement a copy constructor for your class then just do:

MyClass *newPointer = new MyClass( *oldPointer );//since the values have been copied//you can happily delete the old instance without data lossdelete oldPointer;


here's a little tut on Copy Constructors: http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html

-me
What you described is usually referred to as 'cloning.' It's a common idiom in C++.

You have to write a clone() member function yourself. Yuo can sometime leverage the copy constructor.

Stephen M. Webb
Professional Free Software Developer

A _very_ important thing to remember is that if your class has member variables which are pointers, those variables will also have to be copy-constructed.

-me
so i would have to create my own copy constructor and copy constructor those pointers as well?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by EvilKnuckles666
so i would have to create my own copy constructor and copy constructor those pointers as well?


correct. if you just assign the values of the member variable pointers then you're just copying a memory address, not the data itself. So you'll have to implement copy-constructors for all the member variable objects also. If they're dynamically allocated arrays, you'll have to new [] and then memcpy the data, etc.

-me
does this look correct?

class myClass
{
myClass(myClass &Copy)
{
m_pOne = *Copy.m_pOne;
m_pTwo = *Copy.m_pTwo;
m_Three = Copy.m_Three;
};

// ...

};
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
The "deep copy" issue is one of the most problamatic in all of programming, because there is no such thing as a "correct" one-size-fits-all solution.

But for your case I think you want to implement either the "deepest copy reasonable" / "copy everything I can create" version or the "copy everything I own" version. I would start with the "copy everything I own" version.

Which simply means this, every single thing you destroy in your destructur, you should create a new copy instance of in your assignment operator and copy constructor.

So if you destructor does this:

delete x;
// or something like this:
x.Dispose();
x = null;

in your copy functions you should use:

x = new XsType(*(rhs.x));

not:

x = rhs.x;
ok, i got that much, but how do i copy a value from one pointer to the value of another?

i thought this: but it doesn't work

*newpointer = *oldpointer
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
what's wrong with this code? it's giving me an error


myClass(myClass &Copy)
{
m_pOne = NULL;
m_pTwo = NULL;

memcpy((void*)m_pOne , (void*)Copy.m_pOne, sizeof(myClass));
memcpy((void*)m_pTwo , (void*)Copy.m_pTwo, sizeof(myClass));
m_Three= Copy.m_Three;
};
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML

This topic is closed to new replies.

Advertisement