Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Probably a stupid question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 nfries88   Members   -  Reputation: 259

Like
0Likes
Like

Posted 22 August 2012 - 09:04 PM

I'm currently trying to write a library in C, but I've found myself in a situation where using something like polymorphism would save me from a ton of code duplication.

However, I'd also like the library to function perfectly in C++ without requiring the library user to do a ton of casting.

This brings me to one of two solutions:
1) Overload every function that expects the base struct if using a C++ compiler.
2) use inheritance in C++, just make the base struct the first data member in C

#2 seems like the most attractive choice, just due to the number of functions involved. But it also seems as though it could break if the library was initially built in C (or if used in C, would break if the library was initially built in C++), I honestly have no clue.

Any advice?

[edit]Answered the question for myself. Sorry, mods can delete or whatever.

[mod edit]We generally don't delete threads, but prefer to leave them around for others to also learn from. If someone answers their own question, it's appreciated when they share their thoughts for the above reason.

Edited by Hodgman, 22 August 2012 - 09:18 PM.

Looking for paid or open-source C++ programming work. Been programming since 2005. No degree.

Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS