pokche
pokche

Reputation: 1149

How to return vector of enum outside the class?

Lets say A class has 2d vector of enum and I want to access this 2d vector outside of the class and manipulate the value.

My Question is : How can I declare the new vector to hold the return by value outside of the class, since my type ( enum type) is inside the class? I was hoping to some thing like

A a(5);
std::vector<std::vector<A::s> > x = a.get_2dvec();

But this give me error saying its private and then if I make type public I get not declared error.

I know I could place enum s {RED, BLUE, GREEN}; and typedef s color; outside of class and achieve the result but lets say the main is on different file.

 // f1.cpp

#include <iostream>
#include <vector>

class A{
    // This enum is inside class 
    enum s {RED, BLUE, GREEN};
    typedef s color;
    const int size = 3;
    std::vector<std::vector<color> > color_array;
public:
    A(int size_):size(size_),color_array(size){
        std::cout << "vector size  = " << color_array.size() << std::endl;
        for(int i = 0; i < size; i++){
            for(int j = 0; j < size; j++){
                color_array[i].push_back(RED);
            }
        }  
    }
    void print(){
        for(auto it = color_array.begin(); it != color_array.end(); it++){
            for(auto itt = it->begin(); itt != it->end(); itt++){
                std::cout << *itt << " : " << std::endl;
            }
        }
    }

    // pass vector by value
    std::vector<std::vector<color> > get_2dvec(){
        return color_array;
    }
};

// main.cpp
int main(){
    A a(4);
    a.print();
    // here I some how need to capture the 2d enum vector
    std::vector<std::vector<enum-type> > x = get_2dvec();



return 0;
}

Upvotes: 4

Views: 1398

Answers (2)

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140188

Your enum is in the private part of the class.

By default, a class starts by default in private, whereas a struct starts by default in public.

Move it to the public part

In addition, return the value by reference, in a constant getter method or performance and interface quality in general will suffer.

I would typedef the matrix itself too, and use it at once in the class, thus leading me to put the private part in the end.

Edit: since answering questions also means learning stuff from others, I have fully refactored the example with const references, auto, all types private, all the works, just for the record (and it builds).

#include <vector>
#include <iostream>

class A
{
private:
    // This enum is inside class 
    const int size = 3;
    enum s {RED, BLUE, GREEN};
    typedef s color;
    typedef std::vector<std::vector<color> > ColorMatrix;
    ColorMatrix color_array;
public:
    A(int size_):size(size_),color_array(size){
        std::cout << "vector size  = " << color_array.size() << std::endl;
        for(auto &it : color_array){
            it.resize(size,RED);
           }

    }
    void print() const{
        for(const auto &it : color_array){
            std::cout << " :";
            for(const auto &itt : it){
                std::cout << " " << itt;
            }
            std::cout << std::endl;
        }
    }

    // pass vector by const reference to avoid copies
   // (for better performance)
    const ColorMatrix &get_2dvec() const {
        return color_array;
    }

};

// main.cpp
int main(){
    A a(4);
    a.print();
    // here I some how need to capture the 2d enum vector
    const auto &x = a.get_2dvec();



return 0;
}

Upvotes: 2

songyuanyao
songyuanyao

Reputation: 172924

get_2dvec() is a member function, which needs an object to call on it. And if you don't want to make A::s public, you could use auto specifier (since C++11) to avoid accessing private name directly. (But I'm not sure whether this is what you want.)

Change

std::vector<std::vector<enum-type> > x = get_2dvec();

to

auto x = a.get_2dvec();

Upvotes: 5

Related Questions