Newsgroup: it.comp.lang.c++
Titolo: Virtual

Qualcuno mi spiega cosa comporta dichiarare una classe base come virtuale?
Grazie


Trovi una delle risposte alla tua domanda nel capitolo 6 di "Thinking in C++
vol 2 " di Bruce Eckel.
Il capitolo sulla ereditarieta' multipla ed il paragrafo e' sull'ambiguita'
dell'upcasting.
E' il classico esempio del diamante nell'ereditarieta' multipla, che puo'
causare ambiguita' .
//: C06:MultipleInheritance1.cpp
// MI & ambiguity
#include "../purge.h"
#include <iostream>
#include <vector>
using namespace std;
class MBase
{
public:
    virtual char* vf() const = 0;
    virtual ~MBase() {}
};
class D1 : public MBase
{
public:
    char* vf() const { return "D1"; }
};
class D2 : public MBase
{
public:
    char* vf() const { return "D2"; }
};
// Causes error: ambiguous override of vf():
//! class MI : public D1, public D2 {};
int main()
{
    vector<MBase*> b;
    b.push_back(new D1);
    b.push_back(new D2);
    // Cannot upcast: which subobject?:
    //! b.push_back(new mi);
    for(int i = 0; i < b.size(); i++)
        cout << b[i]->vf() << endl;
    purge(b);
} ///:~
Two problems occur here. First, you cannot even create the class mi because
doing so would
cause a clash between the two definitions of vf( ) in D1 and D2.
Second, in the array definition for b[ ] this code attempts to create a new
mi and upcast the
address to a MBase*. The compiler won't accept this because it has no way of
knowing
whether you want to use D1's subobject MBase or D2's subobject MBase for the
resulting
address.
To solve the first problem, you must explicitly disambiguate the function
vf( ) by writing a
redefinition in the class mi.
The solution to the second problem is a language extension: The meaning of
the virtual
keyword is overloaded. If you inherit a base class as virtual, only one
subobject of that class
will ever appear as a base class. Virtual base classes are implemented by
the compiler with
pointer magic in a way suggesting the implementation of ordinary virtual
functions.
Puoi trovare una risposta anche su "More effective C++" di Scott Meyer Item
24:  Understand the costs of virtual functions, multiple inheritance,
virtual base classes, and RTTI
"
Multiple inheritance often leads to the need for virtual base classes.
Without virtual base classes, if a derived class has more than one
inheritance path to a base class, the data members of that base class are
replicated within each derived class object, one copy for each path between
the derived class and the base class. Such replication is almost never what
programmers want, and making base classes virtual eliminates the
replication. Virtual base classes may incur a cost of their own, however,
because implementations of virtual base classes often use pointers to
virtual base class parts as the means for avoiding the replication, and one
or more of those pointers may be stored inside your objects.
For example, consider this, which I generally call "the dreaded multiple
inheritance diamond:"
class A { ... };
class B : virtual public A { ... };
class C : virtual public A { ... };
class D : public B, public C { ... };
Here A is a virtual base class because B and C virtually inherit from it.
"
Maurus

 Puoi trovare una risposta anche su "More effective C++" di Scott Meyer
Posso anche trovare qualcuno che mi risponda qui? Altrimenti avrei comprato
un libro...
Grazie comunque.

Berserker wrote:


certo anche che non puoi pretendere di imparare il C++ senza nemmeno
comprare un libro. In fondo quello di Bruce Eckel è liberamente
scaricabile da 
http://www.mindview.net.
..
-- 
questo articolo e` stato inviato via web dal servizio gratuito 
http://www.newsland.it/news
 segnala gli abusi ad 
ab...@newsland.it

 wrote in message
news:48hK9.9244$TC5.3...@twister1.libero.it...

Comunque, anche se per voce di altri piu' autorevoli di me, pensavo di
averti risposto.
O hai il problema dell'inglese?
Maurus