Hi!
The net is overflowing with explanations of the "dreaded diamond problem".
So is StackOverflow. I think I understand that bit, but I fail to translate that knowledge into comprehending something similar yet different.
My question begins as a pure C++ question, but the answer might well branch over into MS-COM specifics. The genera...
I have a strange problem in my project. I have a class that inherits from a base class (which again inherits from another base class) and overrides a function. However, when that function is called it never calls the overridden function, but the base function.
However, when I override that function in the middle class it is called. But ...
I have hierarchy of public interfaces like this:
struct ISwitchable {
/* Obtain pointer to another implemented interface of the same instance. */
virtual int switch(unsigned int interfaceId, void** pInstance) = 0;
};
struct IFoo : public ISwitchable { /* Methods */ };
struct IBar : public ISwitchable { /* Methods */ };
struct ...
So I have this idea and I think it's basically impossible to implement in C++... but I want to ask. I read through chapter 15 of Stroustrup and didn't get my answer, and I don't think the billion other questions about inheritance diamonds answer this one, so I'm asking here.
The question is, what happens when you inherit from two base c...
class Temp
{
private:
~Temp() {}
friend class Final;
};
class Final : virtual public Temp
{
public:
void fun()
{
cout<<"In base";
}
};
class Derived : public Final
{
};
void main()
{
Derived obj;
obj.fun();
}
The above code tries to achieve non-inheritable class (final). But using above code t...
Hi, i am currently working on a C++ project where i have an abstract interface that is implemented later on. The interface also has a implemented method which my implementation doesn't override.
My problem is that when using my implementation, the compiler(MSVC) doesn't see the interface method. What causes this, and how can i resolve it...
Hi all!
I have a problem with Visual Studio 2008 concerning virtual inheritance.
Consider the following example:
#include<iostream>
class Print {
public:
Print (const char * name) {
std::cout << name << std::endl;
}
};
class Base : public virtual Print {
public:
Base () : Print("Base") {}
};
class A : ...
Problem:
class Base {
public:
Base(Base* pParent);
... implements basic stuff...
};
class A : virtual public Base {
public:
A(A* pParent) : Base(pParent) {}
...
};
class B : virtual public Base {
public:
B(B* pParent) : Base(pParent) {}
...
};
class C : public A, public B {
public:
C(C* pParent) : A(pParent), B(pParent) {} ...
Is there a way to define a class Foo in C++
so that
I can inherit from it
I can't "diamond inherit" from it
I.e.
class Cat: public Foo{} // okay
class Dog: public Foo{} // okay
class Weird: public Cat, public Dog {} // I want this to throw a compiler error
...
In the following code, it seems class C does not have access to A's constructor, which is required because of the virtual inheritance. Yet, the code still compiles and runs. Why does it work?
class A {};
class B: private virtual A {};
class C: public B {};
int main() {
C c;
return 0;
}
Moreover, if I remove the default constr...
Hello,
I'm working on a graphics application that is using virtual classes fairly extensively. It has:
A picture class, which is essentially a collection of shapes.
A shapes class, which is purely virtual and has a few classes that inherit from it:
Circle
Polygon
Rectangle
A Figure shape, which is any graphical figure (also virtual...
class Base
{
public:
Base(){}
Base(int k):a(k)
{
}
int a;
};
class X:virtual public Base
{
public:
X():Base(10){}
int x;
};
class Y:virtual public Base
{
public:
Y():Base(10){}
int y;
};
class Z:public X,public Y...
Hi there:
I encounter this problem when tackling with virtual inheritance. I remember that in a non-virtual inheritance hierarchy, object of sub-class hold an object of its direct super-class. What about virtual inheritance? In this situation, does object of sub-class hold an object of its super-class directly or just hold a pointer poi...
I'm trying to derive a new class from an old one. The base class declaration looks like this:
class Driver : public Plugin, public CmdObject
{
protected:
Driver();
public:
static Driver* GetInstance();
virtual Engine& GetEngine();
public:
// Plugin methods...
virtual bool InitPlugin (Mgr* pMgr);
virtual bool Ope...
I am trying to refactor some code while leaving existing functionality in tact. I'm having trouble casting a pointer to an object into a base interface and then getting the derived class out later. The program uses a factory object to create instances of these objects in certain cases.
Here are some examples of the classes I'm working w...
Consider the following class hierarchy:
base class Object with a virtual method foo()
an arbitrary hierarchy with multiple inheritance (virtual and non-virtual); each class is a subtype of Object; some of them override foo(), some don't
a class X from this hierarchy, not overriding foo()
How to determine which method will be executed...
I am little confused about vptr and representation of objects in the memory, and hope you can help me understand the matter better.
Consider B inherits from A and both define virtual functions f(). From what I learned the representation of an object of class B in the memory looks like this:[ vptr | A | B ]
and the vtbl that vptr points...
Hi all,
I am trying to inherit two equally named methods with different parameter lists to a derived class. One of them is virtual and overridden in the derived class, the other one is non-virtual. Doing so, i get a compile error while trying to access the non-virtual method of the base class from an derived class object.
Here is the ...
I understand the basics of C++ virtual inheritance. However, I'm confused about where exactly I need to use the virtual keyword with a complex class hierarchy. For example, suppose I have the following classes:
A
/ \
B C
/ \ / \
D E F
\ / \ /
G H
\...
I have never seen a class used as virtual and nonvirtual base (i.e. if some class is intended to be an ancestor then we usually know in advance about type of inheritance - virtual or nonvirtual).
So I suppose that there is an error-prone freedom in c++ to specialize "virtual" inheritance in base class list. It should be better to specif...