C++ inheritance: Can't access private element of base class - c++

I have a base class:
class Base
{
public:
functions...
private:
std::vector<float> privateElement;
}
and a derived one:
class Derived : public Base
{
public:
functions...
Derived(anotherElement) : privateElement(anotherElement)
{
}
}
My problem now is that everytime I try to build my project, the compiler (gcc 4.7.2) always complains about not being able to access privateElement, like:
class Derived does not have any field named privateElement
std::vector<float> Base::privateElement is private
May anybody help me here?

First of all, private members of base class are not accessible from derived class.
Now even if you fix that and make it protected (or public), then this will still be ill-formed, because you cannot initialize the base's members in the derived class's mem-init-list. It doesn't make sense, because by the time the derived class mem-init-list executes the base's members are already initialized, and the syntax : protectedElement(xyz) will make the compiler think that protectedElement is a member of the derived class, not of the base class!
See this error even after making it protected:
main.cpp: In constructor ‘Derived::Derived(std::vector<float>)’:
main.cpp:20:37: error: class ‘Derived’ does not have any field named ‘protectedElement’
Derived(std::vector<float> v) : protectedElement(v)
^
Online demo
The right way to do this is to define a constructor for the base class, and call this from the derived class initialization-list.
Hope that helps.

The right way to do it without breaking encapsulation would be for the base class to provide a constructor which we can call from derived class :
class Derived : public Base
{
public:
functions...
Derived(anotherElement) : Base(anotherElement)
{
}
}
class Base
{
public:
Base(anotherElement):privateElement(anotherElement) { }
private:
std::vector<float> privateElement;
}

Derived class cannot access private parts of the base class. That's because they are, well, private. Use protected if you want the derived class to have access to the base class' members without breaking the encapsulation.

Related

How can I access base class's protected members through derived class? [duplicate]

This question already has answers here:
Accessing protected members in a derived class
(8 answers)
Closed 3 years ago.
I'm trying several programs about inheritance, and it turned out that the following caused an error but I don't really know the rationale.
#include <iostream>
using namespace std;
class Base {
protected:
int x = 0;
};
class Derived: public Base {
// OK: access protected member via this
void g() { cout<<x; }
// OK: access protected member of other Derived
void h(Derived& d) { cout<<d.x; }
// FAIL: access Base class's protected member, why?
void f(Base& b) { cout<<b.x; }
};
int main() {}
I expect that the Derived class could access the ​Base class's public or protected data members and member function.
However it didn't work as what I was thinking about, could anyone help me light up my concepts?
There is not more to it than you already discovered. Derived instances may acces their protected members and those of other derived instances but not those of base class instances. Why? Because thats how protected works by definition.
For more details I refer you to cppreference (emphasize mine):
A protected member of a class Base can only be accessed
1) by the members and friends of Base
2) by the members and friends (until
C++17) of any class derived from Base, but only when operating on an
object of a type that is derived from Base (including this)
void f(Base& b) {cout<<b.x;}
Here you are trying to access a protected member of a different class. It does not matter that you also share the same base class. (still looking for a source)
void g() {cout<<x;}
In this example you are acccessing your own private member. (protected members of base class are inherited and protected in derived class)
void h(Derived& d) {cout<<d.x;}
Here you are accessing the private member of the same class. But for more on this look at this post: Access private elements of object of same class
From this documentation
A protected member of a class Base can only be accessed
by the members and friends of Base
this is not your case
by the members and friends (until
C++17) of any class derived from Base, but only when operating on an
object of a type that is derived from Base (including this)
this is your case, but the argument b is not such a derived type
The reason for protected member access is to allow a base class to define an interface for use by derived classes. That's not the same as allowing every different derived type special access to every base class object.
The code in your question seems like the example in the cppreference website and there we can see a good explanation for that limitation in the code comments:
struct Base {
protected:
int i;
private:
void g(Base& b, struct Derived& d);
};
struct Derived : Base {
void f(Base& b, Derived& d) // member function of a derived class
{
++d.i; // okay: the type of d is Derived
++i; // okay: the type of the implied '*this' is Derived
// ++b.i; // error: can't access a protected member through Base
// (Otherwise it would be possible to change other derived classes,
// like a hypothetical Derived2, base implementation)
}
};
So, if you have a
class Derived2: public Base {
};
The Derived class shall not be allowed to access Derived2 protected attributes as it is not a child of Derived2. The purpose of protected is not to allow siblings but children class access to members.
Full details in the standard:
http://eel.is/c++draft/class.access#:access_control,protected
http://eel.is/c++draft/class.access#class.protected

Protected Constructor in multilevel virtual inheritance in C++

