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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:01:14+00:00 2026-05-22T21:01:14+00:00

How can I access base class variable from a child method? I’m getting a

  • 0

How can I access base class variable from a child method? I’m getting a segmentation fault.

    class Base
    {
    public:
        Base();
        int a;
    };

    class Child : public Base
    {
    public:
        void foo();
    };

    Child::Child() :Base(){

    void Child::foo(){
        int b = a; //here throws segmentation fault
    }

And in another class:

    Child *child = new Child();
    child->foo();
  • 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-22T21:01:15+00:00Added an answer on May 22, 2026 at 9:01 pm

    It’s not good practice to make a class variable public. If you want to access a from Child you should have something like this:

    class Base {
    public:
      Base(): a(0) {}
      virtual ~Base() {}
    
    protected:
      int a;
    };
    
    class Child: public Base {
    public:
      Child(): Base(), b(0) {}
      void foo();
    
    private:
      int b;
    };
    
    void Child::foo() {
      b = Base::a; // Access variable 'a' from parent
    }
    

    I wouldn’t access a directly either. It would be better if you make a public or protected getter method for a.

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

Sidebar

Related Questions

How can hibernate can access a private field/method of a java class , for
How can I go about making a child class override a privileged method of
I recently discovered that a method in a derived class can only access the
Why I can't access base class A's a member in class B initialization list?
I've got this trivial class hierarchy: class Base { public: virtual int x( )
I can access the database either from a .NET program (using ODBC) or through
I know you can access the Contact Store from the SDk, but is it
Is there any way i can access the page object from within the global.asax
In javascript is it possible for a child class to access & change a
I believe, a derived class can override only those functions which it inherited from

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.