1

I am writing code in c++. I need to support few basic data types and something like BigInt. This types will be exposed to outside world(through header file), which might also include c program.
Should I implement BigInt like data type as struct or class?
The confusion is because
1. If I implement it as class, as OO advantages, I can do all processing in class. But I may have to implement some work around for c programs
2. If I implement it as struct I need not do anything special for c programs, but I loose modularity and ease of implementation.

R. Martinho Fernandes
236k73 gold badges443 silver badges518 bronze badges
asked May 8, 2012 at 6:21
0

2 Answers 2

4

basically C couldn't access C++ objects, either struct/class (they're the same in C++, only differs in default visibility specifier). You have to create procedural wrapper for the C++ object (i.e. creation, method call, destruction, etc).

For creation, create a function that returns opaque pointer (in C++, this would be the object pointer). For method call, add the returned pointer (from creation function above) as one of the (typically first) parameter. For destruction it's the same as method call, but typically receives no other parameter other than the pointer above.

answered May 8, 2012 at 6:32
Sign up to request clarification or add additional context in comments.

Comments

0

If you plan on using it in C, I suggest you write it in C. C++ gets along with C a million times better than C gets along with C++. Another option would be to write it in C and then provide a thin C++ wrapper that gives it an OO interface.

answered May 8, 2012 at 6:24

2 Comments

I have my library already written in c++. I am just extending it to support BigInt like data type. The main usage will be for c++, but for few cases it might be c.
Well, there are ways to use C++ objects in C, but since C has no concept of an object in the sense that C++ does, it gets ugly very quickly. Basically you have to create wrapper functions for all of the methods since the class will not be linkable from C.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.