Copying Class Data

Jul 13, 2009 at 3:25am
closed account (S6k9GNh0)
Scenario:

I make a pointer called pConfig to a class. I then assign it to dynamically allocated memory on the heap.I then load a text file into pConfig class.

I then do the same thing for another pointer call pConfigBak. I then try and copy data from pConfig to pConfigBak for a temporary backup of the configuration file.

Problem:

When I call *pConfigBak = *pConfig; with the default = operator, it simply copies the pConfig pointer to pConfigBak so as a result, anything that happens to pConfig happens to the backup. I clearly don't want this. Same thing with default copy constructor.

I tried using memcpy to exchange the data via binary (or however they're done). This didn't work as it gives the same result as the above.

Am I missing something? I would normally make my own but that's not currently possible as I have private variables needing to be copied which cannot be moved to the public section. Any solutions?
Last edited on Jul 13, 2009 at 3:26am
Jul 13, 2009 at 3:52am
Try to rewrite the operator '=' by yourself.
Jul 13, 2009 at 3:54am
closed account (S6k9GNh0)
... I can't. I was hoping I could get something that copies binary or something as to avoid private variables. Although at best, I'll simply change the rules on the library. Anything?
Last edited on Jul 13, 2009 at 3:57am
Jul 13, 2009 at 3:59am
You mean you can't change the class definition, but you want copy a private member from one instance to another instance?
Jul 13, 2009 at 4:37am
closed account (S6k9GNh0)
Yes. Let's keep it at that. Are there any solutions as to how to copy that variable?
Jul 13, 2009 at 5:26am
- -b, It's dangerous game...
What about this one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <fstream>
#include <iostream>

class ProtectVip
{
private:
	char Vip[20];
public:
	void SetVip(char *pInput) {strcpy(Vip, pInput);}
	void Print() {std::cout<<Vip<<std::endl;}
};

//--------------------------------------------------------------------------------------------

int main()
{
	ProtectVip *pConfig = new ProtectVip();
	ProtectVip *pConfigBak = new ProtectVip();
	ProtectVip *pConfigFak = new ProtectVip();

	pConfig->SetVip("AnyOne");

    *pConfigFak = *pConfigBak;
	pConfigBak->SetVip("S");
	int VipStart = memcmp((char *)pConfigFak, (char *)pConfigBak, sizeof(ProtectVip));
	memcpy(((char *)pConfigBak)+VipStart, ((char *)pConfig)+VipStart, 20); //I don't know how to get the member's length
	
    pConfigBak->Print();

	delete pConfigBak;
	delete pConfig;
	delete pConfigFak;
	return 0;
}



My boss will kill me if he see this..............
Jul 13, 2009 at 5:29am
I'm going to assume this is in a library you're using and not code you wrote yourself. If I'm wrong on that assumption then the solution is just to redesign this class to better suit your needs.


"you want copy a private member from one instance to another instance?"

Yes. Let's keep it at that. Are there any solutions as to how to copy that variable?


Even if there are, you shouldn't use them. It side-steps encapsulation, which could cause severe side-effects in whatever lib you're using. Private stuff is private for a reason: you're not supposed to mess with it. Doing so could (and probably would) cause "bad things" to happen.

Provided 'pConfigBak' and 'pConfig' are in fact pointers to objects (Foo*) and not pointers to pointers to objects (Foo**) this tells me that 'Foo' does some sort of internal reference counting. Normally reference counting is broken when you modify one of the objects (ie: you shouldn't be having this problem). But look around in the lib for a way to break the reference count and isolate the object so that it's not shared with any others.

Alternatively, look for a Copy() style function and use that instead of the assignment operator.

I'm afraid I can't get more specific than that without knowing what lib you're referring to or seeing the class in question.

If there are no such isolation or copy functions, you're SOL. Nothing you can do but start shopping around for a different lib.
Jul 13, 2009 at 7:01am
closed account (S6k9GNh0)
I wrote a majority of it. It's private because I don't want regular people using the variables regularly because it can screw up certain functions which rely on data being changed when other data is changed which isn't what I want the user to even begin to deal with because of confusion and such. I could move towards the public section at last hope but surely there is something I could do to copy raw data. When I completed the recent additions for a project, I noticed that the gentlemen who wrote the foundation for the library didn't allow the copy constructor or the assignment operator. I disregarded this section of code and tried to use it anyways which resulted in this.

It's just a regular fucking class that can write, read, and other cool features. And player6, thanks for the code snippet but in reality that snippet doesn't do anything I want. Remember when I mentioned that I tried this:

*pConfigBak = *pConfig memcpy(pConfigBak, pConfig, sizeof(ConfigClass));//Needs void* casting? ConfigClass *pConfigBak = new ConfigClass(*pConfig);

They ALL result in me having pConfigBak as a reference to pConfig for no apparent reason that I can find. The only thing I can do so far is load the text file twice for each variable but I'm trying to avoid this since the parsing algorithms are somewhat thick and the configuration is long. I don't need to double the time of parsing although it's only a second or two.
Jul 13, 2009 at 11:34am
I don't understand why you say you cannot write operator=.

This works just fine:

1
2
3
4
5
6
7
8
9
10
11
class Foo {
    /* private */
    int x;
    int y;
  public:
    Foo& operator=( Foo f ) {
       std::swap( x, f.x );
       std::swap( y, f.y );
       return *this;
    }
};


Jul 13, 2009 at 4:28pm
When I completed the recent additions for a project, I noticed that the gentlemen who wrote the foundation for the library didn't allow the copy constructor or the assignment operator. I disregarded this section of code and tried to use it anyways which resulted in this.


Well there's your problem. If copying is disabled it's usually disabled for a reason.

The bottom line is this: The class simply does not do what you want it to do. You can rewrite/change the class to add this functionality, which may require rewriting/changing parent classes to allow copying. But if that is not an option, then you just can't copy the object. At least not in a safe manner.

You should not even be considering things like memcpy. Talk about a disaster waiting to happen. Especially if there's a class hierarchy in the picture.
Jul 13, 2009 at 4:55pm
closed account (S6k9GNh0)
@jsmith: Maybe I wrote something incorrectly without noticing. I suppose I can take a step back and see what I fuxed up on. Though I could have sworn I did this already.
Topic archived. No new replies allowed.