Avoiding copying variables during initialization in C++ with block -
please code. c++
clang
's block
feature. can code avoid copying? please let me know opinion. practice of avoiding heap.
class element { public: int value[1024]; // here large entity. element() { } }; class world { public: element a; element b; inline world(element& newa, element& newb) { = newa; // source of newa stored in somewhere, copies whole element during assignment. b = newb; } inline world(void(^init)(element& a, element& b)) { init(a, b); // assignment done without copying whole element. } };
the way totally avoid copying use pointer or reference. example:
class world { public: element& a; element& b; inline world(element& newa, element& newb) : a(newa), b(newb) { } ... };
as other reference or pointer, approach requires variables passed not go out of scope.
Comments
Post a Comment