fork download
  1. #include <stdio.h>
  2.  
  3. class A {
  4. public:
  5. A() { a = 0; printf("A()\n"); }
  6.  
  7. int a;
  8. };
  9.  
  10. class B : virtual public A {
  11. };
  12.  
  13. class C : public B {
  14. public:
  15. C() {}
  16. C(const C &from) : B(from) {}
  17. };
  18.  
  19. template<typename T>
  20. void
  21. test() {
  22. T t1;
  23. t1.a = 3;
  24. printf("pre-copy\n");
  25. T t2(t1);
  26. printf("post-copy\n");
  27. printf("t1.a=%d\n", t1.a);
  28. printf("t2.a=%d\n", t2.a);
  29. }
  30.  
  31. int
  32. main() {
  33. printf("B:\n");
  34. test<B>();
  35.  
  36. printf("\n");
  37.  
  38. printf("C:\n");
  39. test<C>();
  40. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
B:
A()
pre-copy
post-copy
t1.a=3
t2.a=3

C:
A()
pre-copy
A()
post-copy
t1.a=3
t2.a=0