code_junkie

How to pass method result as parameter to base class constructor in C++?

Nov 13th, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. class Base
  2. {
  3. public:
  4.  
  5. Base(string S)
  6. {
  7. ...
  8. };
  9. }
  10.  
  11. class Derived: Base
  12. {
  13. public:
  14. int foo;
  15. string bar()
  16. {
  17. return stringof(foo); // actually, something more complex
  18. };
  19.  
  20. Derived(int f) : foo(f), Base(bar())
  21. {
  22. };
  23. }
  24.  
  25. class Derived: Base
  26. {
  27. public:
  28. int foo;
  29.  
  30. static string bar(int f)
  31. {
  32. return stringof(f); // actually, something more complex
  33. }
  34.  
  35. string bar()
  36. {
  37. return bar(foo);
  38. };
  39.  
  40. Derived(int f) : Base(bar(f)) , foo(f)
  41. {
  42. };
  43. }
  44.  
  45. Derived(int f) : Base(stringof(f)), foo(f) {}
  46.  
  47. class Base {
  48. public:
  49. Base(string S) { ... }
  50. void bat() { ... }
  51. };
  52.  
  53. class Derived {
  54. Base *base;
  55. int foo;
  56.  
  57. public:
  58. Derived(int f) : base(NULL), foo(f) {
  59. base = new Base(bar());
  60. }
  61. ~Derived() {
  62. delete base;
  63. }
  64.  
  65. string bar() {
  66. return stringof(foo); // actually, something more complex
  67. }
  68.  
  69. void bat() {
  70. base->bat();
  71. }
  72. };
  73.  
  74. class Derived: Base
  75. {
  76. public:
  77. int foo;
  78. string bar()
  79. {
  80. return stringof(foo); // actually, something more complex
  81. };
  82.  
  83. Derived(int f) : foo(f), Base(bar())
  84. {
  85. };
  86. }
  87.  
  88. class Derived: Base
  89. {
  90. public:
  91. int foo;
  92. static string bar(int f)
  93. {
  94. return stringof(f); // actually, something more complex
  95. };
  96.  
  97. Derived(int f) : Base(bar(f)), foo(f)
  98. {
  99. };
  100. }
Add Comment
Please, Sign In to add comment