How is the folloing code working? MakeFinal constructor is protected, so it should not be accessible to FinalUser class. But I didn't get any build or execution error.
class MakeFinal
{
protected:
MakeFinal(void) {};
public:
~MakeFinal(void) {};
};
class Final : virtual public MakeFinal
{
public:
Final(void) {};
~Final(void) {};
};
class FinalUser : public Final
{
public:
FinalUser(void) {};
~FinalUser(void) {};
};
int main()
{
FinalUser *finalUserHeap_ = new FinalUser();
return 0;
}
A virtual base class is initialized by the single most derived class' constructor's member initializer list.
Because the virtual base can be a common base-class object for multiple derived classes, and the initializations specified by those derived classes can conflict.
The initialization specification in the most derived class acts conceptually as if the most derived class was derived directly from the virtual base class, i.e.
FinalUser(void) {};
… is equivalent to
FinalUser(): MakeFinal() {}
Since the MakeFinal constructor is protected, it's available to all derived classes.
That includes that it's available to class FinalUser.
In other news:
The names in this code indicate that it's about using a C++03 trick for creating a class that can't be (usefully) derived from, a “final” class. The trick is essentially to have a class template that can act as most derived class and that has the necessary friend-ship to access the for other classes inaccessible constructor of the virtual base class. C++11 introduced the keyword final to do that more easily, and without the overhead of virtual inheritance.
You need to know the next:
If the inheritance is public, everything that is aware of Base and Child is also aware that Child inherits from Base.
If the inheritance is protected, only Child, and its children, are aware that they inherit from Base.
If the inheritance is private, no one other than Child is aware of the inheritance.
#Anzurio answer in Difference between private, public, and protected inheritance
According this. You need to use private if you want that FinalUser class donot have access to MakeFinal.
Derived classes have access to protected members of their base classes. That's the difference between protected and private.

does a derived class object contain private members of the base class? What does it look like in memory?

class base{
private:
int a;
public:
int b;
void setData(){
a = 10; b = 5;
}
};
class derived: public base{
private:
int c;
public:
// b is inherited
};
void main(){
derived D1;
D1.setData();
}
I learned that private members aren't inherited. So, the private variable a (in the base class) is not present in the derived class object. My question is when D1.setData() is called, how can it assign a value to a, if a doesn't exist?
I'm pretty sure I have this concept wrong, so can some one explain how this works? Are the base class members also created when the derived class object is created?
I learned that private members aren't inherited.
Of course they are inherited: otherwise, all member functions of the base class that need these private variables would be broken. Although private members are not accessible to the inheriting class, the base class retain full access to them.
Can some one explain how this works?
Layout of the inheriting class derived includes a place to store a, the private member of the base class. All methods of the base class can access base::a. At the same time, it remains inaccessible to the derived class: any attempt to access a from derived would cause a compile-time error.
Private member variables are inherited, but cannot be accessed externally. The memory pattern of inheritance is simple:
class base {
int x;
};
class subclassCpp : public base {
int y;
};
class subclassCStyle {
base a;
int y;
};
Now, subclassCpp and subclassCStyle have the exact same memory pattern, regardless of private/protected status, etc. This should illustrate how the memory is laid out.
For the other question, "Are the base class members also created when the derived class object is created?"
The answer is yes, a constructor is always invoked on the base class. If you don't add this yourself, the default constructor is automatically invoked. If the base class doesn't have a default constructor, it won't let you create a subclass constructor that doesn't properly invoke whatever constructor is necessary to initialize the base. So there is no legal way you can end up with a base class that hasn't been initialized when the subclass was created, as long as a constructor in the subclass was executed.

friendship from derived class method to base class members

