By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,379 Members | 1,646 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,379 IT Pros & Developers. It's quick & easy.

using function pointer class members.

P: n/a
Hi,
I am having a CPP file which defines a class:

class xyz{
public:
int (*funcptr)(void);
}

A function foo is defined in a "C file"

and in the CPP file, I need to make an assignment
funcptr = foo;

So I declared foo as extern "C" in the CPP file:
extern "C" int foo(void);

But I am getting an error in the assignment, since one side is of type
"extern C". What is the solution for this?

Please do help.

TIA.
--
jag.
"Quaerendo invenietis"

Nov 10 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
<ja*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com
Hi,
I am having a CPP file which defines a class:

class xyz{
public:
int (*funcptr)(void);
}

A function foo is defined in a "C file"

and in the CPP file, I need to make an assignment
funcptr = foo;
funcptr is a member and hence only exists as part of an object. Thus you
need something like

xyz obj;
obj.funcptr = foo;

Perhaps this is what you do, but if you don't post real code then we can't
know and hence can't diagnose the problem.
So I declared foo as extern "C" in the CPP file:
extern "C" int foo(void);

But I am getting an error in the assignment, since one side is of type
"extern C". What is the solution for this?


Aside from the problem described above, it should work. If the above doesn't
solve the problem, then post the smallest complete code that exhibits the
problem (i.e., copy and paste actual code that has failed to compile).

--
John Carson

Nov 10 '05 #2

P: n/a
Hi,
main.CPP file
=======
extern "C" int function(void);

class xyz{
public:
int (*funcptr)(void);
}object;

int main(void){
int temp = 0;
object.funcptr = function; //this line gives me the prob.
temp = object.funcptr();
return 0;
}

second.C file
=========

int function(void){
return 1;
}

***************
The assignment "funcptr = function" gives me the error. Actually I am
working on TI's Code Composer Studio. So I am not sure if the error
confines to that environment.

The code is just a remake, just to present the core idea.

--
jag
"Quaerendo invenietis"

Nov 10 '05 #3

P: n/a
<ja*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com
Hi,
main.CPP file
=======
extern "C" int function(void);

class xyz{
public:
int (*funcptr)(void);
}object;

int main(void){
int temp = 0;
object.funcptr = function; //this line gives me the prob.
temp = object.funcptr();
return 0;
}

second.C file
=========

int function(void){
return 1;
}

***************
The assignment "funcptr = function" gives me the error. Actually I am
working on TI's Code Composer Studio. So I am not sure if the error
confines to that environment.

The code is just a remake, just to present the core idea.

--
jag
"Quaerendo invenietis"

Your code compiles and runs without a problem on VC++ (7.1 and 8.0) --- as
it should. If it won't run on TI's Code Composer Studio, then it looks like
that is where the problem lies. What does the error message actually say?
--
John Carson

Nov 10 '05 #4

P: n/a
ja*********@gmail.com wrote:
Hi,
I am having a CPP file which defines a class:

class xyz{
public:
int (*funcptr)(void);
}

A function foo is defined in a "C file"

and in the CPP file, I need to make an assignment
funcptr = foo;

So I declared foo as extern "C" in the CPP file:
extern "C" int foo(void);

But I am getting an error in the assignment, since one side is of type
"extern C". What is the solution for this?

Please do help.

TIA.
--
jag.
"Quaerendo invenietis"


Well I don't think that should be an error, but since it is the simple
solution is this
extern "C" int foo(void);

int foo_wrapper()
{
return foo();
}

class xyz{
public:
int (*funcptr)(void);
};

xyz bar;
bar.funcptr = foo_wrapper;

john
Nov 10 '05 #5

P: n/a
Hi,
Thanks for the replies. The error is as follows:
--error: a value of type "int (*)() C" cannot be assigned to an entity
of type "int (*)()"

That wrapper concept works. But applying the wrapper gives me an
overhead of an additional function call, increasing the overall MIPS of
my program (since the call is made in lakhs of times). So is there a
way of doing it, beyond the wrappers, declaring one the function
pointer as taking a __cdecl type function?
--
jag
"Quaerendo invenietis"

Nov 11 '05 #6

P: n/a
Hi,
Thanks for the replies. The error is as follows:
--error: a value of type "int (*)() C" cannot be assigned to an entity
of type "int (*)()"

That wrapper concept works. But applying the wrapper gives me an
overhead of an additional function call, increasing the overall MIPS of
my program (since the call is made in lakhs of times). So is there a
way of doing it, beyond the wrappers, declaring one the function
pointer as taking a __cdecl type function?
--
jag
"Quaerendo invenietis"

Nov 11 '05 #7

P: n/a
ja*********@gmail.com wrote:
Hi,
Thanks for the replies. The error is as follows:
--error: a value of type "int (*)() C" cannot be assigned to an entity
of type "int (*)()"

