Reputation: 303
I am wondering if there is a way I can, in my class' constructors, initialize an array in the base initialization list.
Right now, I am initializing myChars (a char*) to an empty character array of size DEFAULT_SIZE:
A::A() :
mySize(0), maxSize(DEFAULT_SIZE)
{
myChars = new char[DEFAULT_SIZE];
}
Is there any way to do this in the base initialization list? So that it would be simply:
A::A() :
mySize(0), maxSize(DEFAULT_SIZE), **myChars initialized here**
{
}
Thanks!
Upvotes: 1
Views: 4734
Reputation: 66912
Did you try?
A::A() :
mySize(0), maxSize(DEFAULT_SIZE), myChars(new char[DEFAULT_SIZE])
{}
However, it's extremely recommended to use std::string
, std::vector<char>
, or std::unique_ptr<char[]>
instead, all of which manage memory without error.
Upvotes: 3