Solved

Array/vector class member of variable size

Posted on 2007-11-14
7
674 Views
Last Modified: 2008-02-01
How is it possible to have a class with an array (or vector) as a class member, whose size is unknown in advance? Say one of the class members must be an array of times t0, t1, .... tN. However we don't know N until we create an instance of the class.
0
Comment
Question by:Rothbard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 20

Expert Comment

by:ikework
ID: 20280718
you can either use stl::vector or pass the array size to the class and create the c-array then, here is the latter:

class Times
{
public:

};
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 20280720
Hi Rothbard,

Absolutley.

Consider the TList object.  It is a list of arbitrary length of pointers to objects that the programmer passes to the class.  The objects are user defined with the class having no knowledge of the underlying data structure or size.

You can certainly do the same thing with your own classes.



Good Luck,
Kent
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20280732
you can use a STL vector<> class, you just create it empty, then you can add items or separate some room for elements furtherly.
Do you know about it?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:ikework
ID: 20280749
sorry .. my browser was too fast .. ;)

class Times
{
public:
    Times( int size )
    : m_size( size )
    {
        m_array = new float[m_size];
    }
    ~Times()
    {
        delete[] m_array;
    }

    float& GetTimeAt( int idx ) { return m_array[idx]; }

private:
    float *m_array;
    int      m_size;
};

0
 
LVL 20

Accepted Solution

by:
ikework earned 100 total points
ID: 20280779
the same with a stl-vector:

#include <vector>

class Times
{
public:
    Times( int size )  : m_vector( size ) {}
    ~Times() {}

    float& GetTimeAt( int idx ) { return m_vector[idx]; }

private:
    std::vector<float> m_vector;
};
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 25 total points
ID: 20281037
For variable size use std::vector as suggested above.

If you need a fixed size buffer for variable types and sizes, consider...

#include <string>

template <size_t SIZE, typename T>
class FixedArray
{
public:
    typedef T data_t[SIZE];

    // Example accessor
    data_t const & GetData() const { return m_data; }

    // Your accessors go here

private:
    data_t m_data;
};

// Friendly typedef
typedef FixedArray<10, char> FixStringArray10;

int _tmain(int argc, _TCHAR* argv[])
{
      FixStringArray10 fsa10;
      FixStringArray10::data_t const & data = fsa10.GetData();

}

I'll leave the implementation detail to you :)

-Rx.
0
 

Author Comment

by:Rothbard
ID: 20287872
Thanks a lot for all your replies, guys. I gave the points to the answers I found most useful for what I need to do.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 79
Should CArray be used for a list of pointers in C++? 19 128
max float value 3 60
C++ Code Issue 4 38
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question