Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Not sure about using vectors to store object of different Types
Forum Updated to NodeBB v4.3 + New Features

Not sure about using vectors to store object of different Types

Scheduled Pinned Locked Moved Unsolved C++ Gurus
5 Posts 5 Posters 1.1k Views 1 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.
  • J Offline
    J Offline
    jkwok678
    wrote on last edited by jkwok678
    #1

    Hi,

    What I have right now is a simple inheritance structure with classes water, juice and tea that all inherit from the drink class. For example in the main method i created a few waters, juices and teas, each with their specific fields that only they have. Then I stored them in a vector that holds drink, but when I iterate through the vector, I would lose the properties and methods that only apply to the sub classes, is there a way to get around this apart from making lots of arrays for each class and iterating through them, as I would want to add many different other types of drinks.

    Thanks

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      This a better topic for the C++ specific forum, rather than the Qt framework forum...but for the record, this is basic OO programming in C++ knowledge...I suggest reading a book on how C++ implements inheritance and polymorphism...your generic vector will work fine if it stores pointers to the generic base class, and you have defined the sublclasses properly.

      1 Reply Last reply
      3
      • R Offline
        R Offline
        Ray Gray
        wrote on last edited by
        #3
        std::vector<std::any> beverages;
        std::vector<std::variant<Gin, Whiskey, Vodka>> beverages;
        
        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by fcarney
          #4

          You create generic methods in the base class. These methods should be virtual. Then in each derived class you define the corresponding method and override the base class method. The trick is keeping the interface generic. So lets say you have a method called ingredients() which returns a list of ingredients. Then you have a base class method:

          class drink
          {
          ...
          virtual QStringList ingredients()=0;  // tells compiler it is pure virtual
          ...
          };
          
          class tequila : public drink
          {
          ...
          QStringList ingredients() override 
          {
            return QStringList();
          }
          ...
          };
          

          The syntax may not be exact, but you get the idea. So in your list you only deal with drink:
          **Edit:**Like @jsulm pointed out, it needs pointers. DOH!

          QVector<drink*> drinks;
          drinks.push_back(new tequila());
          ...
          for(auto d: drinks){
            qInfo() << d->ingredients();  //  calls function that is specific to actual object
          }
          

          C++ is a perfectly valid school of magic.

          jsulmJ 1 Reply Last reply
          0
          • fcarneyF fcarney

            You create generic methods in the base class. These methods should be virtual. Then in each derived class you define the corresponding method and override the base class method. The trick is keeping the interface generic. So lets say you have a method called ingredients() which returns a list of ingredients. Then you have a base class method:

            class drink
            {
            ...
            virtual QStringList ingredients()=0;  // tells compiler it is pure virtual
            ...
            };
            
            class tequila : public drink
            {
            ...
            QStringList ingredients() override 
            {
              return QStringList();
            }
            ...
            };
            

            The syntax may not be exact, but you get the idea. So in your list you only deal with drink:
            **Edit:**Like @jsulm pointed out, it needs pointers. DOH!

            QVector<drink*> drinks;
            drinks.push_back(new tequila());
            ...
            for(auto d: drinks){
              qInfo() << d->ingredients();  //  calls function that is specific to actual object
            }
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @fcarney For this to work you have to use pointers:

            QVector<drink*> drinks;
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3

            • Login

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