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 8144957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:30:47+00:00 2026-06-06T13:30:47+00:00

I have a class with a private member ( struct ) as follows: enum

  • 0

I have a class with a private member (struct) as follows:

enum indices {
   zero,
   one, 
   two,
   ...,
   N
};

class myclass {
  ...
  ...
private: 
  struct impl;
  impl* p_impl;
};

Then, in the implementation I have defined a static container (vector) in the struct as follows:

struct myclass::impl {
   impl() : ... {
      ...
      do_something;
      ...
   }
   ...
   ...
   static std::vector<std::string> v;
   ...
};

std::vector<std::string> myclass::impl::v = std::vector<std::string>(N); //-N is defined by an enum, say.


myclass::myclass() {
    p_impl = new impl();
    //-Is this where I should populate the static container?
    impl::v[zero] = str0;
    impl::v[one] = str1;
    ...
    impl::v[N] = strN;
}

... 

My questions are: Is the place where I have initialized the static container appropriate? If I moved the initializing of v before allocating p_impl, would it result in run-time errors? In other words, is there an order in which memory is assigned to the static members? Also, how can I make my container to be static const without running into the errors described below?

Additional info:

When I tried to initialize it in the implementation but outside of the struct and any of the class members (and after the assignment that sets the size of the vector) as follows:

std::vector<std::string> myclass::impl::v = std::vector<std::string>(N); //-N is defined by an enum, say.
myclass::impl::v[zero] = str0;
myclass::impl::v[one] = str1;
...
myclass::impl::v[N] = strN;

then I get a build error on Windows (VC++ 2008):

error C2466: cannot allocate an array of constant size 0

This led me to place the initialization at the constructor of myclass (seemed to be a natural place). Additionally, if I tried making the container a static const, everything breaks with errors along the lines as follows.

 error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]
        c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xstring(914): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const std::basic_string<_Elem,_Traits,_Ax> &)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]
        c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xstring(919): or       'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]
        c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xstring(924): or       'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]
        while trying to match the argument list '(const std::basic_string<_Elem,_Traits,_Ax>, const std::string)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]

Does anybody have an example of doing the same thing with a static const container? Appreciate any thoughts and ideas as always. Thanks!

  • 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-06-06T13:30:48+00:00Added an answer on June 6, 2026 at 1:30 pm

    Is the place where I have initialized the static container appropriate?

    Yes, static data members of a class need a definition in namespace scope. But these

    myclass::impl::v[zero] = str0;
    myclass::impl::v[one] = str1;
    ...
    myclass::impl::v[N] = strN;
    

    are expressions, and they’re not allowed in global scope. Compiler is trying to parse them as declarations, hence the error: “cannot allocate an array of constant size 0”. You’ll need to move this logic in a member function.

    If I moved the initializing of v before allocating p_impl, would it result in run-time errors? In other words, is there an order in which memory is assigned to the static members?

    Static members have external linkage, so they’re guaranteed to be fully constructed before main() starts.

    Also, how can I make my container to be static const without running into the errors described below?

    You can only initialize a const variable, not change it later. In C++11 you can do

    std::vector<std::string> myclass::impl::v = { str0, str1, str2, ... };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

suppose you have two (or more) classes with private member vectors: class A {
I have a class which has two HashSet<String> collections as private members. Other classes
I have a class that looks like the following: public class MyClass { private
I have a class which has a struct with pointers as one of its
I have a struct, MyStruct , that has a private member private bool[] boolArray;
I have a class whose private member is a static map: Class Devices {
Assume the following: we have class B, which is a private class nested inside
I have: @Component class MyDecorator{ private Cache cache; /* some wrapped methods like get
I have a class like this public class foo { private void getThread() {
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int

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.