Not all instances of a type can be copied to another instance of the same type with an =
sign.
For example, although it may work on ints:
int x = 0;
int y = 5;
x = y; //x is now: 5
It would not work on char arrays:
char x[32]="data to overwrite";
char y[32]="new data";
x = y; //incorrect
or other arrays:
int x[5] = {1,2,3,4,5};
int y[5] = {6,7,8,9,10};
x = y; //incorrect
or char*s:
char* x="data to overwrite";
char* y="new data";
x = y; //incorrect
How can I write an overloaded function that will allow me to do the following?
int x = 0;
int y = 5;
Copy(x,y); //x is now: 5
char x[32]="data to overwrite";
char y[32]="new data";
Copy(x,y); //x is now: "new data"
int x[5] = {1,2,3,4,5};
int y[5] = {6,7,8,9,10};
Copy(x,y); //x is now: {6,7,8,9,10}
char* x="data to overwrite";
char* y="new data";
Copy(x,y); //x is now: "new data"
*I'll assume that any abstract data types do the necessary work in their overloaded assignment operator(or from the shallow copy provided by the compiler)
Why do you need to do this?
In order to easier test portions of a legacy C code base, I'd like to generate some C++ wrappers around a few components. Due to the strange design of the C code, there is a lot of indirection that I would like to get rid of. As such, it would be a lot easier to copy variables into another instance using a simple Copy
function instead of parsing out the types, and deciding how to make the appropriate copy into the other instance variable.