开发者

C++::How to put different length arrays of const strings into common class

Sorry, new to C++, converting from C, and have struggled to find a good way to do this...

//Fragment follows

const char *List1[]={"Choice1", "Not a good choice", "choice3"}; //rom-able
const char *List2[]={"Hello", "Experts", "Can", "You", "Help?"};

class ListIF{
private:
 int index;
 char *list;
public:
  void GetValString(char *tgt,int len);//get parameter value as string max len chars
  void SetIndex(int n){index = n;};
  int  GetIndex(void){return index;};
};

//end Fragment

The problem is how to write the constructor so that I can开发者_运维技巧 "encapsulate" the lists inside the class, without getting heap bloat (embedded target). And then how to write the gettor so that we can see list[index] within the class.

I am going daft trying to do something that seems obvious, so I am missing something?


In C++, prefer using std::string over const char*. It will solve most of your problems you face with const char*.

For an array of strings, use std::vector<std::string>. It will solve most of your problems you face with const char *[].

You can even initialize the std::vector with multiple strings as,

std::vector<std::string> List1(adder<std::string>("Choice1")("Not a good choice")("choice3"));
std::vector<std::string> List2(adder<std::string>("Hello")("Experts")("Can")("You")("Help?"));

Where adder<> is a class template defined as:

template<typename T>
struct adder
{
   std::vector<T> items;
   adder(const T &item) { items.push_back(item); }
   adder& operator()(const T & item) { items.push_back(item); return *this; }
   operator std::vector<T>&() { return items ; }
};

Sample running code here : http://www.ideone.com/GLEZr


/** Wrapper for C style arrays; does not take ownership of the array */
template <typename T>
class static_array
{
    T *array;
    size_t nelems;

  public:
    template <size_t N>
    static_array(T (&a)[N]) : array(a), nelems(N) {}

    T &operator[](size_t i) { return array[i]; }
    T const &operator[](size_t i) const { return array[i]; }
    size_t size() const { return nelems; }
};

typedef static_array<char const *> static_cstr_array;

Construct as static_cstr_array array1(List1). The setter is operator[], i.e.

array1[1] = "foo!";

You can add any method that you want to this class.

(I chose the name static_array because, as far as the class is concerned, the underlying array must be static: it should not grow, shrink or move due to realloc or otherwise. It doesn't mean the array must have static linkage.)


Not sure what you want your functions to be but one way to wrap the arrays would be:

EDIT : changed to incorporate Larsmans suggestion (on the chance that your compiler can't handle his answer).

class ListIF
{
private:
    std::vector<const char*> m_list;//stores ptrs to the ROM
public:

    ListIF(char const **list, size_t n) : m_list(list, list+n) {}

    const char* get( int pos )
    {
        return m_list[pos];
    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