C++ (Qt)template <class T1, class T2>void Copy( const T1 * src, T2 * dst, size_t num ){ for (size_t i = 0; i < num; ++i) dst[i] = src[i];}
C++ (Qt)template <class T1, class T2>void Scale( const T1 & src, T2 & dst ){ dst.x = src.x / 2;}
class Clonable{public: virtual Clonable* clone() const = 0;};void Copy(const Clonable* src, Clonable** dst, size_t num){ for (size_t i = 0; i < num; ++i) dst[i] = src[i]->clone();}
class Exception : public std::exception{public:... Exception* clone() const { Exception* result = createClone(); // check that a descendant overides createClone() assert(typeid(*result) == typeid(*this) && " (createClone() is not overrided)"); return result; }private:... virtual Exception* createClone() const { return new Exception(*this); // copy ctor }};class InvalidArgumentException : public Exception{...private: virtual InvalidArgumentException* createClone() const { return new InvalidArgumentException(*this); }
dst[i] = src[i]->clone();
C++ (Qt)dst[i].x = src[i].x;
class Foo : public Clonable{ int x() const { ... }...}std::vector<Foo*> dst(Count);copy(src, &dst[0], Count); // initialize arraydst[0]->x();class Foo1 : public Clonable{ int y() const { ... }...}std::vector<Foo1*> dst1(Count);copy(src1, &dst1[0], Count); // initialize arraydst[0]->y();
std::vector<Foo1*> dst1(Count);copy(src1, &dst1[0], Count); // initialize array
class Foo : public Clonable{ int x() const { ... }...}
struct Foo1{ double x; ...}struct Foo2{ int x; ...}template <typename T1, typename T2>void copyItem(const T1& src, T& dst){ dst.x = src.x; ...}
struct Base {}struct Foo1 : public Base{ double x; ...}struct Foo2 : public Base{ int x; ...}...Foo1 foo1;Foo2 foo2 = foo1;
...Foo1 foo1;Foo2 foo2 = foo1;
C++ (Qt)Base * test1 = new Foo1;Base * test2 = new Foo2; // Как теперь добраться до (например)test1->x = test2->x;
template <typename T1, typename T2>void copyItem(const T1& src, T& dst){ dst.x = src.x; ...}