我在C ++中对多态类练习static_cast和dynamic_cast。我使用原始指针和unique_ptr尝试了它。虽然前者不会产生问题,但后者却会产生问题。在这里,我提出了我的代码: -
#include <iostream>
#include <memory>
#include <exception>
#include <stdexcept>
using namespace std;
class A
{
int a, id=0;
static int i;
public:
A()
{
id=++i;
cout<<"constructing A: "<<id<<"\n";
}
virtual void get()
{
cout<<"enter a: ";
cin>>a;
}
virtual void disp()
{
cout<<"a = "<<a<<"\n";
}
virtual ~A()
{
cout<<"destroying A: "<<id<<"\n";
}
};
int A::i=0;
class B: public A
{
int b;
public:
B()
{
cout<<"constructing B\n";
}
void get()
{
cout<<"enter b: ";
cin>>b;
}
void disp()
{
cout<<"b = "<<b<<"\n";
}
~B()
{
cout<<"destroying B\n";
}
};
void show (unique_ptr<B> &p)
{
p->get();
p->disp();
}
void d_cast (unique_ptr<A> &pa)
{
unique_ptr<B> pb;
try
{
pb.reset(dynamic_cast<B*>(pa.release()));
if (pb==nullptr)
throw runtime_error {"nullptr exception"};
show(pb);
cout<<"dynamic_cast successful\n\n";
}
catch (exception &e)
{
cout<<"dynamic_cast unsuccessful: "<<e.what()<<"\n\n";
}
pa.reset(pb.release());
}
void s_cast (unique_ptr<A> &pa)
{
unique_ptr<B> pb;
try
{
pb.reset(static_cast<B*>(pa.release()));
if (pb==nullptr)
throw runtime_error {"nullptr exception"};
show(pb);
cout<<"static_cast successful\n\n";
}
catch (exception &e)
{
cout<<"static_cast unsuccessful: "<<e.what()<<"\n\n";
}
pa.reset(pb.release());
}
int main()
{
cout<<R"(using "unique_ptr<A> pa with new A" :-)"<<"\n\n";
unique_ptr<A> pa(new A); // (1)
d_cast(pa);
s_cast(pa); // (2)
cout<<"\n"<<R"(using "unique_ptr<A> pa with new B" :-)"<<"\n\n";
pa.reset(new B);
d_cast(pa);
s_cast(pa);
return 0;
}
代码的输出是: -
using "unique_ptr<A> pa with new A" :-
constructing A: 1
dynamic_cast unsuccessful: nullptr exception
static_cast unsuccessful: nullptr exception
using "unique_ptr<A> pa with new B" :-
constructing A: 2
constructing B
enter b: 7
b = 7
dynamic_cast successful
enter b: 8
b = 8
static_cast successful
destroying B
destroying A: 2
我只有2个问题,因为我已经标记了: -
为什么第一个对象{表示为(1)}被破坏,而一个对象用#34;新B&#34;被毁了?
为什么(2)抛出异常?有趣的是,如果我反转s_cast(pa)
和d_cast(pa)
的定位,那么(2)不会抛出任何异常并且工作正常(问题(1)仍然存在)。
答案 0 :(得分:1)
好的!因此,您需要更改函数d_cast
函数定义,如下所示: -
void d_cast (unique_ptr<A> &pa)
{
unique_ptr<B> pb;
A *aptr=pa.release(); // make a pointer of type A
try
{
pb.reset(dynamic_cast<B*>(aptr)); // assign aptr instead of pa.release() here
if (pb==nullptr)
throw runtime_error {"nullptr exception"};
show(pb);
cout<<"dynamic_cast successful\n\n";
pa.reset(pb.release()); // reset pa with pb.release() and not with aptr becomes pb has the ownership of aptr
}
catch (exception &e)
{
cout<<"dynamic_cast unsuccessful: "<<e.what()<<"\n\n";
pa.reset(aptr); // reset aptr back to pa as pb holds no ownership of aptr
}
}
因为您必须知道d_cast
会失败,因此dynamic_cast<B*>(pointer_of_type_A)
的表达将返回nullptr
。如果存在引用而不是指针,那么将抛出std::bad_cast
异常。但是因为你正在使用release()
函数,所以unique_ptr对象pa
摆脱了指针的所有权,并且没有任何对象或指针可以追溯它。因此,如果转换失败,您应该使用A *aptr
来保存已发布的指针并将其返回pa
。
如果你这样做,你的两个问题都解决了