I need to create an array of classes inside an other class.
-
3Retagged as C++. You can't create classes in C. Also, you need to ask a question of some sort. The only answer I can give to this is "That's nice; so what?"Ant– Ant2011-02-14 13:08:01 +00:00Commented Feb 14, 2011 at 13:08
-
Do you need an array of homogeneous or heterogeneous classes?gr0v3r– gr0v3r2011-02-14 13:26:23 +00:00Commented Feb 14, 2011 at 13:26
Add a comment
|
2 Answers
Here you go:
class Class {};
class OtherClass {
Class array[1];
};
2 Comments
filipe
that's a very clean and elegant solution.
Lior Kogan
That is an array of objects, not an array of classes (Not that I have any idea what is an array of classes...)
you should look at using std::vector, instead of carring about C-style arrays.
This is more the C++ way of coding, imagine a class containing an array of double:
class A
{
public:
std::vector<double> m_doubles;
}
EDIT: so for an array of class, let's say class B:
class A
{
public:
std::vector<B> m_bs;
}
EDIT2:
and like @cppanda suggests in the comments below, the implementation of std::vector is done in the Standard C++ Library (also known as the STL). It's really worth the effort learning to use it intensively, a lot of things already done for you. ( like vector and many sorts of containers)
2 Comments
cppanda
he wants an array of classes, double is not a classe. still the example is good, just replace <double> with <B> or something. @Manuel Marín Calero: best would be if you take a look at stl containers cplusplus.com/reference/stl
Stephane Rolland
@cppand okay I make an edit. to be more explicit. Nice remark.