Using protected static variable in another file - c++

How can I access a variable that has been declared as static and protected in a class definition for use in another file b.cpp

The only code allowed to use protected class members (static or not) are explicit friends of the class in question and classes derived from the class in question (and of course, members of the class itself). Therefore, if "you" want to access that value, then "you" must either be a friend of that class or a member of a class derived from it.
The protection classes (public, protected, and private) exist to provide protection for data. By declaring the member to be protected, the writer of that class is making a semi-strong statement about what code should be allowed to touch that piece of memory. If you're not a derived class or have been given permission with an explicit friend specification, then you're not allowed to touch it.
You should not derive from a class solely to get access to a protected static member. You should only derive from a class if it makes sense to do so based on what your derived class is trying to do.

Having declared as protected, the static variable can only be accessed with in it's member functions and it's derived classes( public, protected inheritance ).

I faced this once in a class that I can't modify(standard adaptors), and came up with the following solution with the help of SO & Google!
#include <iostream>
class my_type{
protected:
static int var;
};
int my_type::var = 0;
int& get_var(my_type& obj){
class protected_accessor : my_type{
public:
static int& get_var(my_type&){
return my_type::var;
}
};
return protected_accessor::get_var(obj);
}
int main(){
my_type obj;
std::cout << get_var(obj) << std::endl;
get_var(obj) = 1;
std::cout << get_var(obj);
}
I have used a variation of this code in my tiny utility: https://bitbucket.org/AraK/streamer/wiki/Developer_FAQ. Look for "Streaming Standard Adapters".

Related

Is it possible for classes to work together in C++?

I have this large class that I want to separate into different classes. The reason why it was large because the class had many private variables and functions that are needed to run the program. I was tired of scrolling down the 1000+ lines of code trying to add or edit code. I am wondering if it is possible for the classes to interact with one base class that includes all the private/protected variables it needed to operate, or simply have them as global variables.
I am aware of inheritance as I tried having the separate classes be derived from the base class, by doing something similar to this:
#include <iostream>
class Base {
public:
void sayPrivateVar() {
std::cout << privateVar;
}
protected:
std::string privateVar = "I am a protected variable!";
};
class Derived : public Base {
public:
void manip() {
base->privateVar = "That was updated from a derived class";
}
private:
Base* base;
};
int main() {
Base base;
Derived derived;
derived.manip();
base.sayPrivateVar();
return 0;
}
EDIT: Without creating another object inside a class.
it depends on your class and what you have in it. It is often better not have inheritance because derived classes may then get member variables that they don't need. There are a few other ways you can do. One way would be to group you private variables in different classes and then have member variables in the first class.
E.g.
class X {
int x;
int y;
int angle;
...
};
becomes
class XYVector;
class X {
XYVector v;
};
class XYVector
{
int x;
int y;
int angle;
};
You can continue in this direction and instead of making them concrete class like XYVector above have them as interfaces : to have it more elaborate and flexible check out https://en.wikipedia.org/wiki/Composition_over_inheritance
At any rate: avoid having globally declared variables.
This is a good question and the answer is absolutely. Inheritance is actually a very good solution in this particular context since that is how object code shares it's scope with other classes. One important thing to note here is that how you call your derived class is important because the inherited scope is set along with the derived class (i.e. declaring it public base would inherit the public and protected methods as opposed to declaring it private which would give the derived class even more access!)

C++: Creating derived class that has access to private variables of base?

I have an abstract class base with private member variable base_var. I want to create a derived class, which also has base_var as a private member.
To me, this seems like an obvious thing you would want to do. base is abstract so it will never be instantiated. The only time I will create a base-object is if it is actually a derived object, so obviously when I give ´base´ a private member variable, what I am really trying to do is give that variable to all of its derived objects.
However, the below diagram seems to suggest that this is not doable with inheritance?
Why not? What would then even be the point of having private stuff in an abstract class? That class will never be instantiated, so all that private stuff is essentially useless?
However, the below diagram seems to suggest that this is not doable with inheritance?
Correct, private members of a class can not be accessed by derived classes. If You want a member of a class to be accessible by its derived classes but not by the outside, then You have to make it protected.
Why not? What would then even be the point of having private stuff in an abstract class? That class will never be instantiated, so all that private stuff is essentially useless?
Even an abstract class can have member functions which act on a (private) member variable. Consider (somewhat silly example, but well):
class MaxCached
{
private:
int cache = std::numeric_limits<int>::min();
public:
bool put(int value)
{
if (value > cache)
{
cache = value;
return true;
}
return false;
}
int get() const
{
return cache;
}
virtual void someInterface() const = 0;
};
Deriving from this class gives You the functionality of the base class (put and get) without the danger of breaking it (by for example writing a wrong value to cache).
Side note: Above is a purely made up example! You shouldn't add such a cache (which is independent of Your interface) into the abstract base class. As it stands the example breaks with the "Single Responsibility Principle"!
Just because a class is abstract doesn't mean there cannot be code implemented in that class that might access that variable. When you declare an item in a class to be private, the compiler assumes you had a good reason and will not change the access just because it there is a pure virtual function in the class.
If you want your derived classes to have access to a base class member declare the member as protected.
I have an abstract class base with private member variable base_var
class foo {
public:
virtual void a_pure_virtual_method() = 0;
int get_var() { base_var; }
virtual ~foo(){}
private:
int base_var;
};
Note that a class is said to be abstract when it has at least one pure virtual (aka abstract) method. There is nothing that forbids an abstract class to have non-pure virtual or even non-virtual methods.
I want to create a derived class, which also has base_var as a private member.
class derived : public foo {};
To me, this seems like an obvious thing you would want to do.
Sure, no problem so far.
The only time I will create a base-object is if it is actually a derived object, so obviously when I give ´base´ a private member variable, what I am really trying to do is give that variable to all of its derived objects.
Still fine.
Why not?
You are confusing access rights that are display in the image you included with the mere presence of the members in the derived. The derived class has no access to members that are private in the base class. Period. This is just according to the definition of what is private.
What would then even be the point of having private stuff in an abstract class? That class will never be instantiated, so all that private stuff is essentially useless?
It is not useless at all. Derived classes inherit all members, they just cannot access all of them. The private stuff is there you just cannot access it directly. Thats the whole point of encapsulation. Consider this example:
class bar : public foo {
void test() {
std::cout << base_var; // error base_var is private in foo
std::cout << get_var(); // fine
}
};

