Search code examples
c++arraysmultidimensional-arraygetter-setter

Define setter/getter attributes for a 3D array


In c++, I have a class that contains a 3d array of defined type. I need to define several attributes that have a value of int and be able to set and get the attributes for 3d array.

    //header;
    class Voxel
    {
      private:
        vector< vector < vector < myPoints* > > > Vox; // 3D array
      public:
        //... 
     };

     //Here is the constructor and methods of the class:
     Voxel::Voxel(myType var1; double var2)
     {
     // this is constructor

     } 

For example I need to define "Attributes" like this which have int values:

Attributes:
LabelTag; // Vox tag e.g. an int tag=2
NumberofPoints; // Vox nr of containing points e.g. int nr=5


Vox.setAttribute(LabelTag tag, Value 2);
Vox.setAttribute(NumberofPoints nr, Value 5);

int tag        = Vox.getAttribute(LabelTag);
int nrofpoints = Vox.getAttribute(NumberofPoints);

How should I define the attributes, as struct or as a typedef or something else, and then how may I set the values to my 3D array members e.g. Vox, I want to set attributes to the Vox itself, not the points inside? Is it possible? And should them defined as private or public?


Solution

  • Edited answer below the second block of code.

    Ok, firstly as commented by @StoryTeller, your vector is storing pointers; so you will need to use the -> syntax to access the object they are pointing to.

    So you should probably setup a myPoints class as structs are not commonly seen (structs in c++ are the same thing as a class except the default access modifier to their properties and functions is public). I'd imagine this class to look something like

    class myPoints // you should probably split this into a header and cpp
    {
        int tag;
        int noOfPoints;
    
        myPoints() : tag(0), noOfPoints(0) // construct with whatever values, you can pass your own
        {}
    
        void setNoOfPoints(noOfPoints)
        {
            this->noOfPoints = noOfPoints;
        }
    
        void setTag(tag)
        {
            this->tag = tag;
        }
    
        int getNoOfPoints(){ return noOfPoints; }
        int getTag(){ return tag; }
    };
    

    Assuming you've initialized vox with some *myPoints literals you can then simply access and use your myPoints objects as follows

    int tag = Vox.at(i).at(j).at(k)->getTag();
    int noOfPoints = Vox.at(i).at(j).at(k)->getNoOfPoints();
    
    Vox.at(i).at(j).at(k)->setNoOfPoints(6);
    Vox.at(i).at(j).at(k)->setTag(6);
    

    Leaving the above answer, as with @Aconcagua's answer, you might find it useful in the future.

    Anyway, I think I understand a little better what you are trying to do, given the code you have already written as @StoryTeller has said, you can just use your Voxel class to hold the tag and noOfPoints properties for each vector. The Voxel class will look something like (forgive my laziness in not providing a header)

    class Voxel
    {
    private:
        vector< vector < vector < myPoints* > > > Vox; 
    
        int tag;
        int noOfPoints;
    public:
        Voxel() : tag(0), noOfPoints(0) // construct with whatever values, you can pass your own
        {}
    
        vector< vector < vector < myPoints* > > >& getVox(){ return Vox; } //Ignore my shitty naming scheme, you can use this to set or get elements
    
        void setNoOfPoints(noOfPoints)
        {
            this->noOfPoints = noOfPoints;
        }
    
        void setTag(tag)
        {
            this->tag = tag;
        }
    
        int getNoOfPoints(){ return noOfPoints; }
        int getTag(){ return tag; } 
    };
    

    and then to access your vector and set your tag and noOfPoints, simply create an instance of Voxel, it should look something like this

    //A new vector of voxels
    
    vector<Voxel> voxels;
    
    voxels.push_back(Voxel); // this only needs to be a 1d array, you can construct each voxel however you like and add as many as you like
    
    //settting the tag for the first element
    
    voxels[0].setTag(0);
    
    //getting the tag for the first element 
    
    int tag = voxels[0].getTag();
    
    // manipulating the contained vector 
    
    voxels[0].getVox().at(i).at(j).at(k) = //whatever you are storing in your vector