
请问这一道C++题如何做?
Asmentionedinclass,clone()doesn’tworkwithcovarianceandvirtualinheritanceunderMicrosof...
As mentioned in class, clone() doesn’t work with covariance and virtual
inheritance under Microsoft Visual C++ due to a compiler bug. The goal
of this problem is to create a workaround. You do not need to have
Visual C++ to do this problem.
We will start from the covariance.cpp program in the same chalk folder
as this lecture
Define classes A, B, C, D that have a diamond inheritance:
Give a A, B, C, and D clone() methods such that they all return the
“right” type without using covariance. In other words, A::clone() should
return an A *, D::clone() should return a D *, etc.
(Note that clone() cannot be virtual because that
is the definition of covariance.)
Running the modified covariance.cpp should print
I’m a B
I’m a D
covariance:
// This fails on MSVC due to a compiler bug. It works
// on all the other standard compilers.
#include <iostream>
#include <string>
using namespace std;
class A {
public:
virtual A *clone() { return new A(*this); }
virtual string identity() { return "I'm an A"; }
};
class B : virtual public A {
public:
virtual B *clone() { return new B(*this); }
virtual string identity() { return "I'm a B"; }
};
class C : virtual public A {
public:
virtual C *clone() { return new C(*this); }
virtual string identity() { return "I'm a C"; }
};
class D : public B, public C {
public:
virtual D *clone() { return new D(*this); }
virtual string identity() { return "I'm a D"; }
};
int main()
{
A *a = new B;
A *a2 = a->clone();
cout << a2->identity() << endl;
D *d = new D;
B *b2 = d->clone();
cout << b2->identity() << endl;
}
======================================
修改上述代码,只改变clone方法,clone方法不能为虚函数。其中主函数的内容不能更改。继承关系不能更改。最后怎么输出:
I’m a B
I’m a D
?
拜托了! 展开
inheritance under Microsoft Visual C++ due to a compiler bug. The goal
of this problem is to create a workaround. You do not need to have
Visual C++ to do this problem.
We will start from the covariance.cpp program in the same chalk folder
as this lecture
Define classes A, B, C, D that have a diamond inheritance:
Give a A, B, C, and D clone() methods such that they all return the
“right” type without using covariance. In other words, A::clone() should
return an A *, D::clone() should return a D *, etc.
(Note that clone() cannot be virtual because that
is the definition of covariance.)
Running the modified covariance.cpp should print
I’m a B
I’m a D
covariance:
// This fails on MSVC due to a compiler bug. It works
// on all the other standard compilers.
#include <iostream>
#include <string>
using namespace std;
class A {
public:
virtual A *clone() { return new A(*this); }
virtual string identity() { return "I'm an A"; }
};
class B : virtual public A {
public:
virtual B *clone() { return new B(*this); }
virtual string identity() { return "I'm a B"; }
};
class C : virtual public A {
public:
virtual C *clone() { return new C(*this); }
virtual string identity() { return "I'm a C"; }
};
class D : public B, public C {
public:
virtual D *clone() { return new D(*this); }
virtual string identity() { return "I'm a D"; }
};
int main()
{
A *a = new B;
A *a2 = a->clone();
cout << a2->identity() << endl;
D *d = new D;
B *b2 = d->clone();
cout << b2->identity() << endl;
}
======================================
修改上述代码,只改变clone方法,clone方法不能为虚函数。其中主函数的内容不能更改。继承关系不能更改。最后怎么输出:
I’m a B
I’m a D
?
拜托了! 展开
2个回答
展开全部
代码无误
[Note that clone() cannot be virtual because that
is the definition of covariance.]-引自line 10
这句话有误,既然是clone()怎么能不是虚函数,不是虚函数我如何让继承类override?
再说对于 covariance function, 只是override的函数返回参数不一样而已,那你就要自己去认真数序override的概念了,概念中说道返回参数不一样是可以实现override的,而如果参数不一样那就是overload了。
这主要要看编译器了,这也不能说别人是一个bug。
[Note that clone() cannot be virtual because that
is the definition of covariance.]-引自line 10
这句话有误,既然是clone()怎么能不是虚函数,不是虚函数我如何让继承类override?
再说对于 covariance function, 只是override的函数返回参数不一样而已,那你就要自己去认真数序override的概念了,概念中说道返回参数不一样是可以实现override的,而如果参数不一样那就是overload了。
这主要要看编译器了,这也不能说别人是一个bug。
更多追问追答
追问
这里面没有我自己的理解,一点也没有。这是老师出的题。不能出现虚函数。不能改写main里面的内容。最后还要输出:I'am B,I’am D。我说的所有的都是题目。我没有发表自己的看法啊。上面的都是题目的描述。
追答
我只能说没有虚函数支持的clone是很奇怪的...
语义上来说, clone函数或是copy函数都作为一个继承类的转换作用。
如果你非要做那只能是用rtti来识别然后static_cast了
我只是反驳你老师的观点而已
展开全部
虚函数是函数名、返回类型、参数个数和类型都相同,因此不应将clone定义为虚函数
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
A *clone() { return new A(*this); }
virtual string identity() { return "I'm an A"; }
};
class B : virtual public A
{
public:
B *clone() { return new B(*this); }
virtual string identity() { return "I'm a B"; }
};
class C : virtual public A
{
public:
C *clone() { return new C(*this); }
virtual string identity() { return "I'm a C"; }
};
class D : public B, public C
{
public:
D *clone() { return new D(*this); }
virtual string identity() { return "I'm a D"; }
};
int main()
{
B *a = new B;
A *a2 = a->clone();
cout << a2->identity() << endl;
D *d = new D;
B *b2 = d->clone();
cout << b2->identity() << endl;
return 0
}
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
A *clone() { return new A(*this); }
virtual string identity() { return "I'm an A"; }
};
class B : virtual public A
{
public:
B *clone() { return new B(*this); }
virtual string identity() { return "I'm a B"; }
};
class C : virtual public A
{
public:
C *clone() { return new C(*this); }
virtual string identity() { return "I'm a C"; }
};
class D : public B, public C
{
public:
D *clone() { return new D(*this); }
virtual string identity() { return "I'm a D"; }
};
int main()
{
B *a = new B;
A *a2 = a->clone();
cout << a2->identity() << endl;
D *d = new D;
B *b2 = d->clone();
cout << b2->identity() << endl;
return 0
}
追问
还是有虚函数,不能出现虚函数。一个也不能有。继承关系还不能变。main函数还不能改。这是题目的要求。拜托了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询