mix c and c+++



 DEVELOP > C > mix c and c+++

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER