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!