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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:23:07+00:00 2026-06-17T16:23:07+00:00

I have a private static member variable of a class Central, with type map.

  • 0

I have a private static member variable of a class Central, with type map. I want to populate this map with ” Base* ” pointers, which point to instances of classes deriving from ” Base “. These instances of the derived classes must be stored in dynamic memory. There is a bit of template fun involved also. I did try a method to populate the map, but it gave me a compilation error (which will be stated later).

Here is a code snippet for clarity:

#include <all_necesary_std_headers>

class Base
{
    /* guts */
};

template<class CType>
class Derived: public Base
{
    /* innards */
};

class Central
{
    /* partial entrails */
private:
    // the static map I was referring to
    static std::map<string, Base*> base_map;
};

// Initializing the static map here
std::map<string, Base*> Central::base_map;

class Test1
{
    /* viscera */
};

// Compilation error here, on next line of code.
Central::base_map["Test1"] = dynamic_cast<Base*>( new Derived<Test1>);

class Test2
{
    /* bowels */
};

// Compilation error here, on next line of code.
Central::base_map["Test2"] = dynamic_cast<Base*>( new Derived<Test2>);

This is the Compilation error that I get:
error: expected constructor, destructor, or type conversion before ‘=’ token;

I already have a destructor that frees the memory allocated by the map, so no need to remind me. I want to use the class ” Central ” in main(). This class structure will be used to dynamically create new instances of classes, from class names stored in files.

Hope it is clear, please say if anything isn’t.

  • 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-17T16:23:08+00:00Added an answer on June 17, 2026 at 4:23 pm

    Assignment statements can only go in functions, not at namespace scope.

    In C++11, you can initialise the map in its declaration:

    std::map<string, Base*> Central::base_map = {
        {"Test1", new Derived<Test1>},
        {"Test2", new Derived<Test2>}
    };
    

    If you’re stuck in the past, then you’ll need to use a function to populate the map; perhaps something like:

    std::map<string, Base*> make_base_map() {
        std::map<string, Base*> map;
        map["Test1"] = new Derived<Test1>;
        map["Test2"] = new Derived<Test2>;
        return map;
    }
    
    std::map<string, Base*> Central::base_map = make_base_map();
    

    Also, note that you can’t use dynamic_cast unless these types are polymorphic (i.e. Base has at least one virtual function). You can convert Derived<T>* to Base* without a cast, as I’ve done in my examples; but if you need to convert back with dynamic_cast then you’ll need to make sure they are polymorphic.

    UPDATE: If you want to be able to register each class separately by adding code only to that class’s implementation files, then you’ll need to declare a static object that registers the class in its constructor. Now we run into the initialisation order fiasco: if static objects are initialised in different translation units then the initialisation order is unspecified, so you can’t safely access one from the constructor of the other.

    We can fix this by making the map a local static variable, initialised the first time it’s accessed:

    static std::map<string, Base*> & base_map() {
        static std::map<string, Base*> map;
        return map;
    }
    

    Now we can define the class to register the classes:

    template <typename T>
    struct BaseMapEntry {
        explicit BaseMapEntry(string name) {
            base_map()[name] = new Derived<T>;
        }
    };
    

    And use it for each class:

    // header file
    class Test1
    {
        static BaseMapEntry<Test1> entry;
        /* gubbins */
    };
    
    // source file
    BaseMapEntry<Test1> Test1::entry("Test1");
    

    (Alternatively, you could make it a static namespace-scope object in the source file, rather than a static member; the choice is largely aesthetic. Unfortunately, whatever you do will have to be defined in the source file due to the One Definition Rule.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with a static std::map member variable that maps char s
I want to have a class with a private static data member: class C
Is it OK to have a static member variable defined in a base class,
I have a static map that is a private data member. How do I
I have this class: public static class CsvWriter { private static StreamWriter _writer =
I have this small class looking like this: private static int field1 = -
I have code which looks like: private static DirectiveNode CreateInstance(Type nodeType, DirectiveInfo info) {
I have a header file which contains a member variable declaration of a static
Possible Duplicate: how do i access static member of a class? I have this
When a static member variable is declared private in a class, how can it

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.