派生类调用父级的方法
Derived class calls parent's method
(注意:此处提供相应的要点。(
我有很多方法是从std::unary_function<K::Point_3, K::FT>
加typedef K::Point_3 Point;
派生的(底层库CGAL需要它(——这个类被称为Function
。
我现在需要向Function_vector
:添加一些派生类的实例(例如:MySphere
(
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Implicit_to_labeling_function_wrapper.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::FT FT;
class Function: public std::unary_function<K::Point_3, K::FT>
{
public:
typedef K::Point_3 Point; // necessary
// I'd rather not define operator() here
virtual K::FT operator()(K::Point_3 p) const {return 0.0;}
};
class MySphere: public Function
{
public:
virtual K::FT operator()(K::Point_3 p) const {return 3.14;}
};
typedef CGAL::Implicit_multi_domain_to_labeling_function_wrapper<Function>
Function_wrapper;
typedef Function_wrapper::Function_vector Function_vector;
int main()
{
MySphere f1;
Function_vector v;
v.push_back(f1);
std::cout << f1(CGAL::ORIGIN) << std::endl; // MySphere::operator()
std::cout << v[0](CGAL::ORIGIN) << std::endl; // Function::operator() :(
return 0;
}
问题:
Function_vector
不接受指针,因此实际抽象的Function
类不能是虚拟的,需要从std::unary_function
实现operator()
。当向Function_vector
添加MySphere
实例时,MySphere
被强制转换为Function
,并且Function
的operator()
被调用,而不是MySphere
的。
如何让v[0]
调用MySphere::operator()
?
由于您将Function
对象放入向量中,因此有对象切片,因此您根本无法。虚拟函数需要指针或引用才能正确使用继承树。
我能给你的唯一建议是你重新考虑你的设计。
作为一种变通方法,可以创建另一个包装器MyFun
并将其输入到Function
àla 中
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Implicit_to_labeling_function_wrapper.h>
#include <memory>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::FT FT;
class MyFun: public std::unary_function<K::Point_3, K::FT>
{
public:
virtual K::FT operator()(K::Point_3 p) const = 0;
};
class MySphere: public MyFun
{
public:
virtual K::FT operator()(K::Point_3 p) const {return 3.14;}
};
class Function: public std::unary_function<K::Point_3, K::FT>
{
public:
typedef K::Point_3 Point;
explicit Function(std::shared_ptr<MyFun> fun):
fun_(fun)
{
}
virtual K::FT operator()(K::Point_3 p) const {
return fun_->operator()(p);
}
private:
std::shared_ptr<MyFun> fun_;
};
typedef CGAL::Implicit_multi_domain_to_labeling_function_wrapper<Function> Function_wrapper;
typedef Function_wrapper::Function_vector Function_vector;
int main()
{
auto f1 = std::make_shared<MySphere>();
Function_vector v;
v.push_back(Function(f1));
std::cout << (*f1)(CGAL::ORIGIN) << std::endl;
std::cout << v[0](CGAL::ORIGIN) << std::endl;
return 0;
}
相关文章:
- 如何强制从重写方法调用重写的方法基方法?
- C++:使用方法调用析构函数的顺序是什么?
- 派生类调用父类的方法,该方法调用重写的虚拟方法调用错误的方法
- 使用 object 中的方法调用带有 std::bind 和 std::function.target 的 C 样式函数
- 指向类方法调用的指针
- 如何使用 SFINAE 在方法调用中有条件地定义变量?
- 是否有可以处理方法调用依赖关系的设计模式?
- 如何缩短C++中的方法调用?
- 从部分专用模板方法调用模板非静态方法
- 有没有办法禁止派生类中的基类方法调用?
- 为什么这C++只在编译器上编码一个不明确的方法调用Microsoft?
- 从父方法调用子方法
- 如何将子方法调用到父方法
- 虚拟函数在哪里使用 vpointer to vtable 来解析方法调用,非虚拟方法存储在哪里以及如何解析它们?
- 从静态方法调用静态函数指针
- 从同一类中的另一个方法调用方法时出错
- 方法调用意外地像 l 值一样起作用
- 无法从派生的一个方法调用基类方法
- 从类方法调用命名空间中名为 Same 的函数时,重载解析失败
- C 多线程JAVA JNI方法调用