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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:08:03+00:00 2026-05-26T01:08:03+00:00

I know there is no late binding for class attributes. But i need a

  • 0

I know there is no late binding for class attributes. But i need a good pattern to do this:

#include <cstdlib>
#include <iostream>
using namespace std;

class B
{
public:
    const int i;
    B() : i(1) {}
};

class D : public B
{
public:
    const int i;
    D() : i(2) {}
};


int main()
{
    D d;
    B *ptr = &d;

    cout << ptr->i << endl;

    return 0;
}

Output is 1, but I expected 2. I guess, i should use a different pattern. Any suggestion?

  • 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-26T01:08:03+00:00Added an answer on May 26, 2026 at 1:08 am

    The version of i that gets output is dependent on the compile-time type definition. You’ve defined two different variables, so you’ll get two different results. The way to fix this is to make sure there’s only one version of the variable. You can use a contructor in B to initialize the const variable.

    class B
    {
    public:
        const int i;
        B() : i(1) {}
    protected:
        B(int j) : i(j) {}
    };
    
    class D : public B
    {
    public:
        D() : B(2) {}
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

actually i am using late-binding in delphi, and i need to know wich is
I know there have been a few threads on this before, but I have
I have to do reflection and late binding so i don't know if there
I know there is a registry key indicating the install directory, but I don't
I know there are a lot of positive things mod-rewrite accomplishes. But are there
I know there are HTML entities for 1/2, 1/4, and 3/4, but are there
I know downcasting like this won't work. I need a method that WILL work.
I don't know if it's just too late or what, but I don't see
I've submitted a bunch of questions as of late - but this has been
I have this problem with late binding: I am creating a grocery list application.

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.