I would like to know if there's a way to make a method from a derived class a friend of its base class. Something like:
class Derived;
class Base
{
int i, j;
friend void Derived::f();
protected:
Base();
};
class Derived : public Base
{
public:
void f();
};
The errors I got were:
error: C2027: use of undefined type 'Derived'
see declaration of 'Derived'
error: C2248: 'Base::i' : cannot access private member declared in class 'Base'
see declaration of 'Base::i'
see declaration of 'Base'
error: C2248: 'Base::j' : cannot access private member declared in class 'Base'
see declaration of 'Base::j'
see declaration of 'Base'
error: C2027: use of undefined type 'Derived'
see declaration of 'Derived'
I struggled with it during all the day. Everything I found about friendship use only separated classes, not inheritance.
No there is no direct way : Base needs the definition of Derived::f while Derived also needs the definition of it's Base class.
But it does not matter, you should not do that, you can, in order of preference :
Provide protected accessors in the Base class
Make the entire Derived class a friend (not necessary in general)
You can use an intermediate helper class which only forward the call of this specific method, and give it friendship :
Example here:
class Base;
class Derived;
class Helper final
{
friend class Derived;
public:
void f(Base* base);
private:
Helper() {}
};
class Base
{
int i, j;
friend class Helper;
protected:
Base() {}
};
class Derived : public Base
{
public:
void f();
private:
Helper helper;
};
void Helper::f(Base* base)
{
base->i = 10; base->j = 5;
std::cout << "Help !" ;
}
void Derived::f()
{
helper.f(this);
}
One approach for this kind of problem is to apply the rule "if it's a thing, then it's a class."
#quantdev solution is on those lines.
Based on the comment:
assuming that I have two classes both derived from base class and
having an identical private member. Why not saving some codes by
putting that member in the base class and providing a friend access to
both derived classes that need the member. Assuming that other derived
classes won't be able to access that member at all. That's what I'm
trying to achieve
[I know that this does not answer the specified question, but may be what you need.]
I'd solve that by factoring the common element into an intermediate class:
class Base
{
public:
Base();
virtual ~Base() = 0;
};
class Derived : public Base
{
public:
void f()
{
i = 1;
}
private:
int i, j;
};
class Foo : public Derived
{};
class Bar : public Derived
{};
class Fred : public Base
{};
I don't know if it is possible to do exactly what you want (although it seems to me like it should be) -- and I'd be interested to see other answers that show how if it is -- but there are a few other ways to achieve what you want. I assume you are asking about this situation in general -- that is, you are also interested in the case where there can be many different derived classes, not all of which need, no should have access to the private fields of Base (otherwise you should make these fields protected of course).
First, the easiest thing to do would be to make Derived a friend class to Base, although this feels like a hack (in part because it doesn't allow for any encapsulation of Base with respect to Derived) and is definitely sub-optimal here.
In my opinion, a somewhat better approach in terms of encapsulation would be to make an external "free" (non-member) function that is a friend to Base (and potentially Derived if you need that too). This gets around the circular compiler error, but unfortunately still loses the conceptual semantics of being an "operation on Derived".

why does the derived class inherit the private members of the base class? [duplicate]

This question already has answers here:
Do Sub-Classes Really Inherit Private Member Variables?
(7 answers)
Closed 5 years ago.
I know that the derived class can't access the private members of the base class, so why does the derived class inherit the private members of the base class? Is there any case that it is useful?
Thanks!
The derived class needs the private members even though it can't access them directly. Otherwise it's behavior would not build on the class it is deriving from.
For example, pretend the private stuff is:
int i;
and the class has a geti() and seti(). The value of i has to be put somewhere, even if it is private,
The public and protected methods of the base class can still access private variables of the base class, and these methods are available in the derived class.
The base class can still use the private member variables & methods.
If you want the derived classes to have access to members but keep those members hidden from the outside world, make them protected:.
Here's an example to illustrate:
class Base
{
public:
Base() : val(42.0f) {};
float GetValue() const
{
return val_;
}
private:
float val_;
};
class Derived : public Base
{
public:
float ComputedValue() const
{
return GetValue() * 2.0f;
}
};
Don't forget that the base class may have methods that are not private, and thus accessible by the derived class. Those protected or public base class methods can still invoke the private base class methods. This is particularly useful if you want to lock down core functionality in the base class, such as with a Template Method design pattern implementation:
class base
{
public:
virtual ~base() { /* ... */ }
virtual void base_func() { foo_private (); }
virtual void do_func() = 0;
private:
void foo_private()
{
// pre-do_func() operations
do_func();
// post-do_function operations
}
};
class derived : public base
{
public:
void derived_func() { base_func(); }
virtual void do_func()
{
// Derived class specific operations
}
};
The reason is because derived classes have an is-a relationship to the superclass.
A derived class instantiation IS A superclass instantiation...just with more (or less due to setting some superclass functions private) stuff.
As has been outlined by other answers here, the derived class syntactically cannot access the private members of the base class; but it needs to have a copy of the same in its memory layout. Think of casting. using 'C' casting you can cast a derived to a private base. The compiler would then need the correct memory offset in order to produce a valid memory-layout for the base object.
Ex.
class Base {
public:
void printA() {
a = 10;
std::cout << a << std::endl;
}
private:
int a;
};
class Derived : private Base{
int b;
};
Derived* d = new Derived;
Base* b = (Base*)d;
b->printA();
The derived class doesn't "inherit" the private members of the base class in any way - it can't access them, so it doesn't "inherit" them.
An instance of the derived class contains instances of the private members of the base class, for obvious reasons.
So I don't even know what you mean by that question.
when derived class object is created, base class constructor is also called for base object creation. if private members of base class are not allocated memory , the base object will be incomplete.
hence derived class object inherits private members of base, as they are created during creation of base class object, but are not accessible as they are private.
Although the private members are not accessible from the base class, they are inherited by them because these properties are used by the derived class with the help of non-private functions.
Private members of the base class are not directly accessed, but derived by base class by derived class.