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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:18:42+00:00 2026-05-15T17:18:42+00:00

This might have been asked a million times before or might be incredibly stupid

  • 0

This might have been asked a million times before or might be incredibly stupid but why is it not implemented?

class A
{
      public:
             A(){ a = 5;}
             int a;
};

class B:public A
{
      public:
             B(){ a = 0.5;}
              float a;
};

int main()
{
    A * a = new B();

    cout<<a->a;
    getch();
    return 0;
}

This code will access A::a. How do I access B::a?

  • 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-15T17:18:43+00:00Added an answer on May 15, 2026 at 5:18 pm

    To access B::a:

    cout << static_cast<B*>(a)->a;
    

    To explicitly access both A::a and B::a:

    cout << static_cast<B*>(a)->A::a;
    cout << static_cast<B*>(a)->B::a;
    

    (dynamic_cast is sometimes better than static_cast, but it can’t be used here because A and B are not polymorphic.)

    As to why C++ doesn’t have virtual variables: Virtual functions permit polymorphism; in other words, they let a classes of two different types be treated the same by calling code, with any differences in the internal behavior of those two classes being encapsulated within the virtual functions.

    Virtual member variables wouldn’t really make sense; there’s no behavior to encapsulate with simply accessing a variable.

    Also keep in mind that C++ is statically typed. Virtual functions let you change behavior at runtime; your example code is trying to change not only behavior but data types at runtime (A::a is int, B::a is float), and C++ doesn’t work that way. If you need to accommodate different data types at runtime, you need to encapsulate those differences within virtual functions that hide the differences in data types. For example (demo code only; for real code, you’d overload operator<< instead):

    class A
    {
      public:
             A(){ a = 5;}
             int a;
             virtual void output_to(ostream& o) const { o << a; }
    };
    
    class B:public A
    {
      public:
             B(){ a = 0.5;}
             float a;
             void output_to(ostream& o) const { o << a; }
    };
    

    Also keep in mind that making member variables public like this can break encapsulation and is generally frowned upon.

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

Sidebar

Related Questions

I know this might have been asked a thousand times before, but I can't
I know that this question might have been asked like 100 times, but, believe
I am sure this might have been asked before but I've been pulling my
This question might have been asked several times, but i didn't find a solution
This question might have been asked many times but still there are many factors
I know this question might have been asked a few times already, but I
I know this might have been probably asked before but through out the net
This might have been asked before but I'm really looking for a simple way
Well, this certainly might have not been asked before. I have a method that
I know this question might have already been discussed a thousands of times but

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.