Thread: Class -> Array Setter, how to?

  1. #1
    Registered User
    Join Date
    Sep 2022
    Posts
    8

    Class -> Array Setter, how to?

    Hello,

    i have a Class with two arrays (for LC-Display control)
    I want to set two arrays from outsite via a setter call.

    Here it my code:

    Code:
    class userMenu
    {
    
    private:
     
       String menuIdentifierArray[3][3] = 
    
    {{"LED", "Val1", "Unit"},
    {"Helligkeit", "Val2", "Unit"},
    {"Servo", "Val4", "Unit"}};
    
       int16_t menuParameterArray[3][3] = {
             
            {1, 0, 10},
            {2, 0, 10},
            {3, 0, 10},};
    
    ...
    The first array holds the menuDisplay Strings, the second the parameter values and limits.

    Is there any chance to load this array from outsite into the privat area? The length of the array is variable, but is has always 3 items per element

    Thx a lot

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You can add row-by-row like this:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <array>
    using namespace std;
     
    class A {
        vector<array<string, 3>> v;
    public:
         A() { }
         void add_row(const string& a, const string& b, const string& c) {
             v.push_back({a, b, c});
         }
         void print() const {
             for (const auto& row: v)
                 cout << row[0] << ' '
                      << row[1] << ' '
                      << row[2] << '\n';
         }
    };
     
    int main() {
        A a;
        a.add_row("a", "b", "c");
        a.print();
    }
    You might want to structure your data, something like:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <array>
    using namespace std;
     
    struct Value {
        string  name;
        int16_t value;
    };
     
    class A {
        vector<array<Value, 3>> v;
    public:
         A() { }
         void add_row(const string& a, int16_t av,
                      const string& b, int16_t bv,
                      const string& c, int16_t cv) {
             v.push_back({Value{a,av}, Value{b,bv}, Value{c,cv}});
         }
         void print() const {
             for (const auto& r: v) {
                 for (const auto& e: r)
                     cout << e.name << ':' << e.value << "  ";
                 cout << '\n';
             }
         }
    };
     
    int main() {
        A a;
        a.add_row("a", 1, "b", 2, "c", 3);
        a.add_row("d", 4, "e", 5, "f", 6);
        a.print();
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Sep 2022
    Posts
    8
    Hi John,

    ok, I thought it would be quite complex
    Thanks a lot, I will try it.

    Michael

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing parameters to setter functions
    By andirew in forum C++ Programming
    Replies: 17
    Last Post: 11-28-2015, 03:15 PM
  2. accessing setter methods
    By Alexius Lim in forum C++ Programming
    Replies: 7
    Last Post: 11-21-2013, 06:03 PM
  3. Replies: 3
    Last Post: 07-15-2010, 07:53 PM
  4. Class getter and setter functions help
    By MasterM in forum C++ Programming
    Replies: 12
    Last Post: 06-16-2009, 08:37 AM
  5. help with calling this setter
    By mufugger in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 12:46 AM

Tags for this Thread