bitwise copy constructor


if i have a class that stores 3 integers, is it true that using the default bitwise copy constructor would be faster then overloading the assignment operator and explicitly transfering the integers over?
The default copy constructor is NOT bitwise. It calls the copy constructors of every element for you.


would the default copy constructor be faster then the (explicit) one we would write?
It would not be slower.


1
2
3
4
5
6
struct A { int a,b; };
struct B { int a,b; B(const B& x):a(x.a),b(x.b){} };
...
A a1; B b1;
A a2(a1);
B b2(b1);


I wouldn't be surprised if the last two lines yield in exact the same machine code.

However, this may (or may not be) be faster:

1
2
3
4
struct C { int a,b; }
...
C c1;
C c2; memcpy(&c2, &c1, sizeof(c1));
Last edited on
Topic archived. No new replies allowed.