C++ Compiler – Is It Bad Practice to Use for Function Overloading?

ccompilerfunctionsmethod-overloading

So I am working on a software design using C for a certain processor. The tool-kit includes the ability to compile C as well as C++. For what I am doing, there is no dynamic memory allocation available in this environment and the program is overall fairly simple. Not to mention that the device has almost no processor power or resources. There's really no strong need to use any C++ whatsoever.

That being said, there are a few places where I do function overloading (a feature of C++). I need to send a few different types of data and don't feel like using printf style formatting with some kind of %s (or whatever) argument. I've seen some people that didn't have access to a C++ compiler doing the printf thing, but in my case C++ support is available.

Now I'm sure I might get the question of why I need to overload a function to begin with. So I'll try to answer that right now. I need to transmit different types of data out a serial port so I have a few overloads that transmit the following data types:

unsigned char*
const char*
unsigned char
const char

I'd just prefer not to have one method that handles all of these things. When I call on the function I just want it to transmit out the serial port, I don't have a lot of resources so I don't want to do barely ANYTHING but my transmission.

Someone else saw my program and asked me, "why are you using CPP files?" So, that's my only reason. Is that bad practice?

Update

I'd like to address some questions asked:

An objective answer to your dilemma will depend on:

  1. Whether the size of the executable grows significantly if you use C++.

As of right now the size of the executable consumes 4.0% of program memory (out of 5248 bytes) and 8.3% of data memory (out of 342 bytes). That is, compiling for C++… I don't know what it would look like for C because I haven't been using the C compiler. I do know that this program will not grow any more, so for how limited the resources are I'd say I'm okay there…

  1. Whether there is any noticeable negative impact on performance if you
    use C++.

Well if there is, I haven't noticed anything… but then again that could be why I'm asking this question since I don't fully understand.

  1. Whether the code might be reused on a different platform where only a
    C compiler is available.

I know that the answer to this is definitely no. We are actually considering moving to a different processor, but only more powerful ARM-based processors (all of which I know for a fact have C++ compiler tool-chains).

Best Answer

I wouldn't go so far as to call it "bad practice" per se, but neither am I convinced it's really the right solution to your problem. If all you want is four separate functions to do your four data types, why do not what C programmers have done since time immemorial:

void transmit_uchar_buffer(unsigned char *buffer);
void transmit_char_buffer(char *buffer);
void transmit_uchar(unsigned char c);
void transmit_char(char c);

That's effectively what the C++ compiler is doing behind the scenes anyway and it's not that big of an overhead for the programmer. Avoids all the problems of "why are you writing not-quite-C with a C++ compiler", and means nobody else on your project is going to be confused by which bits of C++ are "allowed" and which bits aren't.