- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
#include <iostream>
template <typename Type, typename ReturnType>
struct mem_fun_ptr_t
{
typedef ReturnType (Type::*Func)();
Func func;
public:
mem_fun_ptr_t(Func f):
func(f) {}
ReturnType operator () (Type *p) { return (p->*func)(); }
};
// non-const version
template <typename T, typename R>
mem_fun_ptr_t<T, R> mem_fun_ptr(R (T::*Func)())
{
return mem_fun_ptr_t<T, R>(Func);
}
// const version
template <typename T, typename R>
mem_fun_ptr_t<T, R> mem_fun_ptr(R (T::*Func)() const)
{
typedef R (T::*f)();
f x = const_cast<f>(Func); //error
return mem_fun_ptr_t<T, R>(x);
//but this works:
/*
f x = reinterpret_cast<f>(Func);
return mem_fun_ptr_t<T, R>(x);
*/
}
int main()
{
std::string str = "Hello";
auto x = mem_fun_ptr(&std::string::length);
std::cout << x(&str);
return 0;
}
我想你已经猜到我在写什么了。是的,我应该用 Func const func 实现 mem_fun_ptr_t<>;属性。这将是正确的解决方案。但我正在学习,我想知道所有。那么如何const_cast成员函数指针呢?我试过了 f x = const_cast<f*>(Func)
但我收到错误。
感谢您的反馈
最佳答案
同时将成员函数指针的类型传递给您的模板:( Live at ideone.com ):
template <typename Type, typename ReturnType, typename MemFuncType>
struct mem_fun_ptr_t
{
MemFuncType func;
public:
mem_fun_ptr_t(MemFuncType f) :
func(f) {}
ReturnType operator () (Type *p) const { return (p->*func)(); }
};
// non-const version
template <typename T, typename R>
mem_fun_ptr_t<T, R, R (T::*)()>
mem_fun_ptr(R (T::*Func)())
{
return mem_fun_ptr_t<T, R, R (T::*)()>(Func);
}
// const version
template <typename T, typename R>
mem_fun_ptr_t<const T, R, R (T::*)() const>
mem_fun_ptr(R (T::*Func)() const)
{
return mem_fun_ptr_t<const T, R, R (T::*)() const>(Func);
}
关于C++。如何将 const_cast 指针指向成员函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17626102/
我正在阅读 C++ Primer,我发现了一些非常奇怪且难以理解的东西: Record lookup(Account&); //Record and Account are two unrelate
我的问题是,为什么代码的第一部分不起作用而第二部分有效。非常量指针应该修改之前使用 const_cast 的 const 值,但是对于整数这个技巧不起作用。您能解释一下为什么会这样吗? const i
我已经阅读了很多关于 C++ 中的 const_cast 被认为是错误和危险的讨论,除了向后兼容 C 代码之外,不应将其用于任何其他用途。我大体上同意。 但是最近我遇到了以下用例,这让我感到好奇。 我
C++ Primer 一书的第 6.4 章陈述如下: In § 4.11.3 (p. 163) we noted that const_casts are more useful in the con
我看到这个 post这解释了const_cast<>并说使用指针/引用是有益的。但是,请考虑以下代码: 1- const_cast(xadj) 我得到 invalid const_cast from
我正在阅读有关 c++ 中的 const_cast 运算符 1.我无法理解的第一件奇怪的事情是 const_cast 运算符语法即 -const_cast----(--expression--)---
这个问题在这里已经有了答案: const_cast doesn't work c++? [duplicate] (3 个答案) 关闭 6 年前。 在const_cast之后,main函数中的值没有变
这个问题在这里已经有了答案: Two different values at the same memory address (7 个答案) 关闭 5 年前。 我有以下代码: int main(){
这个问题在这里已经有了答案: Two different values at the same memory address (7 个答案) 关闭 5 年前。 #include using nam
我想使用 C++ 格式进行此转换,它以 C 方式工作。但是当我尝试使用 C++ 格式时它失败了。 有效! void req_password(const void *data, size_t data
我有一个管理输入的类。要显示和更改键绑定(bind),重要的是在完成后将其提交给管理器之前,为调用者提供它可以拥有和更改的绑定(bind)的映射。但是,这个映射中可以插入/删除什么的具体规则只有管理者
我对 const_cast 的返回类型有点困惑?尖括号内的类型是否 <>是返回类型吗? const int i = 5; int b = const_cast(i); 是const_cast返回 in
我有一个函数 static bool Validate(const char& letter, string& aWord) 我需要调用它 Validate(letter, aWord); // wh
这个问题在这里已经有了答案: Two different values at the same memory address (7 个答案) 关闭 5 年前。 考虑以下代码: 我声明了一个新的引用端
我有一个大型库,它实现了一些不可变的数据结构。可以想象,其中几乎所有内容都是 const合格的。有一些选择部分不是 const,例如引用计数器。为了处理嵌入结构中的引用计数器,这些引用计数器只能通过
长话短说是否适用于: mapm; m.insert( make_pair( 1, 40 ) ); for( map::iterator it = m.begin(); it !
据我所知,在类中创建常量函数对于读/写编译器优化很有用。 类中的常量函数意味着类成员在函数执行期间将保持不变。但是,您可以通过 const 强制转换隐式参数来绕过此问题(当然,这是一种非常糟糕的做法)
这个问题在这里已经有了答案: Two different values at the same memory address (7 个答案) 关闭 5 年前。 考虑以下代码: 我声明了一个新的引用端
考虑以下 C++03 程序: #include struct T { mutable int x; T() : x(0) {} }; void bar(int& x) { x
我正在查看我即将开始使用的 API 的一些示例代码。以下模式让我有点困惑: char* str; str = const_cast("Hello World"); printf("%s ", str)
我是一名优秀的程序员,十分优秀!