Tony Johansson wrote:
[color=blue]
> Hello Experts!
>
> I reading a book called programming with design pattern revealed
> by Tomasz Muldner and here I read something that I don't understand
> completely.
>
> When I have these two lines in main I get this compile error of ambigious
> vector<MBase*> b;
> b.push_back(=new MI);
>
> The reason for this according to the book is the following text.
> "In the array definition for b[] this code attempts to create a new MI and
> upcast
> the adres 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"
>
> As far as I understand I don't use any subobject of D1 or D2 I only use
> an array of pointers of the
> static type MBase that can point to object of class MBase or objects
> derived from this MBase class according to the substitution rule.
>
> I understand this part of the text above
> "In the array definition for b[] this code attempts to create a new MI and
> upcast
> the adres to a MBase*.
>
>
> #include <iostream>
> using namespace std;
> #include <vector>
>
> class MBase
> {
> public:
> virtual char* vf() const = 0;
> virtual ~MBase() {}
> private:
> int number;
> };
>[/color]
try:
class D1 : public virtual MBase[color=blue]
> {
> public:
> char* vf() const {return "D1"; }
> };
>[/color]
and try:
class D2 : public virtual MBase[color=blue]
> {
> public:
> char* vf() const {return "D2";}
> };
>
> class MI : public D1, public D2
> {
> };
>
> include "MBase.h"
> int main()
> {
> vector<MBase*> b;
> b.push_back(=new MI);
> return 0;
> }
>
> Many thnaks
>
> //Tony[/color]
--
http://www.gregerhaga.net