Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. c++ passing an array of objects into a Class constructor by reference
Forum Updated to NodeBB v4.3 + New Features

c++ passing an array of objects into a Class constructor by reference

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lineaxe
    wrote on 5 Oct 2015, 11:50 last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 5 Oct 2015, 12:28 last edited by
      #2

      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. :)

      1 Reply Last reply
      1
      • T Offline
        T Offline
        TheBadger
        wrote on 5 Oct 2015, 13:15 last edited by
        #3

        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.


        Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

        1 Reply Last reply
        1
        • L Offline
          L Offline
          Lineaxe
          wrote on 7 Oct 2015, 06:23 last edited by
          #4

          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.

          M 1 Reply Last reply 7 Oct 2015, 06:35
          0
          • L Lineaxe
            7 Oct 2015, 06:23

            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.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 7 Oct 2015, 06:35 last edited by
            #5

            @Lineaxe

            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,

            1 Reply Last reply
            0

            2/5

            5 Oct 2015, 12:28

            • Login

            • Login or register to search.
            2 out of 5
            • First post
              2/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved