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

  • SEARCH
  • Home
  • 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 6821407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:32:30+00:00 2026-05-26T21:32:30+00:00

Consider the following class: class A { const int arr[2]; public: A() { }

  • 0

Consider the following class:

class A {
    const int arr[2];
public:
      A() { }
};

Is it possible to initialize arr from the constructor initializer list or in any other way than on the line where it is declared (i.e. const int arr[2] = {1,2};)?

Note that I’m interested in methods that work with C++98!

  • 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-26T21:32:30+00:00Added an answer on May 26, 2026 at 9:32 pm

    By wrapping them in a struct, e.g.:

    class A
    {
        struct Data
        {
            int arr[2];
        };
    
        Data const arr;
    public:
        A() : arr( someOtherStruct ) {}
    };
    

    This does mean that to access the data, you’d have to write arr.arr.
    It’s possible to avoid that by inheriting from the struct:

    struct PrivateDataForA
    {
        int arr[2];
    };
    
    class A : private PrivateDataForA
    {
    public:
        A() : PrivateDataForA( someOtherStruct ) {}
    };
    

    This does make the name of the struct visible outside of the class
    (which might be an advantage—client code could pass you one as an
    argument).

    If you don’t have an instance of the struct handy, say because you want
    to fill it with values calculated from arguments to the constructor, you
    can use a static member function:

    class A : private PrivateDataForA
    {
        static PrivateDataForA createInitializer( int a, int b );
    public:
        A( int a, int b ) : PrivateDataForA( createInitializer( a, b ) )
        {
        }
    };
    

    For the OP’s concrete example:

    #include <iostream>
    #include <stddef.h>
    
    typedef ptrdiff_t   Size;
    typedef Size        Index;
    
    template< class Element, Size n >
    struct Array{ Element elem[n]; };
    
    class A {
        Array<int, 2> const arr_;       // const int arr[2];
    
        A& operator=( A const& );       // No such.
    
        static Array<int, 2> const& oneAndTwo()
        {
            static Array<int, 2> const a = {1, 2};
            return a;
        }
    
    public:
        A(): arr_( oneAndTwo() ) {}
        int at( Index i ) const { return arr_.elem[i]; }
    };
    
    
    int main()
    {
        using namespace std;
    
        A o;
        for( int i = 0;  i < 2;  ++i )
        {
            cout << o.at( i ) << endl;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following : class A { public: int xx; A(const A& other) {
Consider the following: class A { public: const int c; // must not be
Consider the following code: class A { public: A& operator=( const A& ); const
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following class: class Something : ISomething { public void DoesSomething(int x) {
Consider the following class public class Class1 { public int A { get; set;
Consider the following code: class A { public: virtual void f() throw ( int
Consider the following simple map: class MyCoolMap : public unordered_map<const char *, const char
Consider the following code : #include <vector> #include <iostream> class a { public: int
consider the following: class X { public: X(int i) { cout << X(int i)

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.