在 C++ 中使用 dynamic_cast 转换引用参数时出错
Error when casting a reference parameter with dynamic_cast in C++
我正在学习 Robert C. Martin 的《敏捷软件开发》一书。
在一个关于开闭原则的例子中,我遇到了 dynamic_cast <>.
的问题
例子如下:
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
class Shape {
public:
virtual void Drow() const = 0;
virtual bool Precedes(const Shape & s) const = 0;
bool operator<(const Shape &s){ return Precedes(s);};
};
template<typename T>
class Lessp {
public:
bool operator()(const T i , const T j) {return (*i) < (*j);}
};
void DrowAllShape(vector<Shape*> &vect){
vector<Shape*> list = vect;
sort(list.begin(),
list.end(),
Lessp<Shape*>());
vector<Shape*>::const_iterator i;
for(i = list.begin(); i != list.end() ;i++){
(*i)->Drow();
}
}
class Square : public Shape{
public :
virtual void Drow() const{};
virtual bool Precedes(const Shape& s) const;
};
class Circle : public Shape{
public :
virtual void Drow() const{};
virtual bool Precedes(const Shape& s) const;
};
bool Circle::Precedes(const Shape& s) const {
if (dynamic_cast<Square*>(s)) // ERROR : 'const Shape' is not a pointer
return true;
else
return false;
}
我在方法 Precedes of Circle 中遇到错误
有什么问题吗??
您可以使用 dynamic_cast
来:
- 将指针转换为另一个指针(也需要
const
正确)。
- 将引用投射到另一个引用(也需要
const
正确)。
您不能将其用于:
- 将指针转换为重新引用,或者
- 转换对指针的引用。
因此,
dynamic_cast<Square*>(s)
错了。
您可以使用
if ( dynamic_cast<Square const*>(&s) )
解决编译器错误。
您可以使用 dynamic_cast<Square const&>(s)
(引用),但这需要 try
/catch
块。
try
{
auto x = dynamic_cast<Square const&>(s);
}
catch ( std::bad_cast )
{
return false;
}
// No exception thrown.
return true;
我正在学习 Robert C. Martin 的《敏捷软件开发》一书。 在一个关于开闭原则的例子中,我遇到了 dynamic_cast <>.
的问题例子如下:
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
class Shape {
public:
virtual void Drow() const = 0;
virtual bool Precedes(const Shape & s) const = 0;
bool operator<(const Shape &s){ return Precedes(s);};
};
template<typename T>
class Lessp {
public:
bool operator()(const T i , const T j) {return (*i) < (*j);}
};
void DrowAllShape(vector<Shape*> &vect){
vector<Shape*> list = vect;
sort(list.begin(),
list.end(),
Lessp<Shape*>());
vector<Shape*>::const_iterator i;
for(i = list.begin(); i != list.end() ;i++){
(*i)->Drow();
}
}
class Square : public Shape{
public :
virtual void Drow() const{};
virtual bool Precedes(const Shape& s) const;
};
class Circle : public Shape{
public :
virtual void Drow() const{};
virtual bool Precedes(const Shape& s) const;
};
bool Circle::Precedes(const Shape& s) const {
if (dynamic_cast<Square*>(s)) // ERROR : 'const Shape' is not a pointer
return true;
else
return false;
}
我在方法 Precedes of Circle 中遇到错误 有什么问题吗??
您可以使用 dynamic_cast
来:
- 将指针转换为另一个指针(也需要
const
正确)。 - 将引用投射到另一个引用(也需要
const
正确)。
您不能将其用于:
- 将指针转换为重新引用,或者
- 转换对指针的引用。
因此,
dynamic_cast<Square*>(s)
错了。
您可以使用
if ( dynamic_cast<Square const*>(&s) )
解决编译器错误。
您可以使用 dynamic_cast<Square const&>(s)
(引用),但这需要 try
/catch
块。
try
{
auto x = dynamic_cast<Square const&>(s);
}
catch ( std::bad_cast )
{
return false;
}
// No exception thrown.
return true;