Solved

Array/vector class member of variable size

Posted on 2007-11-14
7
651 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
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:Kdo
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

896 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now