link
link

Reputation: 23

how to initialise a static member variable container with elements that are instances of structs?

Hello so I have a class that uses 5 instances of a struct and I would like a static vector to hold those instances but I do not want the elements themselves to be static.

the header file looks like this

#pragma once
#include <string>
#include<vector>

using namespace std;


struct table {
    unsigned int tableNumber;
    unsigned int numChairs;
    unsigned int numberOfPeople;
    bool occupied;

    //table();

    void toggleOccupied() {
        if (occupied == true) {
            occupied = false;
        }
        else
        {
            occupied = true;
        }
    }

    bool getTableOccupied() {
        return occupied;
    }

    void setNumChairs(int newChairNum) {
        numChairs = newChairNum;
    }

    void setTableNumber(int assignTableNum) {
        tableNumber = assignTableNum;
    }

     int getTablenumber() {
        return tableNumber;
    }

     int getNumberOfChairs() {
        return numChairs;
    }

    void setNumberOfPeople(int addNumPeople) {
        numberOfPeople = addNumPeople;
    }


};





class tableOrder {
public:

    tableOrder();
    
    int getSP() {
        return SP;
    }

    enum ServingProgress {
        seated,
        drinksOrder,
        starters,
        main,
        dessert,
        bill
    };

    ServingProgress SP = seated;

    std::string progress;

    void getCurrentProgress() {
        switch (SP) {
        case 0:
            progress = "seated";
            break;
        case 1:
            progress = "drinksOrder";
            break;
        case 2:
            progress = "starters";
            break;
        case 3:
            progress = "main";
            break;
        case 4:
            progress = "dessert";
            break;
        case 5:
            progress = "bill";
            break;
        }
    }

    void ProgressOrder() {
        switch (SP) {
        case 0:
            SP = drinksOrder;
            break;
        case 1:
            SP = starters;
            break;
        case 2:
            SP = main;
            break;
        case 3:
            SP = dessert;
            break;
        case 4:
            SP = dessert;
            break;
        case 5:
            SP = bill;
            progress = "finished";
            break;
        }
    }

    void checkForTablesInUse() {
        for (int i(0); i < allTables.size(); i++) {
            if (allTables[i].occupied) {
                TablesInUse.push_back(allTables[i]);
            }
        }
    }

    void checkForTablesNotInUse() {
        for (int i(0); i < TablesInUse.size(); i++) {
            if (TablesInUse[i].occupied == false) {
                TablesInUse.erase(TablesInUse.begin() + i);
            }
        }
    }

    void updateTablesInUse() {
        checkForTablesInUse();
        checkForTablesNotInUse();
    }

    table& getTable(unsigned int tableIndex) {
        return allTables[tableIndex - 1];
    }

    //instantiate tables
    table table_1 = { 1,6,0,false };
    table table_2 = { 2,5,0,false };
    table table_3 = { 3,4,0,false };
    table table_4 = { 4,8,0,false };
    table table_5 = { 5,4,0,false };


//protected:

    unsigned int assignedTableNumber;

    table assignedTable;

    static vector<table> availableTables;
    static vector<table> TablesInUse;
    static vector<table> TablesToSelect;
    static vector<table>  allTables;
    static tableOrder passTable;

};
    

I have made 5 instance of table and want to store them in the static allTables. Here is the cpp file

#include "TableOrder.h"

vector<table> tableOrder::availableTables = {};
vector<table> tableOrder::TablesInUse = {};
vector<table> tableOrder::TablesToSelect = {};
vector<table>tableOrder::allTables = {table_1,table_2,table_3,table_4,table_5 };

tableOrder::tableOrder() {
     assignedTableNumber = 0;

     assignedTable = table_1;
    
     
     
}

I understand why I can't initialise allTables like this as I am putting unique variables inside a shared variable. Is there any decent way around this? thanks for the help!

Upvotes: 0

Views: 180

Answers (2)

user10
user10

Reputation: 296

Your table is simple Model and tableOrder manages list of tables. In such case vector should be private and non static otherwise anyone can push and pop in your static vector breaking all logic of your table order. Construct your tableOrder from vector<tables> and make all vectors private.

Upvotes: 0

atl
atl

Reputation: 773

Trying to help, hope this is helpful to you.

here is a discussion of static vector initialization and interesting reference to emphasize c++11 initializer lists ability to work on all classes:

C++11 extends initializer-lists, so they can be used for all classes including standard containers like std::vector

Problem statement:

a class . . . that uses 5 instances of a struct and . . . a static vector to hold them . . . do not want the elements themselves to be static

EDIT: using a class now and the allTables vector as a static member of that class.

Expected output is this:

./table
1
2
3
4
5

The compilation command using c++11 is this:

g++ -std=c++11 table.cpp -o table

The header file is table.h and simplified to this:

#include <vector>

using namespace std;

struct table {
  unsigned int tableNumber;
};

class Foo {
  public:
    static const vector<struct table *> allTables;
};

The program file table.cpp is this:

#include<vector>
#include<iostream>
#include"table.h"

using namespace std;

struct table table1 = { 1 };
struct table table2 = { 2 };
struct table table3 = { 3 };
struct table table4 = { 4 };
struct table table5 = { 5 };

const vector<struct table *> Foo::allTables = { &table1, &table2, &table3, &table4, &table5 };

int main( )
{
  for ( auto &tbl : Foo::allTables )
  {
    cout << tbl->tableNumber << "\n";
  }
  return 0;
}

Upvotes: 1

Related Questions