2

I'm developing a NES emulator as a hobby, in my free time.
I use C++ because is the language I use mostly, know mostly and like mostly.
But now that I made some advance into the project I realize I'm not using almost any specific features of C++, and could have done it in plain C and getting the same result.
I don't use templates, operator overloading, polymorphism, inheritance... so what would you say? should I stay in C++ or rewrite it in C?
I won't do this to gain in performance, it could come as a side effect, but the idea is why should I use C++ if I don't need it?
The only features of C++ I'm using is classes to encapsulate data and methods, but that can be done as well with structs and functions, I'm using new and delete, but could as well use malloc and free, and I'm using inheritance just for callbacks, which could be achieved with pointers to functions.
Remember, it's a hobby project, I have no deadlines, so the overhead time and work that would require a re-write are not a problem, might be fun as well.

So, the question is C or C++?

Petruza
  • 11,744
  • 25
  • 84
  • 136
  • Are you using any of the STL ? – mmmmmm Dec 03 '11 at 17:06
  • I think [Programmers](http://programmers.stackexchange.com) was a better place to ask this. – Hossein Dec 03 '11 at 17:07
  • 2
    Well, your essay is saying you don't know C++. Basically, you are using a C++ compiler to compile your C code, which is unfortunate but true. – Khaled Alshaya Dec 03 '11 at 17:09
  • Take a look at my question and the other questions I referenced http://stackoverflow.com/questions/8333789/when-is-it-appropriate-to-use-c-as-object-oriented-language I'm highly interested in the answer too. – Roman Byshko Dec 03 '11 at 17:09
  • 2
    @AraK well that's pretty rude. I do know C++, but it's also true that in this project I started to use C++ without realizing that I wouldn't need it later. – Petruza Dec 03 '11 at 17:22
  • @Mark : not yet, may need to use vectors, but might as well get around with plain old arrays – Petruza Dec 03 '11 at 17:27
  • 1
    @AraK the OP said that classes were being used, thus it's not just "C being compiled by C++". I agree with Petruza, very rude. – Chris Eberle Dec 03 '11 at 17:28
  • @Petruza Sorry, English is not my first language :) – Khaled Alshaya Dec 03 '11 at 17:29
  • Why is this not constructive? I have a serious programming related question and more experienced programmer can and are helping me. – Petruza Dec 03 '11 at 21:04

7 Answers7

8

If you are making use of even a few of C++ features, I would just stick with C++. The only reason to really avoid C++ would be if you were on an embedded system and had no option. There are a couple of nice things about C++ that makes life easier and more maintainable. Unless of course you want to use this as an exercise to force yourself to learn how to do things in pure C.

TJD
  • 11,800
  • 1
  • 26
  • 34
  • 3
    Indeed. IMHO, just using `std::string`, `std::cin` and `std::cout` is enough of a justification to use C++ over C. Many introductory programming classes use the C subset of C++ to teach procedural programming and still use C++ strings and I/O facilities. Then, of course, there's no reason to deprive yourself of `std::vector<>` and `std::map<>`, nor `std::sort()` *et. al.*. Of course, defining classes to use [RAII](http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization) is also a very nice feat. – André Caron Dec 03 '11 at 17:25
3

Stay in C++ and utilize its STL containers, even if your application is not built around classes.

André Caron
  • 44,541
  • 12
  • 67
  • 125
Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90
3

One of the design principles of C++ is not to add any overhead for features you don't use.

You say you use "almost none" of C++ features. Just keep using those few features you do like or find useful and don't worry about the rest.

Tomas Andrle
  • 13,132
  • 15
  • 75
  • 92
1

I would stick with C, until you feel like there is some feature that you really need in C++ that would be difficult for you to do in C.

The reason is, with C++, it's very easy to get sucked into learning new features so you end up spending a ton more time on C++ than on your NES emulator.

This, in itself, is not a bad thing, if your primary objective is to learn C++. Since learning C++ is not your main focus, and since you are just barely starting out in C++, I would recommend that you stay in C. After all, entire kernels are still being written in C.

(FWIW, I'm primarily a C++ programmer these days, but I started in C).

kfmfe04
  • 14,936
  • 14
  • 74
  • 140
0

If for whatever reason you would want to use C instead of C++ you should make the switch now. C is a subset of C++, so if you can do it in plain C I would say go for it. If you decide later that you need some of the ++ that C++ has to offer it would be easier to change the code from C to C++. It's much harder to change from C++ to C.

Boundless
  • 2,444
  • 2
  • 25
  • 40
  • I disagree, it might be easy to mutate C code to compile under C++ compiler, but writing idiomatic/maintainable C++ code is for me very different from writing C code. – Khaled Alshaya Dec 03 '11 at 17:15
  • @AraK Yes, writing C++ is very different from writing C. I agree with you, I didn't imply this. – Boundless Dec 03 '11 at 17:17
0

Staying with C++ you might gain something in the future, and will never lose anything.

xpda
  • 15,585
  • 8
  • 51
  • 82
sabof
  • 8,062
  • 4
  • 28
  • 52
  • 1
    Staying with C++ you might loose performance. – Roman Byshko Dec 03 '11 at 17:17
  • @RomanB.: not unless the C++ compiler is worse than the C compiler when it comes to compiling simple C-like code. – Alexey Frunze Dec 03 '11 at 17:21
  • Even if I don't use C++ specific features? If this is the case, is the performance loss significant enough to be considered? – sabof Dec 03 '11 at 17:23
  • Based on my understanding most of the c++ features are overhead during compile time, not time. As that was one of the design goles. Unless the platform is extreamly restrictive C++ is the best bet. – rakesh Dec 03 '11 at 18:42
  • @rakesh: Classes incur a performance hit because of passing around `this`, because of accessing virtual method tables and dealing with RTTI. The rest depends on how careful you are using C++, on whether or not your templates explode in numbers when compiled, on whether or not you do unnecessary/avoidable object creation/destruction/copying, on how often you throw exceptions, etc. More code and/or data isn't going to go unnoticed. – Alexey Frunze Dec 03 '11 at 23:06
  • 5
    @Alex: Overhead compared to what? C has no classes, no vtables, no RTTI. If you need those features, you would have to emulate them in C, which would be at least as "inefficient" as the native C++ facilities. If you don't need them, you don't use them, and there is no overhead. – fredoverflow Dec 04 '11 at 10:00
0

If you use strings and some kind of containers, C++ and its STL would be best to stay with. Also, C++ is not as lax as C when it comes to conversion, so you may get better (more) warnings/errors when compiling with C++, which is good. Other than that, I wouldn't care.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180