Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private methods suddenly inaccessible when chaining

Tags:

c++

I have a simple class hierarchy with a base class and a derived class. The base has two protected members that the derived class calls. Coming from some recent C# experiences, I thought it would be good to make the interface a bit more fluent and allow chaining of method calls, so instead of calling this->A(), then this->B() you can call this->A()->B(). However, the following code will not compile:

#include <iostream>

class Base
{
  protected:
    Base* A()
    {
      std::cout << "A called." << std::endl;    
      return this;
    }

    Base* B()
    {
      std::cout << "B called." << std::endl;    
      return this;
    }
};

class Derived : public Base
{
  public:
    void Test()
    {
        // Base::A and Base::B are private here.
        this->A()   // This works fine
            ->B();  // Suddenly I cannot access my own private method?
    }
};

int main()
{
    Derived d;
    d.Test();

    return 0;
}

This produces the following compiler error:

main.cpp: In member function 'void Derived::Test()':
main.cpp:12:15: error: 'Base* Base::B()' is protected
         Base* B()
               ^
main.cpp:26:21: error: within this context
                 ->B();  // Suddenly I cannot access my own private method?
                     ^

I also tried making the base class methods virtual, but that didn't help.

My C++ is rusty enough that I can't seem to figure out what is going on here, so help would be much appreciated. Also I was wondering if this is a bad idea because C++ != C# and C++-people aren't used to such fluent interfaces.

like image 590
CompuChip Avatar asked Feb 04 '14 10:02

CompuChip


People also ask

Do private methods get inherited in Java?

private methods are not inherited. A does not have a public say() method therefore this program should not compile.

Why are there no private methods in Python?

Strictly speaking, private methods are accessible outside their class, just not easily accessible. Nothing in Python is truly private; internally, the names of private methods and attributes are mangled and unmangled on the fly to make them seem inaccessible by their given names.

Can we access private method?

You can access the private methods of a class using java reflection package. Step1 − Instantiate the Method class of the java. lang. reflect package by passing the method name of the method which is declared private.

What happens if we call a private method in the ctor?

It's ok to call private methods from your constructor to initialize some data that used inside the class. Just be sure that your methods have no "side-effects" like long-time methods that user of your class would probably not expect with just calling your constructor.


2 Answers

A protected member in a class is accessible from a derived class only through that derived class, i.e. through an object of, or reference or pointer to, that derived class.

The return type of A() is Base*, which is not the derived class, which is why you can't access its protected members. The compiler doesn't track that it really refers to the same object.

like image 127
Sebastian Redl Avatar answered Oct 24 '22 16:10

Sebastian Redl


Yes, you cannot call protected methods of Base class from Base *. You can think as protected methods are private with a difference that they become private of a derived class as well.

like image 23
Alex Telishev Avatar answered Oct 24 '22 15:10

Alex Telishev