I have something like this sample of code:
class A
{
public:
bool ComputeSomething(double &x, double y);
...
};
class B: public A{...}
class C: public A{...}
class D: public B, public C
{
bool FindSomething(const A& a);
...
}
My problem is that in class D the function FindSomethng does not know if A comes from B or C. Is there a way to block A in C so that the methods in C from A to be used just in C and in D to use the methods of A from B? I know this is kind od a stupid question. I need to make this to work just by modifying class C because class B and D are to developed to support such changings...
I suggest that you read this FAQ on virtual/multiple inheritance.
In C++, if you have multiple inheritance, either it is non-virtual in which case you get multiple copies of common base classes, or it is virtual in which case there is just one copy of common base classes.
In most cases it is simply better to avoid multiple inheritance.
Related
Hi I know the title is a little hard to understand, and that's just because I have no idea how to phrase this problem. Fortunately, I can provide an easy-to-understand example of my problem. Imagine a base class A derived class B and unrelated class C setup as follows:
class A
{
public:
};
class B : public A
{
public:
C c;
};
class C
{
public:
void foo();
};
I want to know how to call foo() using an object of class B without doing this:
B b;
b.c.foo();
but rather this:
B b;
b.foo();
Additionally, I don't want to inherit from class C or make copies of class C's functions. Is this possible with a simple implementation? Thanks!
Your constraints mean your example does not make sense unless the function foo is static; if it is not then you need to provide information about the instance of C you are using before foo makes sense; so mentioning c would be essential. If it is static then C::foo works but not b.foo(). If you want a member function for B that calls b.c.foo() then you should write one as suggested above.
Perhaps you meant to privately inherit C in B:
class B : private C , public A
{
public:
using C::foo;
};
will do the trick.
Maybe this is a bit of a beginner's question but I need some help regarding this issue.
Assume there is a class A that inherits from class B that itself inherits from class C.
Now when class A constructor looks like this everything is fine:
A::A(params)
:B(params)
{
}
Next I tried this but it fails:
A::A(params)
:C(params)
{
}
Why can't I ignore inheritance from B here - or is there a way to make this possible? Defining A as follows does not work, here compiler complains C is already a base class of A:
class A : B, C
Why can't I ignore inheritacne from B here - or is there a way to make this possible?
Because classes can only decide how to construct their direct subobject. The direct subobject for A is B and for B is C. The B object needs to be constructed too, you can't bypass it.
The error you got, basically complains about this.
Defining A as follows does not work, here compiler complains C is already a base class of A: class A : B,C
With:
class A : B, C
you are actually declaring a private multiple inheritance. And since you said B has already a subobject (inherits from) C, the compiler is complaining that inheriting from C in A is useless.
But please remember to always specify the private/protected/public kind of inheritance so that you avoid confusion. By default the inheritance for classes is private and for structs is public. So the above code corresponds to:
class A : private B, private C
You should write:
class C
{
public: C(ParamType param) { ... }
};
class B : public C
{
public: B(ParamType param) : C(param) { ... }
};
class A : public B
{
public: A(ParamType param) : B(param) { ... }
};
There is really no other way....
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 have class A. And I have class B. And I have many-many classes derived from class B.
I want to achieve this: derivatives of B should have access to the protected variables of A. Whithout each of them containing an instance of A, which would need a lot of memory.
So I guess public inheritance is not a good idea this time. How do I solve this?
Thanks!
You could do it with friend and accessor functions. This does trust B to stay off A's privates - don't see a good way to let only B and subclasses access only protected members of A unless there's an inheritance relationship between A and B.
class A {
friend class B;
protected:
int X;
};
class B {
protected:
static int getX(A const & a) { return a.X; }
};
class C : public B {
public:
void foo(A const & a) { int bar = getX(a); }
};
Make the classes that are derived from B friends of A.
From what I understand children of B are unrelated to class A and as such should not have access to non-public parts of A.
The right way to get access to A's data within B child classes is through A's public interface. If such public interface isn't adequate then that's a signal that either you're trying to do something that's a poor design, or that A's public interface needs to be improved.
Hello :) i would like to ask, if it's posible to make something like this:
i have base class (parent) A and three other classes (childs) B C D
in class A, i have virtual functions, that's ok.
but what if i need a virtual class ?
class A
{
public:
virtual int func1()=0;
virtual int func2()=0;
virtual class AB; // !!!!???
};
class B
{
public:
int func1();
int func2();
class AB
{
public:
....
};
};
classes B C D are same as class B. Now, i would like to create class instance and it should automaticly "redirect" class to instance of B or C D etc like functions.
is it possible ? i hope, you understand :) Thank you very much for answer.
This is fundamentally impossible. A virtual function call is determined at runtime. A class changes the behaviour of the program at compile-time. You can't make a compile-time determination at runtime unless runtime and compiletime are the same time, i.e. using a JIT or other dynamic code generators. In standard C++, this is impossible.
What you CAN do is have a base class AB, with a virtual function that creates a class that is guaranteed to inherit from this base class, and then return a pointer to that.