| Topic: |
DEVELOP > C |
| User: |
"Toondalis" |
| Date: |
13 Nov 2003 05:11:22 AM |
| Object: |
mix c and c+++ |
Hi,
newbie question
How can i mix C and C++. and get compile
How could i add memeber function in C
eg
struct animation
{
int bPlay;
Play (int a)
{
// code
}
}
thanks
seewan
.
|
|
| User: "John Bode" |
|
| Title: Re: mix c and c+++ |
13 Nov 2003 09:49:00 AM |
|
|
(Toondalis) wrote in message news:<4977da50.0311130311.25fc993d@posting.google.com>...
Hi,
newbie question
How can i mix C and C++. and get compile
Not in the same source file. You can create projects where some files
are written in C and others in C++, but that's kind of out of scope
for this newsgroup.
How could i add memeber function in C
eg
struct animation
{
int bPlay;
Play (int a)
{
// code
}
}
thanks
seewan
C does not allow struct members to be of type "function returning...".
However, you may have struct members that are *pointers* to
functions, e.g.
struct animation
{
int bPlay;
int (*Playfp)(int a);
...
};
int Play (int a)
{
/* code */
}
....
struct animation anm;
anm.Playfp = Play;
anm.Playfp (x);
It's not the same thing as a member function in C++, though; you don't
have a *this pointer, so if you want the function to operate on a
specific instance of struct animation, you'll have to pass a pointer
to that instance, e.g.
struct animation {
int bPlay;
int (*Playfp) (struct animation *anm, int x);
...
};
int Play (struct animation *anm, int x)
{
/* code */
}
struct animation anm1, anm2;
anm1.Playfp = anm2.Playfp = Play;
anm1.Playfp (&anm1, x);
anm2.Playfp (&anm2, x);
.
|
|
|
|
| User: "Andreas Kahari" |
|
| Title: Re: mix c and c+++ |
13 Nov 2003 05:15:10 AM |
|
|
In article <4977da50.0311130311.25fc993d@posting.google.com>, Toondalis wrote:
How can i mix C and C++.
This is in the C++ FAQ:
http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html
How could i add memeber function in C
You can't.
--
Andreas Kähäri
.
|
|
|
|
| User: "Ed Morton" |
|
| Title: Re: mix c and c+++ |
13 Nov 2003 09:18:19 AM |
|
|
Toondalis wrote:
Hi,
newbie question
How can i mix C and C++. and get compile
How could i add memeber function in C
Best you can do is declare a function pointer within your structure and
then later initialise it to point to the function. Your function should
also take a pointer to the structure so it can access the other data. e.g.
#include "stdio.h"
typedef void (*FP_VOID)(); /* ptr to function returning void */
typedef struct {
int bPlay;
FP_VOID pPlay; /* function pointer */
} ANIMATION;
void Play(ANIMATION *this, int a) {
printf("bPlay=%d\n",this->bPlay);
printf("a=%d\n",a);
}
int main(void) {
ANIMATION anim = { 7, Play };
anim.pPlay(&anim,10);
return 0;
}
Of course, if this is what you need, you may want to reconsider just
using C++. Note that doing the above loses the compilers argument
type-checking ability for "Play".
Regards,
Ed.
.
|
|
|
|

|
Related Articles |
Is it a good thing that program mix C and C++? Newbie quesiton: Mix C and C++ Mix different C source files into a single one Mix C++ DLL with C main program acceptable to mix C/C++? Programmable Logic Circuits and C Pointers and Arrays in C?? Re: printf() and character arrays
| Sizeof and increment operators GUI project, C, Win32, and lisp pointers and integers C Program [ Turbo-C ] , to extract only-Printable-characters from a file ( any type of file) and display them ( i.e equivalent to "strings" command in UNIX) Why the setjmp and longjmp I wrote can not work? Best PHP or C libraries for handling images, video, and music: watermarking, thumbnailing, truncating, fading, etc. signed and unsigned char
|
|
|