How can I clone an object in C++, so it is a seperate object, but exactly the same as the original?

Thanks,
- Moshe

Recommended Answers

All 2 Replies

You can use the default copy constructor, like this:

struct myObject {
  int data[10];
};

int main() {
  myObject a;

  /* Do stuff to 'a' */

  myObject b( a );

  /* 'b' is identical to 'a' now */

  return 0;
}
commented: Absolutely right :P +3

Thanks for the prompt and informative response!

- Moshe

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.