Short story:
is it possible to do
class A{};
class B:public virtual A{};
class C:public virtual A,private B{};
i.e. "showing" that C is an A and not a B, but making it actually a B
without adding the virtual (and the corresponding vptrs)?
Long story:
A has several methods.
B adds some more.
Sometimes I want to forbid the use of one of them. C has this purpose.
The program has many B, few Cs. I do not want then to make B a subclass of C.
Yes, this will do exactly what you intend it to do.
But consider another option: inheriting publicly and hiding the unwanted methods:
class A
{
public:
int a() {return 0xaa;}
};
class B: public A
{
public:
int b() {return 0xbb;}
};
class C: public B
{
private:
using B::b; // makes the method called b private
};
...
B().b(); // OK, using method b in class B
C().b(); // error: b is private in class C
C().B::b(); // OK: calling b in base-class (not sure if you want to prevent this)
This will work with both virtual and non-virtual inheritance.
Related
Suppose i have three classes A, B and C. class B inherits from class A and the inheritance is private whereas class C inherits from B and the inheritance is public. Now class A has a protected function which class C wants to access. So, what must be done in class B to make that protected function available to class C.
Here is the link to the code : http://pastebin.com/9E2sLZzj
The "using" keyword makes a member of an inherited class visible, and resolvable, in the scope of its subclass. So, to make the privately-inherited member available to B's subclasses:
class A {
protected:
void foo() {}
};
class B : private A {
protected:
using A::foo;
};
class C : public B {
void bar()
{
foo();
}
};
Okay i got the solution
This code fragment worked after inserting it into Class B.
int get(){
return A::get();
}
Not sure what it does though
Can someone explain why this code doesn't work.
class A
{
public:
A(void){}
virtual ~A(void){}
protected:
A* parent;
};
class B : public A
{
public:
B(void){parent = new B;}
~B(void){delete parent;}
protected:
int a;
};
class C : public B
{
public:
C(void){}
virtual ~C(void){}
void f(void){ ((B*)parent)->a; }
};
How is it possible that C isn't able to access members of B?
If I convert parent to a C* in stead of a B* it works fine. But I don't want users to take any unnecessary risks. Is there a cleaner way access a?
Thanks.
From an object of the C class, you can access protected members of B, but only if they're part of some object of class C (maybe yours, maybe not). In other words, to access a from C, you need a pointer (or a reference) to C. This is what the protected modifier means.
The reason for this is the following. The ((B*)parent) pointer may point to some other subclass of B, completely different from C, and that subclass may have the a member inaccessible.
I have class C and it is inheriting from Class A and Class B.
Is it possible for class A to access Class B function( eg fun1() ) using this inheritance. A and B are both independent class and fun1() is only in class B.
Not strictly through inheritance. Although A and B are parents of C, A and B have no relationship and no way to access eachother.
It is possible through inheritance and polimorphism, then class A have a virtual method that C implements calling the function in B, like this:
class A {
public:
b get_b() { return do_get_b(); }
private:
virtual do_get_b() = 0;
};
class B {
public:
b some_b;
};
class C : public A, public B {
private:
virtual do_get_b() {return some_b;}
}
You could check with dynamic_cast to see if your object of A is really a object of C, and thereby of B
void A::foo()
{
B* pB = dynamic_cast<B*>(this);
if (pB) pB->bar();
}
But if A and B are really independent of one another it is probably better to find a different solution.
Sometimes, it is possible. An example how to achieve this.
That example applies to a diamond hierarchy, which is a bit more complicated than yours. In your terms, ClassA and ClassB should be derived from class Base, ClassC is derived from ClassA and ClassB.
I must admit I'm having trouble formulating this question, but I'll try my best to be precise. I have tried to search for an answer to my question, but I suspect I have been unable to find what I'm looking for, as I'm not exactly sure what to call this.
I have a base class A, and several child classes inheriting from this base class. I then make another class X that inherits from some of the mentioned child classes. The problem I'm now facing is that each of the classes X inherits, have their own instance of class A. The code below should give a better understanding of what I mean.
class A;
class B : public A;
class C : public A;
class X : public B, public C;
Is there a way to make class B and C share the same instance of class A, when they are both acting as indirect base classes for the same class?
To give an example of why I want this, lets look at this code.
class A
{
int _x;
};
class B : public A
{
void outputX(){std::cout << A::_x << std::endl;
};
class C : public A
{
void setX(int x){A::_x=x;}
};
class X : public B, public C
{
C::setX(5);
// this will output an un-initialized _x,
// as B and C have their own version of A
B::outputX()
};
Now I realize this seems rather unnecessary in this example here, but in my real situation I like to think it would be a good solution if B and C shared instance of A in class X.
Is this at all possible?
You can solve this by using virtual inheritance:
class B : virtual public A;
class C : virtual public A;
class X : virtual public B, virtual public C;
See more on the diamond problem.
By 'promote' I mean make access more restrictive and by 'demote' I mean make less restrictive.
For example, when class B is derived from class A using : protected or : private then the public members of A get promoted, to protected and private, respectively.
Could some class C ever come in and derive itself from class B, while at the same time demoting the inherited members of class A back to their original access specifications?
If you're using protected derivation, then class C could indeed give access to the protected members, by creating an appropriate wrapper:
class A {
public:
void F();
};
class B : protected A { };
class C : public B {
public:
using B::F;
};
This also can be made to work with data members:
class A {
public:
int n;
};
class B : protected A { };
class C : public B {
public:
using B::n;
C() : n(this->B::n) { }
};
With private inheritance this is not directly possible, because C cannot itself access members in A.. However, if B is derived from A using private virtual inheritance, it becomes possible again:
class A {
public:
void F();
};
class B : private virtual A { };
class C : public B, public virtual A { };
int main() {
C x;
x.F();
return 0;
}
This works because with virtual inheritance, C can derive directly from the same instance of A as B, but with a different access specifier.
A using declaration can give access to public or protected members of protected base classes:
struct A {int x;};
struct B : protected A {};
struct C : B
{
using A::x; // publicly accessible
}
Obviously, private members and base classes aren't available to C, so you can't reduce the restrictions on them.
Of course not, what would be the point of having private or protected inheritance if I can just create another derived that class that blows away the intermediate class' access restrictions and makes everything public?
No.
Because, when class C derives class B; it doesn't know about the original access specifiers of class A. It just respects the access specifiers in class B.