1

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!

1
  • Please show the complete class declaration. Commented Feb 17, 2016 at 2:14

1 Answer 1

3

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.

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

1 Comment

Ok, I don't know why I thought it would be any different. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.