分配时的成员和静态函数 [英] Member and static functions while assignment
问题描述
我有以下问题。
等级基础
{
公开:
void(* pfunc)(int)= 0;
};
class派生:public Base
{
public:
void(* pfunc)(int a)
{
// Stuff
}
};
class Foo
{
private:
void(* pf)(int);
public:
Foo(Base * ptr)
{
pf = ptr-> pfunc; //错误,因为pfunc是一个成员函数,
不是静态
}
};
有没有办法做类似pf = ptr-> pfunc的事情?
Alex Vinokur
电子邮件:alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
I have the following problem.
class Base
{
public:
void (*pfunc) (int) = 0;
};
class Derived : public Base
{
public:
void (*pfunc) (int a)
{
// Stuff
}
};
class Foo
{
private:
void (*pf) (int);
public:
Foo (Base* ptr)
{
pf = ptr->pfunc; // Error, because pfunc is a member function,
not static
}
};
Is there any way to do something like "pf = ptr->pfunc"?
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
推荐答案
Alex Vinokur写道:
Alex Vinokur wrote:
我有以下问题。
>班级基地
{
公开:
void(* pfunc)(int)= 0;
这行不会编译。它既不是静态积分常数也不是纯粹的虚函数,但你似乎混合了
二的语法。
};
类派生:公共基地
{
公开:
void(* pfunc)(int a)
你不能为指向函数的指针定义一个体。有关如何发布代码,请参阅常见问题解答
:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
{
//东西
}
};
class Foo
{
私人:
void(* pf)(int);
public:
Foo(Base * ptr)
{/ pf = ptr-> pfunc; //错误,因为pfunc是一个成员函数,
不是静态
你有比这更大的问题!
}
};
有没有办法做类似pf = ptr-> pfunc的事情?
I have the following problem.
class Base
{
public:
void (*pfunc) (int) = 0;
This line won''t compile. It''s neither a static integral constant nor a
pure virtual function, but you seem to have mixed the syntax for the
two.
};
class Derived : public Base
{
public:
void (*pfunc) (int a)
You can''t define a body for a pointer to a function. Please see the FAQ
for this group on how to post code:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
{
// Stuff
}
};
class Foo
{
private:
void (*pf) (int);
public:
Foo (Base* ptr)
{
pf = ptr->pfunc; // Error, because pfunc is a member function,
not static
You have a lot bigger problems than that!
}
};
Is there any way to do something like "pf = ptr->pfunc"?
是的。请参阅常见问题解答:
http://www.parashift.com/c++-faq-lit...o-members.html
干杯! --M
Yes. See the FAQs:
http://www.parashift.com/c++-faq-lit...o-members.html
Cheers! --M
Alex Vinokur写道:
Alex Vinokur wrote:
我有以下问题。
类基地
{
公开:
void(* pfunc)(int)= 0;
指向函数的指针,该函数采用int并返回void。
};
类派生:public Base
{
公开:
void(* pfunc)(int a)
嗯?另一个指向函数的指针...
{
// Stuff
你是否在声明指向函数的指针后尝试定义一个正文?
}
};
class Foo
{
私人:
void(* pf)(int);
好的,指向函数的指针。
public:
Foo(Base * ptr)
{
pf = ptr-> pfunc; //错误,因为pfunc是一个成员函数,
不是静态的
}
};
有没有办法做类似pf = ptr- > pfunc"?
I have the following problem.
class Base
{
public:
void (*pfunc) (int) = 0;
A pointer to a function taking an int and returning void.
};
class Derived : public Base
{
public:
void (*pfunc) (int a)
Huh? Another pointer to a function...
{
// Stuff
Are you trying to define a body after declaring a pointer to a function?
}
};
class Foo
{
private:
void (*pf) (int);
OK, a pointer to a function.
public:
Foo (Base* ptr)
{
pf = ptr->pfunc; // Error, because pfunc is a member function,
not static
}
};
Is there any way to do something like "pf = ptr->pfunc"?
你想在这里完成什么?也许阅读模板
''std :: binder_1st'',你可以做类似
pf = std :: bind1st(& Base: :pfunc,ptr);
虽然有点令人费解,但应该得到你想要的东西,IIUYC。
下次试试显示你打算用''pf''做什么。
V
What are you trying to accomplish here? Perhaps read about the template
''std::binder_1st'', you can do something like
pf = std::bind1st(&Base::pfunc, ptr);
although it is a bit convoluted, but should get you what you want, IIUYC.
Next time try to show what you intend to do with ''pf''.
V
" mlimber" <毫升***** @ gmail.com>在消息中写道
news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Alex Vinokur写道:
Alex Vinokur wrote:
我有以下问题。
班级基地
{
公开:
无效(* pfunc)(int)= 0;
I have the following problem.
class Base
{
public:
void (*pfunc) (int) = 0;
这行不会编译。它既不是静态整数常量也不是纯虚函数,但你似乎混合了
两种语法。
This line won''t compile. It''s neither a static integral constant nor a
pure virtual function, but you seem to have mixed the syntax for the
two.
[snip]
我的第一个消息是不走运。请忽略它。
这是有问题的程序。
====== foo1.cpp ======
struct Base
{
virtual void func(int)= 0;
};
struct派生:公共基地
{
void func(int)
{
// Stuff < br $>
}
};
struct Foo
{
void (* pfunc)(int);
基数* p_;
Foo(基数* p):p_(p){}
void doit()
{
//这是我要达到的目的
pfunc = p _-> func;
// ------------------------
// g ++ 4.0.1产生以下错误信息:
// ........................
//错误:类型''void(Base ::)(int)的参数''
//与''无效(*)(int)< br $>
// ........................
//因为func()是_member_(功能
// -------------- ----------
}
};
int main()
{
Base * p = new Derived;
Foo foo(p);
删除p;
返回0;
}
======================
这是问题的_partial_解决方案。
====== foo2.cpp ======
struct Base
{
virtual void func(int)= 0;
};
struct派生:public Base
{
void func(int)
{
// Stuff
}
};
struct Foo
{
void(* pfunc)(int);
static Base * p_s;
Foo(Base * p){p_s = p; }
静态void包装器(int a)
{
p_s-> func(a);
}
void doit()
{
pfunc = wrapper;
}
};
基础* Foo :: p_s = 0;
int main()
{
基数* p =新衍生;
Foo foo(p);
删除p;
返回0;
}
====================== >
-
Alex Vinokur
电子邮件:alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
[snip]
My first message was unlucky. Please ignore it.
Here is problematic program.
====== foo1.cpp ======
struct Base
{
virtual void func (int) = 0;
};
struct Derived : public Base
{
void func (int)
{
// Stuff
}
};
struct Foo
{
void (*pfunc) (int);
Base* p_;
Foo (Base* p) : p_ (p) {}
void doit ()
{
// Here is what I want to reach
pfunc = p_->func;
// ------------------------
// g++ 4.0.1 produces the following error message:
// ........................
// error: argument of type ''void (Base::)(int)''
// does not match ''void (*)(int)
// ........................
// because func() is _member_ (not static) function
// ------------------------
}
};
int main ()
{
Base* p = new Derived;
Foo foo (p);
delete p;
return 0;
}
======================
Here is a _partial_ solution of the problem.
====== foo2.cpp ======
struct Base
{
virtual void func (int) = 0;
};
struct Derived : public Base
{
void func (int)
{
// Stuff
}
};
struct Foo
{
void (*pfunc) (int);
static Base* p_s;
Foo (Base* p) { p_s = p; }
static void wrapper(int a)
{
p_s->func (a);
}
void doit ()
{
pfunc = wrapper;
}
};
Base* Foo::p_s = 0;
int main ()
{
Base* p = new Derived;
Foo foo (p);
delete p;
return 0;
}
======================
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
这篇关于分配时的成员和静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!