Question about mixing C and C++

B

Ben

Is there a clean way to incorporate existing C code as part of a class?
Defining the functions globally works for some cases, but for this
particular application I really need the scope limited to a single class
instance.

(The code in question uses POSIX threads, so simply adding "[CLASS]::"
won't work as far as I know.)

Thanks,
Ben
 
A

Alf P. Steinbach

* Ben:
Is there a clean way to incorporate existing C code as part of a class?
Defining the functions globally works for some cases, but for this
particular application I really need the scope limited to a single class
instance.

Depends what you mean.

Can you give an example?
 
E

Earl Purple

Ben said:
Is there a clean way to incorporate existing C code as part of a class?
Defining the functions globally works for some cases, but for this
particular application I really need the scope limited to a single class
instance.

(The code in question uses POSIX threads, so simply adding "[CLASS]::"
won't work as far as I know.)

Thanks,
Ben

Class functions can call free functions. If you want the function to be
only in the scope of your class then put it in the anonymous namespace
of the compilation unit of the class (the .cpp file).

I'm not 100% sure what you are actually asking though.
 
B

Bart

Ben said:
Is there a clean way to incorporate existing C code as part of a class?
Defining the functions globally works for some cases, but for this
particular application I really need the scope limited to a single class
instance.

Not sure what you mean, but you don't have to declare your functions in
a header file if you don't want them to be visible in other translation
units. You can also place them in an annonymous namespace as already
pointed out.
(The code in question uses POSIX threads, so simply adding "[CLASS]::"
won't work as far as I know.)

<OT>
You can use static member functions with POSIX threads. You just have
to pass 'this' as the initialization parameter so that your non-static
class members can be accessed.
</OT>

Regards,
Bart.
 
M

Martin Steen

Ben said:
Is there a clean way to incorporate existing C code as part of a class?
Defining the functions globally works for some cases, but for this
particular application I really need the scope limited to a single class
instance.

(The code in question uses POSIX threads, so simply adding "[CLASS]::"
won't work as far as I know.)

Thanks,
Ben


You can make a C++ "wrapper" for the C-functions. Example:

// The C-function
// Use "static" to make the C-functions invisible for
// the global scope.
static int FunctionA(int x, int y)
{
return x + y;
}

// the wrapper class
class CWrapper
{
public:

int CallCFunctionA(int x, int y);
};

// implementation of CallCFunctionA
int CWrapper::CallCFunctionA(int x, int y);
{
return FunctionA(x, y);
}


This is A LOT of work if you have many C-files..

Best regards,
-Martin
 
E

Earl Purple

Martin said:
You can make a C++ "wrapper" for the C-functions. Example:

// The C-function
// Use "static" to make the C-functions invisible for
// the global scope.
static int FunctionA(int x, int y)
{
return x + y;
}

// the wrapper class
class CWrapper
{
public:

int CallCFunctionA(int x, int y);
};

// implementation of CallCFunctionA
int CWrapper::CallCFunctionA(int x, int y);
{
return FunctionA(x, y);
}

If a C API is written with function to create a pointer to something, a
set of functions to manipulate the pointer and a function to dispose of
it, then it is a good candidate for a C++ wrapper.

pthread_mutex is an obvious example, although it gets tricky when
Condition Variables get into the picture. Note that the mutex lock is a
different object (class) to the mutex itself. I usually make it a
nested class but it doesn't have to be.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top