我需要一个不同功能的开关来规避频繁的if方向.它应该类似于以下代码,封装在一个类中:
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class T>
class MaxSwitch
{
public:
typedef T (MaxSwitch::*Max_P)(T,T);
Max_P fooC_P;
MaxSwitch(int Q) {
if (Q==0)fooC_P=&MaxSwitch::MaxVal1;
else fooC_P=&MaxSwitch::MaxVal2;
}
inline T MaxVal1(T kx,T MAX)
{
return kx+1;
}
inline T MaxVal2(T kx,T MAX)
{
return MAX;
}
};
int main( int argc, char ** argv )
{
int Q=atoi ( argv[1] );
MaxSwitch<int> MAXSW(Q);
int MAX=5;
for ( int kx=0;kx<MAX;kx++ ) for ( int ky=0;ky<(MAXSW.fooC_P)(kx,MAX);ky++ )
{
cout<<"(kx="<<kx<<", ky="<<ky<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我现在有一个小问题,函数调用(MAXSW.fooC_P)(ky,MAX)是错误的.怎么做得好?
我试图使用一个线程库,它具有如下定义的线程启动函数:
int vg_thread_create(VGThreadFunction func,
void *arg,
VGThreadDescriptor *tid,
void *stack,
size_t stack_size,
long priority,
int flags );
Run Code Online (Sandbox Code Playgroud)
VGThreadFunction是以下typedef:
typedef void* (*VGThreadFunction) ( void* )
Run Code Online (Sandbox Code Playgroud)
为了论证,我有一个这样的函数:
void doit() {
cout << "Woohoo printing stuff in new thread\n";
}
Run Code Online (Sandbox Code Playgroud)
这个函数的签名不是void*func(void*) - 我是否被迫使用这样的函数签名?
如果我尝试:
VGThreadFunction testfunc = &doit;
Run Code Online (Sandbox Code Playgroud)
我明白了
错误C2440:'初始化':无法从'void(__ cdecl*)(void)'转换为'VGThreadFunction'
我如何使用vg_thread_create?
编辑
我试过这个:
void* doit(void* donothingvar) {
cout << "printing stuff in new thread\n";
return donothingvar;
}
Run Code Online (Sandbox Code Playgroud)
然后在一个函数中:
VGThreadFunction testfunc = (VGThreadFunction)doit;
int ret = vg_thread_create(testfunc, 0, 0, 0, 0, 0, 0); …Run Code Online (Sandbox Code Playgroud) 我不确定这在C++中是否可行.我知道你可以将指向函数或静态成员函数的指针作为参数传递.我想要一个特定对象的函数指针,这样当函数执行时,它就在对象上完成.
class MyClass
{
public:
MyClass(int id){mId = id;}
void execute(){cout<<mId<<endl;}
private:
int mId;
};
MyClass obj1(1);
MyClass obj2(2);
typedef (Executor)();
Executor ex1 = &obj1::execute();
Executor ex2 = &obj2::execute();
Run Code Online (Sandbox Code Playgroud)
因此,当执行ex1时,应打印"1",如果执行ex2,则打印"2".这可能吗?
c++ pointers member-function-pointers function-pointers functor
所以我的程序开头有一个头文件和2个.c文件.我去编译,我得到错误信息(大量的这些反复)
command_parser.c:74:6: error: static declaration of ‘read_args_file’ follows non-static declaration
command_parser.h:9:6: note: previous declaration of ‘read_args_file’ was here
Run Code Online (Sandbox Code Playgroud)
现在我不在我的程序中使用静态关键字ANYWHERE ...那么为什么GCC会认为我已经声明了一个静态函数?
下面是.h和.c文件中read_args_file声明的相关代码:
void read_args_file(char* file_name, char* out_file_name, int (*command_read)(char* command, FILE* out));
void read_args_file(char* file_name, char* out_file_name, int (*command_read)(char* command, FILE* out)) {
.....
}
Run Code Online (Sandbox Code Playgroud)
编辑:
整个.h文件是:
#ifndef COMMAND_PARSER_H_
#define COMMAND_PARSER_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* line 8 follows: */
void switch_parsing(int argc, char* argv[], int (*command_read)(char* command, FILE* out), char* (*pr int_usage)()) {
void read_args_file(char* file_name, char* out_file_name, …Run Code Online (Sandbox Code Playgroud) 有什么区别
typedef double F(double)
Run Code Online (Sandbox Code Playgroud)
和
typdedef double (*FPT)(double);
Run Code Online (Sandbox Code Playgroud)
?
在我看来,我可以将两者作为参数传递给函数,即
bar1(FPT f);
bar2(F f);
Run Code Online (Sandbox Code Playgroud)
但是我能做到
FPT f = &foo;
Run Code Online (Sandbox Code Playgroud)
我不能做
F f = foo;
Run Code Online (Sandbox Code Playgroud)
即我不能创建F类型的变量?
我试图在C++中将函数作为参数传递.我一直收到LNK2019未解决的外部符号错误.
我已经读过这些,并且已经修复了这些,但我无法弄清楚我在这里做错了什么.
我的显示函数中的TreeItemType引用似乎发生了错误.
include "PhoneBook.h"
include "PhoneEntry.h"
include "bst.h"
using namespace std;
using namespace p04NS;
void display(TreeItemType& item);
int main() {
PhoneEntry test = PhoneEntry("first", "last", "618-580-0680");
bst * tree = new bst();
TreeItemType foo = TreeItemType(); // test type reference, no error
tree->insert(test);
tree->inorder(display);
system("pause");
return 0;
}
void display(TreeItemType& item){
cout << "hello worlds!";
}
Run Code Online (Sandbox Code Playgroud)
TreeItemType typedef在此处定义.
namespace p04NS {
typedef PhoneEntry TreeItemType;
typedef string KeyType;
// Pointer to a function that can be used to …Run Code Online (Sandbox Code Playgroud) 今天我了解到函数指针和数据指针不一样,因此彼此不兼容(为什么函数指针和数据指针在C/C++中不兼容?).然而,我的问题是,不同的函数(非成员)指针是否相互兼容(以相同的方式实现).
在代码中:
typedef void(*FuncPtr0)();
typedef void(*FuncPtr1)(int);
FuncPtr0 p0;
FuncPtr1 p1;
p0 = reinterpret_cast<FuncPtr0>(p1); // will this always work, if p1 really
p0(); // points to a function of type FuncPtr0
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
我有一个类,其成员函数之一实际上是一个函数指针.这样,用户可以覆盖此功能的功能.遗憾的是,我在运行此功能时遇到了一些困难 我用g ++编译.
以下是代码:
#include <iostream>
using namespace std;
double fcn_mod(const double &phit){
return 1.2+phit;
}
class Object{
public:
Object() {
fcn_ptr = (double (*)(const double &)) &Object::fcn_default;
}
double (*fcn_ptr)(const double &) = NULL;
private:
double fcn_default(const double &phit){
return 3.2+phit;
}
};
int main(){
Object test1,test2;
cout << (test1.*fcn_ptr)(2) << endl; // can not compile here
test2.fcn_ptr = &fcn_mod;
cout << (test2.*fcn_ptr)(2) << endl; // and here
cout << "test" << endl;
}
Run Code Online (Sandbox Code Playgroud)
错误消息是:
erreur: ‘fcn_ptr’ was not …Run Code Online (Sandbox Code Playgroud) 我需要在另一个类中使用一个类中定义的函数.我尝试将函数作为指针传递,而不是重写整个函数,如下所示:
class C {
public:
int get(int x) { return x*x; }
};
struct S {
int (*func) (int x);
};
int main() {
C c;
S s;
cout << c.get(3) << endl;
s.func = &C::get;
cout << s.func(3) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,并给出以下错误:
func_ptr.cpp: In function ‘int main()’:
func_ptr.cpp:42:18: error: cannot convert ‘int (C::*)(int)’ to ‘int (*)(int)’ in assignment
Run Code Online (Sandbox Code Playgroud)
是否可以做这样的事情,如果是这样,我该如何解决?而且,如果可能的话,我可以使用来自对象实例而不是类的指针吗?也就是说,可能使用在特定类实例中定义的变量.谢谢.
我正在玩C中的函数指针只是为了学习.我尝试调用void函数并将其结果设置为int.
void function(int x, int y){
printf("%d,%d\n",x,y);
}
int main(){
int (*fptr)(int,int);
fptr = function;
int a = fptr(2,3);
printf("%d\n",a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
2,3
4
稍微玩了一下之后,我意识到main是在function()中打印printf语句中的字符数.为什么会这样?这是预期的产出吗?