1

If I for example have a classed called CardHandler and wanna create a static allocated array of pointers that are of a type called Card and have 40 places, how do I write in the h-file and cpp-file?

I have tried something like this:

class CardHandler
{
   private:
   Card **arr;
}

CardHandler::CardHandler()
{
    this->arr = new Card*[40];
}

But don't think this is the right way? I suspect it's something to do with static.

7
  • An array of pointers should be declared with Card **arr;. Commented Mar 18, 2020 at 11:26
  • 1
    Why don't you use std::vector? Commented Mar 18, 2020 at 11:28
  • @BlueTune not allowed to in this assigment. We are learning about Arrays Commented Mar 18, 2020 at 11:29
  • 2
    Can you explain exactly what you mean when you say "static"? What does "static" mean to you in this context? Commented Mar 18, 2020 at 11:32
  • 1
    if you need statically allocated array of pointers use Card* arr[40]. Note that your are missing the constructor declaration in class CardHandler. Commented Mar 18, 2020 at 11:36

1 Answer 1

3

Here's how you can create an array of pointers which has a fixed size:

class CardHandler
{
private:
    Card* arr[40];
}

You'll need to populate the pointers with actual Cards allocated on the heap in the constructor. Or perhaps better:

class CardHandler
{
private:
    Card arr[40];
}

That's an array of 40 actual Cards, which you can initialize however you want, but will always be allocated from the moment your constructor is called until after your destructor is called.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.