That wrapper concept works. But applying the wrapper gives me an
overhead of an additional function call, increasing the overall MIPS of
my program (since the call is made in lakhs of times). So is there a
way of doing it, beyond the wrappers, declaring one the function
pointer as taking a __cdecl type function?
--
jag
"Quaerendo invenietis"


You might find something like this works

int (__cdecl *funcptr)(void);

I forget the exact syntax but if that isn't it try something similar.
This is of course non-standard.

john
Nov 11 '05 #8

P: n/a
In article <dk**********@otis.netspace.net.au>,
John Carson <jc****************@netspace.net.au> wrote:
<ja*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googleg roups.com
main.CPP file
=======
extern "C" int function(void);

class xyz{
public:
int (*funcptr)(void);
}object;

int main(void){
int temp = 0;
object.funcptr = function; //this line gives me the prob.
temp = object.funcptr();
return 0;
}

second.C file
=========

int function(void){
return 1;
}

***************
The assignment "funcptr = function" gives me the error. Actually I am
working on TI's Code Composer Studio. So I am not sure if the error
confines to that environment.

The code is just a remake, just to present the core idea.

Your code compiles and runs without a problem on VC++ (7.1 and 8.0) --- as
it should. If it won't run on TI's Code Composer Studio, then it looks like
that is where the problem lies. What does the error message actually say?


Pretty sure the above code is ill-formed, don't recall if a diagnostic
is required or not. To fix the code, funcptr needs to be declared
with extern "C" too, the way to do that would be via a typedef.

extern "C" typedef int XXX();

and declared funcptr to be one of those.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 11 '05 #9

P: n/a
"Greg Comeau" <co****@panix.com> wrote in message
news:dl**********@panix2.panix.com
In article <dk**********@otis.netspace.net.au>,
John Carson <jc****************@netspace.net.au> wrote:
<ja*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com
main.CPP file
=======
extern "C" int function(void);

class xyz{
public:
int (*funcptr)(void);
}object;

int main(void){
int temp = 0;
object.funcptr = function; //this line gives me the prob.
temp = object.funcptr();
return 0;
}

second.C file
=========

int function(void){
return 1;
}

***************
The assignment "funcptr = function" gives me the error. Actually I
am working on TI's Code Composer Studio. So I am not sure if the
error confines to that environment.

The code is just a remake, just to present the core idea.

Your code compiles and runs without a problem on VC++ (7.1 and 8.0)
--- as it should. If it won't run on TI's Code Composer Studio, then
it looks like that is where the problem lies. What does the error
message actually say?


Pretty sure the above code is ill-formed, don't recall if a diagnostic
is required or not. To fix the code, funcptr needs to be declared
with extern "C" too


Ah. That makes sense. Mea culpa.
, the way to do that would be via a typedef.

extern "C" typedef int XXX();

and declared funcptr to be one of those.


As in

XXX * funcptr.

I vaguely recall having seen these function type typedefs before, but I had
forgotten they were possible. It is good to be reminded. I would have
written

extern "C" typedef int (*YYY)();

YYY funcptr.

--
John Carson

Nov 22 '05 #10

P: n/a
In article <dl***********@otis.netspace.net.au>,
John Carson <jc****************@netspace.net.au> wrote:
"Greg Comeau" <co****@panix.com> wrote in message
news:dl**********@panix2.panix.com
In article <dk**********@otis.netspace.net.au>,
John Carson <jc****************@netspace.net.au> wrote:
<ja*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com
main.CPP file
=======
extern "C" int function(void);

class xyz{
public:
int (*funcptr)(void);
}object;

int main(void){
int temp = 0;
object.funcptr = function; //this line gives me the prob.
temp = object.funcptr();
return 0;
}

second.C file
=========

int function(void){
return 1;
}

***************
The assignment "funcptr = function" gives me the error. Actually I
am working on TI's Code Composer Studio. So I am not sure if the
error confines to that environment.

The code is just a remake, just to present the core idea.
Your code compiles and runs without a problem on VC++ (7.1 and 8.0)
--- as it should. If it won't run on TI's Code Composer Studio, then
it looks like that is where the problem lies. What does the error
message actually say?


Pretty sure the above code is ill-formed, don't recall if a diagnostic
is required or not. To fix the code, funcptr needs to be declared
with extern "C" too


Ah. That makes sense. Mea culpa.
, the way to do that would be via a typedef.

extern "C" typedef int XXX();

and declared funcptr to be one of those.


As in

XXX * funcptr.

I vaguely recall having seen these function type typedefs before, but I had
forgotten they were possible. It is good to be reminded. I would have
written

extern "C" typedef int (*YYY)();

YYY funcptr.


As in that both ways can work:

