c++ passing an array of objects into a Class constructor by reference
-
Hi I am programming in C++ and am sending an array of objects into a Class constructor by reference. ( Object &objname[3] )
I want to access the objects in the class member functions so I created a member variable, a pointer to an object array (of the same type of objects) in the class header file. So far , I can't use objects in the array that I passed into the constructor by reference. I am am not sure what syntax to use that would make all the objects available to all the other member functions of the class. What I am trying now is to pass the reference to the object array over to the pointer of the object array ,and then use that throughout the other class members.. There must be some easier syntax to do this, but I have forgotten a lot of my C++ . Can you help out ? -
Hi
Not sure why you want references, ?
but that would mean a copy of the external list.
If you mean to pass the list to a object that will have a ref to the list then something like this:#include <vector> class Thing { public: Thing( int aid) : id(aid) {} int id; }; class UseThing { std::vector<Thing>& PointToOutside; public: UseThing(std::vector<Thing>& Outside) : PointToOutside(Outside) { qDebug() << "inside:" << PointToOutside[0].id; PointToOutside[0].id = 200; } }; and using it std::vector<Thing> Outside = {{Thing(100)}}; UseThing t(Outside); qDebug() << "out:" << Outside[0].id;
And ofcause Outside should not run out of scope. :)
-
I do agree with what @mrjj said, but just to re-iterate:
Do not use the C type arrays, especially when passing it as an argument. If you do you also need to send the size to the constructor otherwise things can get messy.
UseThing(Object &objname[], int size)
Rather use one of the Qt containers, look at QVector, QList, etc. In most cases a container is a much better solution in C++, you can chose one from Qt or from the STL (std::vector, std::list, etc.)
Containers are much easier to work with and provide some support for checking out of bounds etc.
-
I have used lists in the past , they are very useful. I am not sure if they are as quick as searching through arrays. I suppose however I could create List of QImages and iterate through the list. The use of a reference does not copy a list over , it is a constant pointer to an object. This allows the object to be directly modified inside the class ( in this case a Thread ) . Now , using a List container is a lot cleaner and I can pass a reference to it much easier I think.
-
I have used lists in the past , they are very useful. I am not sure if they are as quick as searching through arrays. I suppose however I could create List of QImages and iterate through the list. The use of a reference does not copy a list over , it is a constant pointer to an object. This allows the object to be directly modified inside the class ( in this case a Thread ) . Now , using a List container is a lot cleaner and I can pass a reference to it much easier I think.
Hi std:vector is almost as fast as list[10] in terms of access.
unless you have millions of items. it wont matter.
also, you can use reserve
http://www.cplusplus.com/reference/vector/vector/reserve/
To avoid the expansion on the first push_backs.
(i guess you know, just in case)The reason I asked about use & is that vector did not used to work with & so normally one would use pointers,