Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Array/vector class member of variable size

Posted on 2007-11-14
7
Medium Priority
?
692 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 46

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 400 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 100 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

636 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