Hide class members outside namespace

I have a collection of classes that have node like relationships. Due to this, I need to access some of the parents/chilren functions that I'd otherwise like to keep away from the end user since they just clutter the suggested method list, make no sense and break things really bad when used from outside their own "ecosystem".
I figured if they'd all inherit the same base class with virtual placeholder functions then I could access the protected members but apparently not. Is there a way to hide class members from access outside a certain namespace or a container class while being accessible from within?
If you really have to has access to some protected/private data you can make friend classes.
From Wikipedia:
class B {
friend class A; // A is a friend of B
private:
int i;
};
class A {
public:
A(B& b) {
b.i = 0; // legal access due to friendship
}
};

access private members in inheritance

I have a class A, which have a field val declared as private.
I want to declare a class B, that inherit from A and have an access to val.
Is there a way to do it on C++?
I want to do it because I need to overload some functions of A, without changing A code at all.
Thanks.
Quick answer: You don't. Thats what the protected key-word is for, which you want to use if you want to grant access to subclasses but no-one else.
private means that no-one has access to those variables, not even subclasses.
If you cannot change code in A at all, maybe there is a public/protected access method for that variable. Otherwise these variables are not meant to be accessed from subclasses and only hacks can help (which I don't encourage!).
Private members of a base class can only be accessed by base member functions (not derived classes). So you have no rights not even a chance to do so :)
class Base
public: can be accessed by anybody
private: can only be accessed by only base member functions (not
derived classes)
protected: can be accessed by both base member functions and derived
classes
Well, if you have access to base class, you can declare class B as friend class. But as others explained it: because you can, it does not mean it's good idea. Use protected members, if you want derived classes to be able to access them.
It is doable as describe in this Guru of the Week - GotW #76 - Uses and Abuses of Access Rights. But it's should be considered a last resort.
You need to define it as protected. Protected members are inherited to child classes but are not accessible from the outside world.
You can access the private members of a base class say A through an inherited member function of A
#include<iostream>
using namespace std;
class A{
int a;
public:
A(){};
A(int val){a=val;};
int get_a(){//To access a private variable it must be initialized here
a=10;
return a;
}
};
class B: public A{
int b;
public:
B(){};
B(int val){b=val;};
void get(){
cout<<get_a();
}
};
int main(){
A ob1(2);
cout<<ob1.get_a()<<endl;
B ob2(4);
ob2.get();
return 0;
}

Private and Protected Members

I'm having trouble understanding the difference between private and protected members in a C++ class. In simple terms, what is the difference?
protected members are accessible by derived classes. private members are not.
Generally (most of the time) members should either be private or public. It is rare and unusual to need a protected member (edit) in a well-designed system.
EDIT:
Maybe I should elaborate on why protected members can be a code-smell.
If derived classes have access to data members that other classes do not, this could be an indication that the base & derived classes are too tightly coupled. The derived classes have access to the base class' state, and therefore the base class' state is subject to corruption. If this were not the case, then there's also often no reason to just make the data members public.
Others have gone in to greater detail on this.
Here is what Stroustrup says in his text:
Members declared protected are far
more open to abuse than members
declared private . In particular,
declaring data members protected is
usually a design error. Placing
significant amounts of data in a
common class for all derived classes
to use leaves that data open to
corruption. Worse, protected data,
like public data, cannot easily be
restructured because there is no good
way of finding every use. Thus,
protected data becomes a software
maintenance problem.
See also this question.
From the C++ FAQ:
A member (either data member or member function) declared in a private section of a class can only be accessed by member functions and friends of that class
A member (either data member or member function) declared in a protected section of a class can only be accessed by member functions and friends of that class, and by member functions and friends of derived classes
A member (either data member or member function) declared in a public section of a class can be accessed by anyone
Protected members can be accessed by derived classes (and friends).
Private members can only be accessed by the declaring class (or by friends).
Simple example:
class Base
{
protected:
int prot;
private:
int priv;
public:
int Prot() const { return prot; }
int Priv() const { return priv; }
};
class Derived
{
public:
void ShowProt() { cout << prot; } // OK - prot is accessible because it is protected
void ShowPriv() { cout << priv; } // Compile Error - cannot access priv, which is private
void ShowPriv2() { cout << Priv(); } // OK, because Priv() is public
};