Guest User

Untitled

a guest
Jun 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. void getObjData( LargeObj& a )
  2. {
  3. a.reset() ;
  4. a.fillWithData() ;
  5. }
  6.  
  7. int main()
  8. {
  9. LargeObj a ;
  10. getObjData( a ) ;
  11. }
  12.  
  13. LargeObj getObjData()
  14. {
  15. LargeObj a ;
  16. a.fillWithData() ;
  17. return a ;
  18. }
  19.  
  20. int main()
  21. {
  22. LargeObj a = getObjData() ;
  23. }
  24.  
  25. typedef std::vector<HugeObj> LargeObj ;
  26.  
  27. #include <iostream>
  28. class X
  29. {
  30. public:
  31. X() { std::cout << "X::X()" << std::endl; }
  32. X( X const & ) { std::cout << "X::X( X const & )" << std::endl; }
  33. X& operator=( X const & ) { std::cout << "X::operator=(X const &)" << std::endl; }
  34. };
  35. X f() {
  36. X tmp;
  37. return tmp;
  38. }
  39. int main() {
  40. X x = f();
  41. }
  42.  
  43. LargeObj&& getObjData()
  44. {
  45. LargeObj a ;
  46. a.fillWithData() ;
  47. return a ;
  48. }
  49.  
  50. int main()
  51. {
  52. LargeObj a = getObjData(); // calls the move constructor
  53. }
  54.  
  55. LargeObj getObjData()
  56. {
  57. return LargeObj( fillsomehow() );
  58. }
  59.  
  60. std::auto_ptr<LargeObj> getObjData()
  61. {
  62. std::auto_ptr<LargeObj> a(new LargeObj);
  63. a->fillWithData();
  64. return a;
  65. }
  66.  
  67. int main()
  68. {
  69. std::auto_ptr<LargeObj> a(getObjData());
  70. }
  71.  
  72. LargeObj::LargeObj() :
  73. m_member1(),
  74. m_member2(),
  75. ...
  76. {}
  77.  
  78. LargeObj::LargeObj()
  79. {
  80. // (The body of fillWithData should ideally be re-written into
  81. // the initializer list...)
  82. fillWithData() ;
  83. }
  84.  
  85. int main()
  86. {
  87. LargeObj a ;
  88. }
  89.  
  90. class LargeObj : public std::vector<HugeObj> {
  91. // constructor that fills the object with data
  92. LargeObj() ;
  93. // ... other standard methods ...
  94. };
  95.  
  96. LargeObj* getObjData()
  97. {
  98. LargeObj* ret = new LargeObj;
  99. ret->fillWithData() ;
  100. return ret;
  101. }
Add Comment
Please, Sign In to add comment