fork download
  1. // C++
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define PRINT_CHAR(chr) printf("\t"#chr"=%c\n",chr)
  5. const unsigned CORRESPOND[] = {0,2,3,1}; // 對應順序 A->第0個字元, B->第2個字元 , ...
  6. class TEST
  7. {
  8. private:
  9. char chrData[4];
  10. public:
  11. char &chrA, &chrB, &chrC, &chrD;
  12. TEST(const char* data) :chrA(chrData[CORRESPOND[0]]), chrB(chrData[CORRESPOND[1]]), chrC(chrData[CORRESPOND[2]]), chrD(chrData[CORRESPOND[3]])
  13. {
  14. memcpy(chrData, data, 4 * sizeof(char));
  15. }
  16. TEST(const TEST &cpy) :TEST(cpy.chrData)
  17. {
  18. }
  19. public:
  20. void ShowABCD()const
  21. {
  22. PRINT_CHAR(chrA);
  23. PRINT_CHAR(chrB);
  24. PRINT_CHAR(chrC);
  25. PRINT_CHAR(chrD);
  26. }
  27. void ShowDataArray()const
  28. {
  29. PRINT_CHAR(chrData[0]);
  30. PRINT_CHAR(chrData[1]);
  31. PRINT_CHAR(chrData[2]);
  32. PRINT_CHAR(chrData[3]);
  33. }
  34. };
  35.  
  36. int main()
  37. {
  38. TEST t1("0123");
  39. printf("\nThe Data of t1:\n");
  40. t1.ShowABCD();
  41. t1.ShowDataArray();
  42.  
  43. t1.chrB = 'B';
  44. TEST t2(t1);
  45. printf("\nThe Data of t2:\n");
  46. t2.ShowABCD();
  47. t2.ShowDataArray();
  48. return 0;
  49. }
  50.  
  51.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
The Data of t1:
	chrA=0
	chrB=2
	chrC=3
	chrD=1
	chrData[0]=0
	chrData[1]=1
	chrData[2]=2
	chrData[3]=3

The Data of t2:
	chrA=0
	chrB=B
	chrC=3
	chrD=1
	chrData[0]=0
	chrData[1]=1
	chrData[2]=B
	chrData[3]=3