473,387 Members | 1,891 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,387 software developers and data experts.

Question about mixing C and C++

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
Oct 20 '06 #1
5 1036
* 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?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 20 '06 #2

Ben wrote:
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.

Oct 20 '06 #3
Ben wrote:
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.

Oct 20 '06 #4
Ben wrote:
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
Oct 20 '06 #5

Martin Steen wrote:
>

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.

Oct 21 '06 #6

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

Similar topics

3
by: bergel | last post by:
Hello, Does anyone already have some experience in mixing AWT and Swing? Is it conceptually doable? Does the design of Swing prevent interaction between an AWT and a Swing widget? Regards,...
6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
9
by: TY | last post by:
Hi all, I have this little simple script: for i in range(10): for j in range(5000000): pass # Timing-delay loop print i When you run it, it behaves as you would expect -- it prints 0...
5
by: Phillip N Rounds | last post by:
I have a webform ( ASP.NET 1.1, CodeBehinde = C#). The user selects an address item from a dropdown list box. The details of the address then populate appropriate text boxes. Each textbox has an...
4
by: Darren | last post by:
This is a theoretical question, meaning I don't have any specific code to show. On a large ASP.NET 1.1 application what is the best way to populate a forms data fields if you expect a large...
8
by: kathy | last post by:
If I have 2D array like: int **p; p = new int*; for(int i=0;i<10;i++) { p = new int; }
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
12
by: kostas | last post by:
Hi I was asked to propose an interview question for C/C++ programmers (CAD/CAE software) I came up with the following...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.