Solved

Array/vector class member of variable size

Posted on 2007-11-14
7
663 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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.

773 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