if we don't provide the acces modifiers for base class and we need to manipulate the private data of base class in derived class.
Is there anyway to acces the private data members?
Here's a coding example
class A
{
private :
int a;
};
class B : public class A
{
public :
void displayA() { cout<<a<<endl; }
};
how i can acces the a of base class A in derived class B without acces modifiers.
This is really a design issue. When designing class A, you need to decide whether you want to provide the ability to change the state of a to other classes, including derived classes.
If you want A to provide this ability to all other classes, you need a public interface method that does this.
If you want A to provide this ability only to classes which inherit A, you need a protected interface method.
As things stand, the code in B can't directly access A::a.
The answer (if you really don't want to provide access modifiers) is to change the state of the data member from private to protected
Public: Any part of the program can access this function or member
Protected: Only this class and derived classes can access this function or member
Private: Only this class can access this function or member
i've to extend the functionalities of class A, and i don't have a right to change the code of class A, data members are also private and no acces modifires in A, so is there any legal way to acces the private data memebers of class A in class B.
By default the entire scope of a class is tagged as private until a new modifier is seen by the compiler. You must declare (as protected/public) either the member itself, or a function within the class which edits the member.
Actually... There is a third option which does keep data locked which I've just remembered.
class A{
int val;
friendvoid set(A& arg, int val);
friendint get(A& arg);
};
void set(A& arg, int val){
arg.val = val;
}
int get(A& arg){
return arg.val;
}
int main(){
A a;
set(a, 5);
get(a);
set(a, 10);
get(a);
}
A friend function gives an exception to all modifiers in a class, a friend function has permission to access any part of the class it wishes but the class can still use private members;
Well then I'm afraid that there is no answer, to access a class member there must be a modifier other than private, or a friend declared.
You can also declare friend classes I THINK
1 2 3 4 5 6 7 8 9 10 11 12
class B;
class A{
int val;
friend B;
};
class B : private A{
public:
int get_val(){return A::val};
void set_val(int v){A::val = v};
}
You could also declare A as a struct instead of a class... Since A holds variables only you can define it as a struct:
1 2 3 4 5 6
struct A{
int val;
};
class B: public A{
//whatever you want in B
};
class A
{
private :
int a;
protected:
int getA() const { return a; }
};
class B : A
{
public :
void displayA() const { cout<< getA() <<endl; }
};
or you can simply create a const reference which will avoid the function call. Altho I doubt that it'll be noticable. Perhaps the compiler optimizes the function away. Then again, a const reference may be implemented as a pointer (and thus take up more space).
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class A
{
private :
A() : ref_a(a) { }
int a;
protected:
constint &ref_a;
};
class B : A
{
public :
void displayA() { cout<< ref_a <<endl; }
};
A standard-layout class is a class that:
— has no non-static data members of type non-standard-layout class (or array of such types) or reference,
— has no virtual functions and no virtual base classes,
— has the same access control for all non-static data members,
— has no non-standard-layout base classes,
— either has no non-static data members in the most derived class and at most one base class with
non-static data members, or has no base classes with non-static data members, and
— has no base classes of the same type as the first non-static data member
And what is "first" member variable? Is it the order in which the variables are declared
yes, but only if all members have the same access control (all public, all private, all protected)
Keep in mind that even though you can reinterpret the address, you can't portably access the member at that address when it breaks strict aliasing
EDIT: And what all these odd, restrictive conditions and rules are really telling you is that, while fun2code's solution is an interesting curiosity, it is a really bad idea to actually do it. As fun2code said in his/her post: