Solved

Array/vector class member of variable size

Posted on 2007-11-14
7
639 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

23 Experts available now in Live!

Get 1:1 Help Now