class MyClass{private: float x, y, z;public: MyClass(); MyClass(float _x, float _y, float _z); float GetX(); void SetX(float _x); // ... // ... // ...};
QList<MyClass> listMyClass;QList<MyClass*> listMyClassP;void AddToListA(float x, float y, float z){ MyClass myClassInstance(x, y, z); listMyClass.push_back(myClassInstance);}void AddToListB(float x, float y, float z){ MyClass myClassInstance(x, y, z); listMyClassP.push_back(&myClassInstance);}int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); for (int i = 0; i < 10; i++) { AddToListA(i, i, i); AddToListB(i, i, i); } return a.exec();}
C++ (Qt)void AddToListB(float x, float y, float z){ MyClass myClassInstance(x, y, z); listMyClassP.push_back(&myClassInstance);}
C++ (Qt)int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); for (int i = 0; i < 10; i++) { listMyClass.append(MyClass(i, i, i)); listMyClassP.append(new MyClass(i, i, i)); } return a.exec();}
C++ (Qt) qDeleteAll(listMyClassP); listMyClassP.clear();
void AddToListB(float x, float y, float z){ MyClass myClassInstance(x, y, z); listMyClassP.push_back(&myClassInstance);}
C++ (Qt)listMyClass.push_back(myClassInstance(x, y, z));listMyClassP.push_back(&listMyClass.back());
C++ (Qt) class MyClass{private: float x, y, z; public: MyClass(): x(0.0), y(0.0), z(0.0) {}; MyClass(float _x, float _y, float _z): x(_x), y(_y), z(_z) {}; ~MyClass() {}; float GetX() {return x;}; void SetX(float _x) {x=_x;}; // ... // ... // ... }; QList<MyClass> listMyClass;QList<QSharedPointer<MyClass> > listMyClassP; void AddToListA(float x, float y, float z){ listMyClass.push_back(MyClass(x, y, z));} void AddToListB(float x, float y, float z){ listMyClassP.push_back(QSharedPointer<MyClass>(new MyClass(x, y, z)));} int main(int argc, char *argv[]){ for (int i = 0; i < 10; i++) { AddToListA(i, i, i); AddToListB(i, i, i); } return 0;} [/code=cpp]