C-style coding in C++

G

Garma

In C++ code of classes, would it be bad to use all those C-runtime
functions, for example, strcpy, strlen, strcat, etc.?

What are Standard libraries which have the above C functions? istringstream
and string?

If using those C-runtime, does it mean that the code will also be linked to
C-runtime library and C++ libraries? Is there any disadvantage with this?

If a source code contains some classes that are not instantiated anywhere in
the code, will compilers automatically ignore them automatically?

Thanks for your comments!
 
E

E. Robert Tisdale

Garma said:
In C++ code of classes, would it be bad to use all those C-runtime
functions, for example, strcpy, strlen, strcat, etc.?
Yes.

What are Standard libraries which have the above C functions?
<sstream> and <string>?
Yes.

If using those C-runtime libraries,
does it mean that the code will also be linked to
C-runtime library and C++ libraries?

Consult your compiler manual for the answer to that question.
Is there any disadvantage with this?

It makes your C++ code more difficult to read, understand and maintain.
If a source code contains some classes
that are not instantiated anywhere in the code,
will compilers ignore them automatically?

Yes.
 
D

Donovan Rebbechi

In C++ code of classes, would it be bad to use all those C-runtime
functions, for example, strcpy, strlen, strcat, etc.?

One usually doesn't need these functions if the string class is used. Functions
like strcpy and strcat are dangerous if not used judiciously (you will get
buffer overruns if the destination string is not large enough to store the
result). strncat and strncpy are safer (you still have to know how
big the destination is, but not how big the source/s is/are)
What are Standard libraries which have the above C functions? istringstream
and string?

Don't understand the question. The functions are defined in said:
If using those C-runtime, does it mean that the code will also be linked to
C-runtime library and C++ libraries? Is there any disadvantage with this?

Implementation dependent. Unlikely to be a disadvantage arising this way.
If a source code contains some classes that are not instantiated anywhere in
the code, will compilers automatically ignore them automatically?

No.

Cheers,
 
N

Nick Hounsome

E. Robert Tisdale said:

No. It depends on what you are doing. If you have a C string then strlen is
the only reasonable way to get its length. Just be
sure you wouldn't be better off using std::string.

By they way they are not C runtime functions they are C++ runtime functions
that happen to be defined to be (more or less) the
same as those in the C standard.

The same as in C.
Or preferably use the C++ified headers: for <string.h> use <cstring> and put
std:: in front of them.

e.g.
#include <string.h>
int len = strlen("hello");
or better
#include said:
Consult your compiler manual for the answer to that question.

No - for a standard compliant C++ compiler it will be linked to the C++
library/libraries (which may just happen to be
the same as the C ones).
It makes your C++ code more difficult to read, understand and maintain.

What? the linking thing? How?

Not necessarily - in most implementations, if the source for the class is in
a separate file and is compiled and the resultant
object file is added to a library then when you come to link your
application it wont be linked in if it is not referenced.
This isn't standard C++ stuff - its just how (static) libraries and linkers
work.
 
R

Ron Natalie

Garma said:
In C++ code of classes, would it be bad to use all those C-runtime
functions, for example, strcpy, strlen, strcat, etc.?

No, what is bad style is using char* as a string type which it sucks
badly at when you have std::string available.
What are Standard libraries which have the above C functions? istringstream
and string?

If using those C-runtime, does it mean that the code will also be linked to
C-runtime library and C++ libraries? Is there any disadvantage with this?

They are required to work by the language. C++ includes the entire 1989
C library. The C runtime support for that IS poart of the C++ library as
far as the language is concerned.
If a source code contains some classes that are not instantiated anywhere in
the code, will compilers automatically ignore them automatically?

It doesn't "ignore" them (it has to read parse them), but they generally do not
show up in the output unless they are used.
 
J

Jonathan Turkanis

Nick Hounsome said:
No. It depends on what you are doing. If you have a C string then strlen is
the only reasonable way to get its length. Just be
sure you wouldn't be better off using std::string.

How about std::char_traits<>::length? :)

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top