关于virtual继承的问题
#include <iostream>
using namespace std;
class Sofa
{
public:
Sofa(void)
{
cout << "Watching TV.." << endl;
}
void Setweight(int i)
{
weight = i;
cout << "Sofa weight.." << endl;
}
protected:
int weight;
};
class Bed
{
public:
Bed(void)
{
cout << "Sleeping..." << endl;
}
void Setweight(int i)
{
weight = i;
cout << "Bed weight.." << endl;
}
protected:
int weight;
};
class SofaBed:virtual public Sofa,virtual public Bed
{
public:
SofaBed()
{
cout << "Fold out ..." << endl;
}
};
void main(void)
{
SofaBed sb;
sb.Setweight(20);
}
为什么编译的时候提示F:\code\Multi-Inheritance.cpp(95) : error C2385: 'SofaBed::Setweight' is ambiguous
而我把中间代码修改为下面的则正常呢?
/*
class Tool
{
public:
void Setweight(int i)
{
weight = i;
cout << "Sofa weight.." << endl;
}
protected:
int weight;
};
class Bed: virtual public Tool
{
public:
Bed(void)
{
cout << "Sleeping..." << endl;
}
};
class Sofa: virtual public Tool
{
public:
Sofa(void)
{
cout << "Watching TV.." << endl;
}
};
class SofaBed:public Bed,public Sofa
{
public:
SofaBed(void)
{
cout << "Watching TV and Sleeping..." << endl;
}
};
*/