extern "C" int function(void) { return 99; }

extern "C" typedef int ecf(void);

extern "C" typedef int (*pecf)(void);

class xyz{
public:
ecf *funcptr;
pecf pfuncptr;
}object;

int main(void){
int temp = 0;
object.funcptr = function;
object.pfuncptr = function;
temp = object.funcptr();
return 0;
}

At least in strict modes (Comeau C++ also support an
--implicit_extern_c_type_conversion to metabolize the types
if such a transition model is needed for some reason like
code coming from a compiler which does not support Standard C++ yet).
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #11

P: n/a
Greg Comeau schrieb:
Pretty sure the above code is ill-formed, don't recall if a diagnostic
is required or not. To fix the code, funcptr needs to be declared
with extern "C" too, the way to do that would be via a typedef.

extern "C" typedef int XXX();

and declared funcptr to be one of those.


it can be typedef'd, but it doesn't have to be. If the function is a
typedef, it could look like this:

-----------------------------------------
extern "C" typedef int func_type();
// or:
//extern "C" typedef int (*func_typePtr)();

int f_specialized();

class X {
public:
func_type *myfunc;
// or:
//func_typePtr myfunc;
}

int main()
{
X xobj;
xobj.myfunc = f_specialized;
int res = xobj.myfunc();
//...
return 0;
}

// ...
int f_specialized()
{
int res;
//...
return res;
}
-----------------------------------------

Without typedef, the other syntax is quite ok and compiles also with g++.
Eckhard
Nov 22 '05 #12

P: n/a
In article <43**********************@news.freenet.de>,
Eckhard Lehmann <ec****@web.de> wrote:
Greg Comeau schrieb:
Pretty sure the above code is ill-formed, don't recall if a diagnostic
is required or not. To fix the code, funcptr needs to be declared
with extern "C" too, the way to do that would be via a typedef.

extern "C" typedef int XXX();

and declared funcptr to be one of those.


it can be typedef'd, but it doesn't have to be. If the function is a
typedef, it could look like this:

-----------------------------------------
extern "C" typedef int func_type();
// or:
//extern "C" typedef int (*func_typePtr)();

int f_specialized();

class X {
public:
func_type *myfunc;
// or:
//func_typePtr myfunc;
}

int main()
{
X xobj;
xobj.myfunc = f_specialized;
int res = xobj.myfunc();
//...
return 0;
}

// ...
int f_specialized()
{
int res;
//...
return res;
}
-----------------------------------------

Without typedef, the other syntax is quite ok and compiles also with g++.


I gave examples to those same effects. As to the original code,
as mentioned it is ill-formed. There is words is 7.5 of the standard
to that effect, despite compilers accepting it to the contrary.
Furthermore, there is words in 5.2.2 that make calling such a thing undefined.
Obviously on machines where they can be considered the same[1],
the compiler would have to go out of its way to "do the wrong thing"
that's a seperate issue.

[1] I mean the same underlying semantics, note the types.
As mentioned, type-wise, they are not the same type.
Casting a int(long) to a int(int) function and calling it
might work too say on a machine where long and int are
the same underneath it all, but that's quite different than
saying it's "quite ok"
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #13

P: n/a

Greg Comeau wrote:
I gave examples to those same effects. As to the original code,
as mentioned it is ill-formed. There is words is 7.5 of the standard
to that effect, despite compilers accepting it to the contrary.
Furthermore, there is words in 5.2.2 that make calling such a thing undefined.
Obviously on machines where they can be considered the same[1],
the compiler would have to go out of its way to "do the wrong thing"
that's a seperate issue.

[1] I mean the same underlying semantics, note the types.
As mentioned, type-wise, they are not the same type.
Casting a int(long) to a int(int) function and calling it
might work too say on a machine where long and int are
the same underneath it all, but that's quite different than
saying it's "quite ok"

What does "typedef" do?
Eckhard

Nov 22 '05 #14

P: n/a
In article <11**********************@f14g2000cwb.googlegroups .com>,
<ec****@web.de> wrote:
Greg Comeau wrote:
I gave examples to those same effects. As to the original code,
as mentioned it is ill-formed. There is words is 7.5 of the standard
to that effect, despite compilers accepting it to the contrary.
Furthermore, there is words in 5.2.2 that make calling such a thing undefined.
Obviously on machines where they can be considered the same[1],
the compiler would have to go out of its way to "do the wrong thing"
that's a seperate issue.

[1] I mean the same underlying semantics, note the types.
As mentioned, type-wise, they are not the same type.
Casting a int(long) to a int(int) function and calling it
might work too say on a machine where long and int are
the same underneath it all, but that's quite different than
saying it's "quite ok"

What does "typedef" do?


Creates an alias (another name) for an existing type.
In this case, it allows one to capture the extern "C"ness.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.