-2

I need to create an array of classes inside an other class.

2
  • 3
    Retagged 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?" Commented Feb 14, 2011 at 13:08
  • Do you need an array of homogeneous or heterogeneous classes? Commented Feb 14, 2011 at 13:26

2 Answers 2

4

Here you go:

class Class {};

class OtherClass {
    Class array[1];
};
Sign up to request clarification or add additional context in comments.

2 Comments

that's a very clean and elegant solution.
That is an array of objects, not an array of classes (Not that I have any idea what is an array of classes...)
3

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

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
@cppand okay I make an edit. to be more explicit. Nice remark.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.