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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:37:25+00:00 2026-05-25T20:37:25+00:00

I have a rather strange situation where I would like to be able to

  • 0

I have a rather strange situation where I would like to be able to define certain constants that a subclass of an ABC can override.

struct A {
    static const int a = 20;
    virtual int func() = 0;
};
struct B : public A {
    static const int a = 3;
    int func() { return 5; }
};
struct C : public A {
    static const int a = 4;
    int func() { return 3; }
};

Unfortunately, if I use A *aPtr = new B, aPtr->a will return 20, instead of 3.

One workaround I see is one-liner functions (along the lines of func in the above example), but the syntax of constants is quite a bit more appropriate for this particularly situation conceptually. Is there a syntactically reasonable way of resolving which constants to use at runtime, where the calling code doesn’t need to know anything after initial object creation?

  • 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-25T20:37:26+00:00Added an answer on May 25, 2026 at 8:37 pm

    Constants, especially static constants, cannot be overriden like you are asking for. You would have to use a virtual function instead:

    struct A {
        virtual int get_a() { return 20; }
        int func() = 0;
    };
    
    struct B : public A {
        virtual int get_a() { return 3; }
        int func() { return 5; }
    };
    
    struct C : public A {
        virtual int get_a() { return 4; }
        int func() { return 3; }
    };
    

    Another option would be to use a template for the constant instead:

    template< const int a_value = 20 >
    struct A {
        static const int a = a_value;
        int func() = 0;
    };
    
    struct B : public A<3> {
        int func() { return 5; }
    };
    
    struct C : public A<4> {
        int func() { return 3; }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rather strange bug which i can't make sense of that is
I have a rather large diff of 2 GIT branches and would like to
I have a rather strange problem: when certain sales are made (completed) a record
I have a rather strange problem. I have a very simple application that reads
I have a strange behaviour. I am using a rather heavy page (4000 nodes)
I have rather large project that uses ICU regex classes. Basically it might run
Have a rather simple question. Does anyone knows if i can use jparallax both
I have a rather huge query that is needed in several stored procedures, and
I have a rather complicated deploy setup for our Drupal site that is a
I have a rather strange requirement. I have property files in which same keys

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.