Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6119137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:31:01+00:00 2026-05-23T15:31:01+00:00

I have a basic C++ class .The header looks like this: #pragma once class

  • 0

I have a basic C++ class .The header looks like this:

#pragma once
class DataContainer
{
public:
    DataContainer(void);
   ~DataContainer(void);
    int* getAgeGroup(void);
    int _ageGroupArray[5];
private:

     int _ageIndex;

};

Now inside the cpp file of the class I want to intialize the _ageGroupArray[5] with default values inside the class contructor like this:

#include "DataContainer.h"


DataContainer::DataContainer(void)
{

_ageGroupArray={20,32,56,43,72};

_ageIndex=10;
}

int* DataContainer::getAgeGroup(void){
return _ageGroupArray;
}
DataContainer::~DataContainer(void)
{
}

Doing it I am getting “Expression must be a modifiable lvalue” on _ageGroupArray line.So is it entirely impossible to initialize an array object in the constructor? The only solution I found was to define the array outside scope identifiers .Any clarification on this will be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:31:02+00:00Added an answer on May 23, 2026 at 3:31 pm

    In the current standard, as you have already noticed, you cannot initialize a member array in the constructor with the initializer list syntax. There are some workarounds, but none of them is really pretty:

    // define as a (private) static const in the class
    const int DataContainer::_age_array_size = 5;
    
    DataContainer::DataContainer() : _ageIndex(10) {
       int tmp[_age_array_size] = {20,32,56,43,72};
       std::copy( tmp, tmp+_age_array_size, _ageGroupArray ); 
    }
    

    If the values in the array are always the same (for all object in the class) then you can create a single static copy of it:

    class DataContainer {
       static const int _ageGroupArraySize = 5;
       static const int _ageGroupArray[ _ageGroupArraySize ];
    // ...
    };
    // Inside the cpp file:
    const int DataContainer::_ageGroupArray[_ageGroupArraySize] = {20,32,56,43,72};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so i have a class which contains basic stuff like Class Test{ public function
i have a basic ruby class: class LogEntry end and what i would like
I have been implementing class methods as shown in this answer Basic Sproutcore: class
I have a basic quiz/survey type application I'm working on, and I'd like to
I have a header file with some inline template methods. I added a class
i have some header tabs on a basic index.aspx page that uses a masterpage.
I have a class file (header and cpp) that I made, that I want
I have a template class in a header that fails to compile, but when
Currently, i have basic C++ and PHP skills. But, i want to switch to
I have a basic form with controls that are databound to an object implementing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.