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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:11:50+00:00 2026-05-30T21:11:50+00:00

For a code like this: class foo { protected: int a; public: class bar

  • 0

For a code like this:

class foo {
  protected:
    int a;
  public:
    class bar {
      public:
        int getA() {return a;}   // ERROR
    };
    foo()
      : a (p->param)
};

I get this error:

 invalid use of non-static data member 'foo::a'

currently the variable a is initialized in the constructor of foo.

if I make it static, then it says:

 error: 'int foo::a' is a static data member; it can only be initialized at its definition

However I want to pass a value to a in the constructor.
What is the solution then?

  • 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-30T21:11:52+00:00Added an answer on May 30, 2026 at 9:11 pm

    In C++, unlike (say) Java, an instance of a nested class doesn’t intrinsically belong to any instance of the enclosing class. So bar::getA doesn’t have any specific instance of foo whose a it can be returning. I’m guessing that what you want is something like:

        class bar {
          private:
            foo * const owner;
          public:
            bar(foo & owner) : owner(&owner) { }
            int getA() {return owner->a;}
        };
    

    But even for this you may have to make some changes, because in versions of C++ before C++11, unlike (again, say) Java, a nested class has no special access to its enclosing class, so it can’t see the protected member a. This will depend on your compiler version. (Hat-tip to Ken Wayne VanderLinde for pointing out that C++11 has changed this.)

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

Sidebar

Related Questions

Say I have some code like this: class Foo { public: Foo(int v) :
I saw someone writing code like this , in a C++ class: int foo
I have a class like this one: public class Foo { public readonly int
What are the cons of some code like this: public class Class1 { public
I've sometimes seen code written like this : public class B1 { } public
Suppose I have code like this: template<class T, T initial_t> class Bar { //
I have code that looks like this: template<class T> class list { public: class
i declared classes like this: class Foo{ public function __construct(){ echo 'Foo was created!';}
Consider this abstract class public abstract class Foo { public Injectable Prop {get;set;} }
Consider the following example code: class Foo { }; class Bar : public Foo

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.