在我一直致力于的任务中,我一直在反对这个问题,而且似乎无法让它完全发挥作用.我写了一个小测试类来演示我正在尝试做什么,希望有人可以解释我需要做什么.
//Tester class
#include <iostream>
using namespace std;
template <typename T>
class Tester
{
typedef void (Tester<T>::*FcnPtr)(T);
private:
T data;
void displayThrice(T);
void doFcn( FcnPtr fcn );
public:
Tester( T item = 3 );
void function();
};
template <typename T>
inline Tester<T>::Tester( T item )
: data(item)
{}
template <typename T>
inline void Tester<T>::doFcn( FcnPtr fcn )
{
//fcn should be a pointer to displayThrice, which is then called with the class data
fcn( this->data );
}
template <typename T> …Run Code Online (Sandbox Code Playgroud) 我看到一些类似的问题,但没有简单的答案.在我真正使用它们之前,我只是在玩NSMutableArray来感受它们.出于某种原因,当我尝试在数组上调用count时,它给了我一个EXC_BAD_ACCESS错误,我无法找出原因.
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
// Create window and make key
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[_window makeKeyAndVisible];
NSMutableArray* test = [[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:@"first!"], [NSString stringWithFormat:@"second!"], nil];
[test insertObject:[NSString stringWithFormat:@"inserted"] atIndex:0];
NSLog(@"%@", [test objectAtIndex:0]);
NSLog(@"%@", [test objectAtIndex:1]);
NSLog(@"%@", [test objectAtIndex:2]);
NSLog(@"%@", [test count]); //bad access here
}
Run Code Online (Sandbox Code Playgroud)
所有插入和访问除了计数方法工作都很好.我不明白为什么这不起作用,非常感谢一些帮助.谢谢!