To simplify my problem, I have something like this:
class Base {
private:
protected:
int a,b;
string c;
public:
[some functions here]
}
class Derived : public Base{
[some variables and functions]
friend void function();
}
void function(){
int d[a][b];
[stuff]
}
basically, my void function needs to access stuff that is in the protected class of the base class. I'd like to keep those variables defined in the protected section. Is there anyway for function, which HAS to be friended into the Derived class, to be able to access a and b?
You can define private methods in Derived: any method in Derived can access the protected members of Base.
Function is a friend of Derived, so it can invoke those private methods in Derived, which in turn access the protected members of Base.
Edit to reply to the comment below
The get_a() member method and the a member data are members of their classes. Instances of those members exist within instances of their classes. They don't exist except within an instance, so to access them you need to access them via an instance of the class.
For example, something like this:
class Derived : public Base{
[some variables and functions]
friend void function(Derived& derived);
};
void function(Derived& derived)
{
int a = derived.get_a();
int b = derived.get_b();
// I don't know but even the following might work
int c = derived.a; // able to access field of friend's base class.
}
void test()
{
Derived* derived = new Derived();
function(*derived);
delete derived;
}
Your functions need to access a and b through an Instance of the class Derived as follows
void function()
{
Derived objectDerived;
int d[objectDerived.a][objectDerived.b];
[stuff]
}
Related
I have a base class in C++ that has some protected member variables (although, I do not think it is relevant that it is protected vs. private in this case).
I have a derived class that derives from this base class. In it, there is a public function that creates an object of the base class and returns that object. However, in that function I need to be able to set the protected member variables into a special state.
Example:
class Base
{
protected:
int b_value;
};
class Derived : public Base
{
public:
Base createBase()
{
Base b;
b.b_value = 10;
return b;
}
};
I specifically only want the derived class to be able to the protected member variable. I do not want a public accessor method in the base class.
I originally tried to fix this by making the derived class's createBase() function be a friend of the Base class. Like so:
class Base
{
protected:
int b_value;
friend Base Derived::createBase();
};
class Derived : public Base
{
public:
Base createBase()
{
Base b;
b.b_value = 10;
return b;
}
};
As you can see, this will not compile since Derived has not been defined yet. If it matters, these two classes are defined in separate header files. I guess one way to describe this problem is a "chicken and egg" problem where one needs the other first.
I have a feeling this has to be a "I am not designing my classes correctly and need to rethink how I am doing this" but I cannot figure out how to get this to work.
You can forward declare Derived and then make it a friend in Base :
class Derived;
class Base
{
friend class Derived;
protected:
int b_value;
};
class Derived : public Base
{
public:
Base createBase()
{
Base b;
b.b_value = 10;
return b;
}
};
However this design seems seriously flawed to me as you already stated, you should probably make createBase() a static public method in your Base class and have a setter for b_value or a constructor that sets it.
Remember that right now inside createBase(), this->b_value is also available.
I have a class, it has several public methods that use one private field that they all changed and use this field in their computations.
I need subclass from this class with few new methods, new methods won't use this field. I expect those methods from the base class behave the same as they did, but a little bit confused that they use private field.
Are private field accessible in the subclass?
Should I also declare a field with same type and name in my subclass?
Private member data is not accessible to derived classes. If you want to access this data, change it to protected. Public member functions of the base class that are used to change the private member will still be available to your derived class but direct access of base class private data is not allowed. All public and protected fields of the base class will be available to your derived class (assuming you are using public inheritance)
Private fields are never accessible to derived classes. If you declared your methods which access the private field as public, the derived classes can use them.
Most posters seem to misunderstood your question.
YES, you can use those public methods. That is the whole point of private members - they can be accessed by any member of the same class be they public, protected or private
You could use the public functions (which use a private field) in you drived class.
You should not declare a field with same type!!
But you could not direct access a private field from the base class in the drived class. Use for this a public function from your base class (e.g. setter and getter functions)
Sample:
//***********************************
//Base Class
//***********************************
class Base
{
public:
Base();
~Base();
void increment();
void decrement();
void get();
private:
int id_;
};
Base::Base()
: id_(0)
{
}
Base::~Base()
{
}
void Base::increment()
{
++id_;
}
void Base::decrement()
{
--id_;
}
int Base::get()
{
return id_;
}
void Base::set(id)
{
id_ = id;
}
//***********************************
// Drived Class
//***********************************
class Drived() : public Base
{
public:
Drived();
~Drived();
void plus(int a);
void minus(int b);
};
Drived::Drived()
: Base()
{
}
Drived::~Drived()
{
}
void Drived::plus(int a)
{
int i = get();
set(i + a); // <- OK
}
void Drived::minus(int b)
{
id_ -= b; // <- Not allowd; id is private
}
In the derived class members, you cannot access the private field. However, the base class members, which you have inherited through the virtue of them being public, can of course still access it. The base member functions will be visible through the derived class if you used public inheritance, but only to the derived class if you used protected or private inheritance. protected and private inheritance is rarely used, though default if you are using the class keyword.
For further clarification, if you did want to access the field, you should define accessors in the base class with protected access. This would allow you to control the access to the private field.
However, you should ask yourself whether polymorphism is the correct solution here? Does the derived class pass the is-a test? Is the base class's destructor virtual?
I believe, a derived class can override only those functions which it inherited from the base class. Is my understanding correct.?
That is if base class has a public member function say, func, then the derived class can override the member function func.
But if the base class has a private member function say, foo, then the derived class cannot override the member function foo.
Am i right?
Edit
I have come up with a code sample after studying the answers given by SO members. I am mentioning the points which i studied as comments in the code. Hope i am right. Thanks
/* Points to ponder:
1. Irrespective of the access specifier, the member functions can be override in base class.
But we cannot directly access the overriden function. It has to be invoked using a public
member function of base class.
2. A base class pointer holding the derived class obj's address can access only those members which
the derived class inherited from the base class. */
#include <iostream>
using namespace std;
class base
{
private:
virtual void do_op()
{
cout << "This is do_op() in base which is pvt\n";
}
public:
void op()
{
do_op();
}
};
class derived: public base
{
public:
void do_op()
{
cout << "This is do_op() in derived class\n";
}
};
int main()
{
base *bptr;
derived d;
bptr = &d;
bptr->op(); /* Invoking the overriden do_op() of derived class through the public
function op() of base class */
//bptr->do_op(); /* Error. bptr trying to access a member function which derived class
did not inherit from base class */
return 0;
}
You can override functions regardless of access specifiers. That's also the heart of the non-virtual interface idiom. The only requirement is of course that they are virtual.
But if the base class has a private member function say, foo, then the derived class cannot override the member function foo.
In Java, you can't. In C++, you can.
You are correct in that to override a function in a derived class, it must inherit it from the base class (in addition, the base class function must be virtual). However you are wrong about your assumption that virtual functions are not inherited. For example, the following works well (and is actually a known idiom for precondition/postcondition checking):
class Base
{
public:
void operate_on(some thing);
private:
virtual void do_operate_on(some thing) = 0;
};
void Base::operate_on(some thing)
{
// check preconditions
do_operate_on(thing);
// check postconditions
}
class Derived: public Base
{
// this overrides Base::do_operate_on
void do_operate_on(some thing);
};
void Derived::do_operate_on(some thing)
{
// do something
}
int main()
{
some thing;
Base* p = new Derived;
// this calls Base::operate_on, which in turn calls the overridden
// Derived::do_operate_on, not Base::do_operate_on (which doesn't have an
// implementation anyway)
p->operate_on(thing);
delete p;
}
A way to see that private methods are really inherited is to look at the error messages generated by the following code:
class Base
{
private:
void private_method_of_B();
};
class Derived:
public Base
{
};
int main()
{
Derived d;
d.private_method_of_B();
d.method_that_does_not_exist();
}
Trying to compile this with g++ leads tot he following error messages:
privatemethodinheritance.cc: In function 'int main()':
privatemethodinheritance.cc:4: error: 'void Base::private_method_of_B()' is private
privatemethodinheritance.cc:15: error: within this context
privatemethodinheritance.cc:16: error: 'class Derived' has no member named 'method_that_does_not_exist'
If class Derived wouldn't inherit that function, the error message would be the same in both cases.
No You can not over ride any function in base class .
My reason for this notion is that if you define the function in derived class that has the same function signiture in base class , the base class function will become hidden for derived class .
For more information about this exciting issue just visit :
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Foverload_member_fn_base_derived.htm
It's also depends on the type of inheritance
class Derived : [public | protected | private] Base { };
In order to override a function inherited from base class, it should be both virtual, and classified as public or protected.
I have a program where I need to make a base class which is shared between a dll and some application code. Then I have two different derived classes, one in the dll one in the main application. Each of these have some static member functions which operate on the data in the nase class. (They need to be static as are used as function pointers elsewhere). In its simplest form my issue is shown below.
class Base {
protected:
int var ;
};
class Derived : public Base {
static bool Process( Base *pBase ) {
pBase->var = 2;
return true;
}
};
My compiler complains that I cannot access protected members of pBase even though Derived has protected access to Base. Is there any way around this or am I misunderstanding something?
I can make the Base variables public but this would be bad as in my real instance these are a lump of allocated memory and the semaphores to protect it for multithreading.
Help?
In general (regardless of whether the function is static or not), a
member function of the derived class can only access protected base
class members of objects of its type. It cannot access protected
members of the base if the static type is not that of the derived class
(or a class derived from it). So:
class Base {
protected:
int var;
} ;
class Derived : public Base {
static void f1( Derived* pDerived )
{
pDerived->var = 2; // legal, access through Derived...
}
static bool Process( Base *pBase )
{
pBase->var = 2 ; // illegal, access not through Derived...
}
} ;
Access specifier applies to the Derived class handle (reference/pointer/object) and not the methods of Derived class itself. Even if the method was not static, you would have ended up with the same error. Because you are not accessing var with the derived handle. Demo.
The correct way is to provide a setter method:
class Base {
protected:
int var ;
public:
void setVar(const int v) { var = v; } // <--- add this method
};
Note: There is one more way out, but I am not sure if it's elegant.
(static_cast<Derived*>(pBase))->var = 2;
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.