473,388 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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
Nov 13 '05 #1
3 2277
In article <49**************************@posting.google.com >, Toondalis wrote:
How can i mix C and C++.
This is in the C++ FAQ:

http://www.parashift.com/c++-faq-lit...c-and-cpp.html

How could i add memeber function in C


You can't.
--
Andreas Kähäri
Nov 13 '05 #2


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.

Nov 13 '05 #3
se****@surreal.com.sg (Toondalis) wrote in message news:<49**************************@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);
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.