0

Say I have an object of class baseclass:

// baseclass.h
class baseclass
{
    baseclass() # default constructor, constructs baseclass object
}

And in the .cpp for baseclass:

// baseclass.cpp
baseclass::baseclass()
{
    // member functions and variables
}

Now my goal is to have a derived class, and in the default constructor for the derived class, create an array of static size n baseclass objects. To try and clarify, an alternative way to think about this is to think about baseclass as playing cards, and I want to create an array (a deck) of those cards by calling the default constructor on the derived class. I decided to keep the scope of my question abstract however, so I will continue to use base/derived so others can more easily see how this could apply to them.

I am not sure the best way to set this up in an object oriented manner, so far I have something like this, but I am getting a segmentation fault. Here is how I have it set up:

// derivedclass.h (extending baseclass)
class derivedclass
{
    // default constructor for derivedclass object
    derivedclass();

    // a pointer to an object of type baseclass
    baseclass* bar;
    // or should it be:
    baseclass* bar[n] // where n is an integer
    // or is there a better way to do it?
}

Lastly, since I said that a derivedclass object can have an array, I must make that true for the default constructor in the .cpp for derivedclass:

// derivedclass.cpp
derivedclass::derivedclass()
{
    // so I need to initialize the member array
    baseclass* bar = new baseclass[n] // where n is the size I want to initialize 
                                      // the array to
}

So would any of the situations I listed cause segmentation fault? What is the best way to create this array of object? Sorry if this is a nooby question, I am a student still learning a lot about memory allocation and pointers, normally deal with languages where I don't have to worry about this. Also, I tried to keep the question abstract for the benefit of others. Thanks in advance!

Community
  • 1
  • 1
Joker
  • 2,119
  • 4
  • 27
  • 38
  • If you know the size it's going to be at compilation time, use `std::array`. If not, use something like `std::vector`. You're also **shadowing** your data member in the constructor. – chris Mar 28 '13 at 19:35

3 Answers3

3

I am not sure why you need to use dynamic allocation at all here. I would rather do something like this, which would also save you some work in derivedclass's constructor:

struct baseclass
{
    // Stuff...
};

struct derivedclass : baseclass
{
    // Stuff...

    baseclass objects[N];
};

In C++11 you should use std::array<> instead of a plain C-style array (std::array<> is a safe, zero-overhead wrapper of a C-style array):

// ...

#include <array>

struct derivedclass : baseclass
{
    // Stuff...

    std::array<baseclass, 10> objects;
};
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
1
// so I need to initialize the member array
baseclass *bar = new baseclass[n];

Except that with this, you don't initialize the member array, only a local variable that has the same name as the member variable, thus it shadows it (and for the very same reason, you're also leaking memory by loosing the pointer to the newly allocated array).

1

Why to use new at all? Why to derive deck from cards? Deck contains cards.

 class Card
 {
     // ... whatever card does
 };

 class Deck
 {
 public:
     static int const CountOfCards = 36;
     typedef std::array<Card,CountOfCards> Cards;
     Cards cards;
     // etc. ... whatever deck does
 };
Öö Tiib
  • 10,809
  • 25
  • 44