M
Bill said:When you code in C or C++, one of
the primary reasons for that language choice is
speed...so why kill yourself on the performance side
by using overly generic data structures?
Several C++ coders I know have expressed such amazing
ignorance of C
Most of the 'nice' feature of C++ are syntactic
sugar (introspection, operator overloading, automatic
constructors/destructors).
The try/catch clause is nice, but leads to sloppy programming.
Namespaces are nice, but lead to sloppy build environments.
Hi everyone,
I have been asked this question quite a few times and i wonder what
could the actual reason?
" When to prefer C over C++ or vice versa, of course this is for real
world pratical scenario"
As per my understanding, C++ makes it easier to program application
related logic as OOPS helps in that sense, but for some reason that is
not a reasonable explanation, could any one add on their comments?
Or C++ without using those features.John said:Some of the features that make C++ worth using (such as virtual
functions) incur some performance cost. For a lot of general-purpose
applications programming, this cost is negligible, but if you have to
account for every CPU cycle, it may be better to go with straight C.
Old said:Automatic constructors and destructors are much more
than syntactic sugar.
Well, it's just the same as setjmp/longjmp, but easier to
work with. I notice you use the passive voice, "leads to
sloppy programming". It is up to any particular programmer
whether to write sloppy code or not, it doesn't just happen
by itself.
Huh?
Martin Ambuhl a écrit :
Translation:
"I want to kill any discussion that goes beyond homework level".
Ian Collins a écrit :
I said "*one of* " the principal reasons.
Try implementing Resource Acquisition Is Initialization without them.Bill said:How so?
Absolutely. My complaints about C++ are primarily
that it seems to encourage sloppy programming.
I find the opposite, using namespaces helps me organise my code beI think that the concept
of namespaces is great, and I love them in Python, but
I just don't see that they buy you anything in C++.
You can accomplish the same thing by keeping
your libraries in order, and having namespaces seems
to encourage disorganized libraries and discourages
proper modularity.
Old said:Bill Pursell wrote:
Well, it's just the same as setjmp/longjmp, but easier to
work with. I notice you use the passive voice, "leads to
sloppy programming".
The problem of C++ is that it is an extremely complex language.
People that have invested a lot of time in mastering its complexity are
productive with it, people that do not want to bother may use C.
Using C coupled with a garbage collector makes many of the features
of C++ unnecessary since memory management, one of the principal reasons
for constructors/destructors disappears from view.
This means in practice that lists, arrays and many other commonly used
data structures are absent from the C standard library.
You can use C anywhere, you can code anything with it, it is a general
purpose language. Its advantages are its simplicity, and its
disadvantages are its simplicity.
Use C when it's appropriate. If you are writing a program
in C++, you may end up writing a lot of low-level routines
that are perfectly valid C.
If at that time you recognize that you are writing C, it increases the
flexibility of your code if you compile those sections as C and create a
C library, and then use them from C++, since they will also be useable
from C applications.
Some people argue that C++ makes it easier to program,
but this is often because they lack sufficient knowledge
of how to apply object-oriented programming techniques
in their C-code. In my (limited experience), C++ tends to
encourage difficult to maintain, hideous crap that is
just annoying to read.
The STL is an extremely nice feature of C++, but it
is often horribly inefficient. The data structures are
designed to be flexible and generic, and as a result
they are not the perfect match for any programming
situation. When you code in C or C++, one of
the primary reasons for that language choice is
speed...so why kill yourself on the performance side
by using overly generic data structures?
I will freely acknowledge that I have a strong bias
against C++.
I think I've met 3 people who write C++
code that doesn't make me vomit. Several C++
coders I know have expressed such amazing
ignorance of C as claiming that the -> operator
is not a part of the C language and an apparent
ignorance of the 'struct' keyword. (Which is to say
that one person actually said 'That's C++, C doesn't
have the arrow operator', and the abundance of
code that fails to use structures makes me believe
these people are just friggin' clueless.)
Most of the 'nice' feature of C++ are syntactic
sugar (introspection, operator overloading, automatic
constructors/destructors). The try/catch clause is
nice, but leads to sloppy programming.
Namespaces are nice, but lead to sloppy build environments. The STL is
nice, but is usually not the best choice.
In summary, use C when it is appropriate,
and use C++ never.
Which wouldn't give you a more efficient memcpy().Frederick said:You'd be suprised! I could write a more efficient "memcpy" in C++ than I
could in C.
If I were to use "memcpy" in C to copy an array of int's, I would have to
perform the redundant check that the arrays are suitably aligned for
optimal copying-time.
In C++, however, I could use a combination of function overloading and
templates. For instance:
Far form optimal where T is char on anything other than an 8 bit machine.#include <cstddef>
template<class T>
void memcpy(T *to,T const *from,std::size_t const len)
{
T const *const pover_to = to + len;
while (pover_to != to) *to++ = *from++;
}
Frederick said:At first I wasn't sure if I'd take part in this C Vs C++ discussion, but
I'll dip my feet...
jacob navia:
>>This means in practice that lists, arrays and many other commonly used
data structures are absent from the C standard library.
int array[5];
Mark said:Translation:
"Jacob Navia likes insulting the regulars in CLC because he is a fool"
Back to Martin's comment:
The commonest two reasons for someone asking why use C or C++ are
a) he's a troll and wants to start a flame war and
b) he's a student struggling with a stupid homework question.
So you see, Jacob's childish remark fits neither camp.
> "Jacob Navia likes insulting the regulars in CLC because he is a fool"
This deficiency is being addressed, have a look at std::tr1::array. Notjacob said:Frederick said:At first I wasn't sure if I'd take part in this C Vs C++ discussion,
but I'll dip my feet...
jacob navia:
This means in practice that lists, arrays and many other commonly used
data structures are absent from the C standard library.
int array[5];
That is an array only in very special situations... In most cases it
will "decay" into a pointer to int. The size information is destroyed
or discarded and there it goes, a buffer overflow please...
Frederick said:At first I wasn't sure if I'd take part in this C Vs C++ discussion, but
I'll dip my feet...
jacob navia:
This means in practice that lists, arrays and many other commonly used
data structures are absent from the C standard library.
int array[5];
That is an array only in very special situations...
In most cases it
will "decay" into a pointer to int.
The size information is destroyed
or discarded
Ian Collins said:jacob navia wrote: [...]This deficiency is being addressed, have a look at std::tr1::array. NotThat is an array only in very special situations... In most cases it
will "decay" into a pointer to int. The size information is destroyed
or discarded and there it goes, a buffer overflow please...
standard yet, but on its way.
Oops, I forgot to stick to the group's main topics of insults andKeith said:Ian Collins said:jacob navia wrote:
[...]
That is an array only in very special situations... In most cases it
will "decay" into a pointer to int. The size information is destroyed
or discarded and there it goes, a buffer overflow please...
This deficiency is being addressed, have a look at std::tr1::array. Not
standard yet, but on its way.
That's C++, isn't it? Did you perhaps momentarily forget which
newsgroup you're reading?
Oops, I forgot to stick to the group's main topics of insults andKeith said:Ian Collins said:jacob navia wrote:
[...]
That is an array only in very special situations... In most cases it
will "decay" into a pointer to int. The size information is destroyed
or discarded and there it goes, a buffer overflow please...
This deficiency is being addressed, have a look at std::tr1::array. Not
standard yet, but on its way.
That's C++, isn't it? Did you perhaps momentarily forget which
newsgroup you're reading?
accusations...
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.