使用boost::shared_ptr的多态调度
Polymorphic dispatch using boost::shared_ptr
我正试图根据向量中指针的派生类型来调用重载函数。
我有一个基类Fruit,并创建一个shared_ptr向量。然后,我将十位小数的共享指针推送到向量中。到目前为止,一切都很好(问题出现在我添加代码打印元素之后)。
接下来,我想遍历容器,为每个水果调用相应的Print函数。我得到编译错误:
# g++ -I/cygdrive/c/Program Files/boost/boost_1_52_0 -o main.exe main.cpp
main.cpp: In function `int main()':
main.cpp:80: error: no matching function for call to `Print_via_ptr(const boost::shared_ptr<Fruit>&)'
main.cpp:57: note: candidates are: void Print_via_ptr(boost::shared_ptr<Apple>)
main.cpp:58: note: void Print_via_ptr(boost::shared_ptr<Orange>)
main.cpp:59: note: void Print_via_ptr(boost::shared_ptr<Strawberry>)
main.cpp:95: error: no matching function for call to `Print(Fruit&)'
main.cpp:53: note: candidates are: void Print(const Apple&)
main.cpp:54: note: void Print(const Orange&)
main.cpp:55: note: void Print(const Strawberry&)
这是代码:
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include "boost/shared_ptr.hpp"
using std::cout;
using std::cin;
using std::endl;
struct Fruit
{
virtual const std::string& who_am_i(void) const = 0;
};
struct Apple
: public Fruit
{
const std::string& who_am_i(void) const
{
static const std::string name = "Apple";
return name;
}
};
struct Orange
: public Fruit
{
const std::string& who_am_i(void) const
{
static const std::string name = "Orange";
return name;
}
};
struct Strawberry
: public Fruit
{
const std::string& who_am_i(void) const
{
static const std::string name = "Strawberry";
return name;
}
};
void Pause(void);
void Print(const Apple& a);
void Print(const Orange& o);
void Print(const Strawberry& s);
void Print_via_ptr(boost::shared_ptr<Apple> p_a);
void Print_via_ptr(boost::shared_ptr<Orange> p_o);
void Print_via_ptr(boost::shared_ptr<Strawberry> p_s);
int main(void)
{
std::vector<boost::shared_ptr<Fruit> > basket;
boost::shared_ptr<Apple> p_apple(new Apple);
boost::shared_ptr<Orange> p_orange(new Orange);
boost::shared_ptr<Strawberry> p_strawberry(new Strawberry);
// Put fruit into basket.
basket.push_back(p_apple);
basket.push_back(p_orange);
basket.push_back(p_strawberry);
// Display the basket of fruit shared_ptr
cout << "Basket contents:n";
for (std::vector<boost::shared_ptr<Fruit> >::const_iterator iter = basket.begin();
iter != basket.end();
++iter)
{
Print_via_ptr(*iter); // Line 80
}
// Create bowl of fruit pointers
std::vector<Fruit *> bowl;
bowl.push_back(new Apple);
bowl.push_back(new Orange);
bowl.push_back(new Strawberry);
// Print the fruit.
std::vector<Fruit *>::const_iterator bowl_iter;
for (bowl_iter = bowl.begin();
bowl_iter != bowl.end();
++bowl_iter)
{
Print(**bowl_iter); // Line 95
}
Pause();
return EXIT_SUCCESS;
}
void
Pause(void)
{
cout << "Paused. Press Enter to continue.n";
cin.ignore(10000, 'n');
}
void
Print(const Apple& a)
{
cout << a.who_am_i() << endl;
}
void
Print(const Orange& o)
{
cout << o.who_am_i() << endl;
}
void
Print(const Strawberry& s)
{
cout << s.who_am_i() << endl;
}
void
Print_via_ptr(boost::shared_ptr<Apple> p_a)
{
cout << p_a->who_am_i() << endl;
}
void
Print_via_ptr(boost::shared_ptr<Orange> p_o)
{
cout << p_o->who_am_i() << endl;
}
void
Print_via_ptr(boost::shared_ptr<Strawberry> p_s)
{
cout << p_s->who_am_i() << endl;
}
我在WindowsVista上,在Cygwin:下使用g++
# g++ --version
g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
我希望在调用时调用Print_via_ptr(boost::shared_ptr<Apple>)
Print_via_ptr(bowl[0]);
多态调度是通过基指针或引用类型完成的,它不会自动转换为派生指针/引用类型。
std::vector<boost::shared_ptr<Fruit> > basket;
boost::shared_ptr<Apple> p_apple(new Apple);
basket.push_back(p_apple);
调用push_back
时,对象存储为boost::shared_ptr<Fruit>
而不是boost::shared_ptr<Apple>
它们只能由调用
void Print_via_ptr(const boost::shared_ptr<Fruit> p_a);
^^^^^
原始指针函数也有同样的问题
或者您可以使用模板为您生成调用,但这可能不是您的目的。
template<typename T>
void Print_via_ptr(T p_o)
{
std::cout << p_o->who_am_i() << std::endl;
}
相关文章:
- 多态性和功能结合
- 具有默认模板参数的多态类的模板推导失败
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 多态二进制函数
- 访问存储在向量C++中的结构的多态成员
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 将 std::allocate_shared 与多态资源分配器一起使用
- 通过switch和static_cast访问多态对象的运行时类型
- C++boost序列化多态性问题
- 多态杆件变量 - 类设计
- 如何查找哪个类对象位于数组的特定索引上(多态性)
- 如何在多线程中正确使用unique_ptr进行多态性?
- Doees the 'this' 指针参与虚函数的多态行为
- C++ 在堆栈中包含多态属性的类对象存储
- 基类和派生类的多态赋值运算符
- 转身多态对象
- 如何在基类指针向量的元素上应用重载的多态函数
- 具有智能指针的多态性
- c++在没有虚拟析构函数的多态性中共享ptr
- ptr和多态性