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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:40:01+00:00 2026-05-15T07:40:01+00:00

Example code is included at the bottom of the message. I’m puzzled about the

  • 0

Example code is included at the bottom of the message.
I’m puzzled about the protected access specifier in a class.
I have define a class node which has a protected string member name

string name;

and a vector of node pointers

vector args;

Before I thought that a member function of node could not do

args[0]->name

but a program that does just this does compile and run.
However, now I would like to inherit this class and access the name field
in one of the args array pointers from this derived class

args[0]->name

but this does not compile. When I compile the example code below with
the commented sections uncommented, the compiler reports:

Compiler output:

g++ test.cc -o test

test.cc: In member function ‘void foo::newnode::print_args2()’:

test.cc:22: error: ‘std::string foo::node::name’ is protected

test.cc:61: error: within this context

Compilation exited abnormally with code 1 at Thu Jun 17 12:40:12

Questions:

  1. Why can I access the name field of the node pointers in args in
    class node, because this is what I would excpect from a similarly
    defined private field in Java.

  2. How can I access those fields from the derived class.

Example code:

    #include <iostream>
#include <vector>

using namespace std;

namespace foo
{
  class node;
  typedef std::vector<node*> nodes;

  class node
  {
  public:
    node (string _name);


    void print_args ();
    void add_node (node* a);

  protected:
    nodes args;
    string name;

  };
}

foo::node::node (string _name)
  : args(0)
{
  name = _name;
}

void foo::node::add_node (node* a)
{
  args.push_back(a);
}

void foo::node::print_args ()
{
  for (int i = 0; i < args.size(); i++)
  {
    cout << "node " << i << ": " << args[i]->name << endl;
  }
}

// namespace foo
// {
//   class newnode : public node
//   {
//   public:
//     newnode (string _name) : node(_name) {}
//     void print_args2 ();
//   protected:
//   };
// }

// void foo::newnode::print_args2 ()
// {
//   for (int i = 0; i < args.size(); i++)
//   {
//     cout << "node " << i << ": " << args[i]->name << endl;
//   }
// }

int main (int argc, char** argv)
{
  foo::node a ("a");
  foo::node b ("b");
  foo::node c ("c");
  a.add_node (&b);
  a.add_node (&c);
  a.print_args ();

  // foo::newnode newa ("newa");
  // foo::newnode newb ("newb");
  // foo::newnode newc ("newc");
  // newa.add_node (&newb);
  // newa.add_node (&newc);
  // newa.print_args2 ();

  return 0;
}
  • 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-15T07:40:02+00:00Added an answer on May 15, 2026 at 7:40 am

    The compiler will allow that an object A accesses private/protected members of an object B if A and B have the same static type.

    I will try to make this clear with an example:

    class Base
    {
        protected:
            int a;
    };
    
    class Derived : public Base
    {
        public:
            void foo(Base& b, Derived& d)
            {
                //allowed because this obviously has the same type as this :)
                a = 1;
                //allowed because this has the same type as d (Derived)
                d.a = 1;
                //not allowed because this (Derived) does not have the same
                //type as b (Base). They might have the same dynamic type
                //but the compiler has no way of knowing this.
                b.a = 1;
            }
    };
    

    So, to answer your questions:

    1. Class node is allowed to access the name field if the node pointers of your args vector because they are also of class node.
    2. You cannot directly. You either have to make the field public (I wouldn’t do that) or make public accessors.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the example code included in the man page for DateTime::Astro::Sunrise , I'm getting
Can someone post an example of as3 code (specifically event listener included) that would
Example code: #include <cstdlib> #include <iostream> using namespace std; class A { public: A(int
Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
I have the following example code: $dataProvider = new CActiveDataProvider('firstTable', array('criteria' => array( 'select'
I'm trying to compile the message pack ( http://msgpack.org/ ) example code and keep
I have this javascript code, from google charts api right in the bottom of
Example code: <html> <head> <script src=jquery-1.3.2.min.js type=text/javascript></script> <script src=jquery-ui-1.7.1.custom.min.js type=text/javascript></script> </head> <body> <table border=1
Example code : int main() { std::vector<int> v1{1, 2, 3, 4, 5, 6, 7,
The example code below works as as a server process. But when I add

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.