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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:23:35+00:00 2026-05-12T16:23:35+00:00

Is something similar to this possible? This way it produces an error. class A

  • 0

Is something similar to this possible? This way it produces an error.

class A {
    public:
    virtual std::string key;
};

class B : public A {
    public:
    std::string key;
};

int main()
{
    A a;
    a.key = "Foo";
    return 1;
}
  • 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-12T16:23:35+00:00Added an answer on May 12, 2026 at 4:23 pm

    No, because this doesn’t really make sense. Remember that a subclass contains all members of its parent class; therefore, B still has A‘s std::string key. Moreover, since B‘s std::string key is the same type, it’s absolutely identical to A‘s – so what was the point of overriding it?

    Also, note that during construction, when we’re running A‘s constructor, B‘s virtual methods won’t be called. This means that if we access key during A‘s construction, we’d get A‘s key – but then when B is constructed, that key would be shadowed, its data completely inaccessible.

    That said, if you really do want to do something like this, for some reason, you’ll need to use a virtual accessor function:

    class A {
        private:
        std::string m_key;
        public:
        virtual std::string &key() { return m_key; }
        virtual const std::string &key() const { return m_key; }
    };
    
    class B : public A {
        private:
        std::string m_key;
        public:
        virtual std::string &key() { return m_key; }
        virtual const std::string &key() const { return m_key; }
    };
    
    int main()
    {
        B b;
        b.key() = "Foo";
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to do a macro selection using something similar to this (wrong)
I would do something similar to this in PHP: http://mysite.com/en/museum/gallery/garden http://mysite.com/es/museo/galeria/jardin It's possible? How
I'm trying to insert a comment character into a string something similar to this:
I have a test method created using Selenium, something similar to this: [TestFixture] public
I'm looking for something similar to this: Active navigation with jQuery - can't apply
I'm trying to write something similar to this (sorry if the sample is not
I would like to perform something similar to this (ie get the sum of
I want to be able to get something similar to this.. I want to
I want to know i can do something similar to this (not working) code
First of all, my table structure is something similar like this: CREATE TABLE Testing(

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.