I learnt C without realising it....how the?

Started by
24 comments, last by Will F 18 years, 9 months ago
First, C++ is really a very different language from C. It just happens to be built upon C, and have bent over backwards to provide compatibility with existing "legacy" C code - but proper, real-world C++ code looks quite different from proper, real-world C code. Any tutorial which claims otherwise is at best misguided and outdated.

Second, it's generally higher-level languages that are "slower"; however, any speed comparison between two languages is generally just plain wrong. The way the code is written can always make up the difference. :) In particular, C++ really is higher-level because it offers a greater range of abstractions; it is further away from the hardware and lets you think in more abstract concepts as opposed to what is actually happening on the hardware - whereas C was designed as a sort of 'super-assembly language'. However, it is also designed to use the C syntax for the most part, and only "automatically generate things" as far as it can without adding overhead.

For example, C++ makes you mark class member functions as "virtual" explicitly, simply because if there are none, it won't have to generate a vtable, such that the data layout becomes identical to what a C compiler would produce. But if the virtualness is needed, the compiler will do all the work there and build it in, at the low low cost of four extra bytes (typically) per object and two indirections per function call (only for the ones that actually are virtual).

IMHO, there's really no good reason to restrict yourself to using C any more, because 99% or so of C is valid C++, and the rest can be easily translated without any performance hit. However, real C++ programmers should be making use of C++ features as well, and there are a lot of them - but they will save you lots of grief and aggravation.

Third ... read what Oluseyi said about cout and printf. :) They are *very much* not the same thing.
Advertisement
Quote:Original post by Zahlman
IMHO, there's really no good reason to restrict yourself to using C any more, because 99% or so of C is valid C++, and the rest can be easily translated without any performance hit.
heh. [smile]
Quote:Original post by Oluseyi
Quote:Original post by shmoove
In the end, the difference between:
a + b;

and:
a.add(b);

is just style.

a.add(b.add(c.sub(d.add(e.add(f)))));

I'd say there's more than style to it.

[grin]
But that's still style. The operator overloading way is more stylish, but in the end both ways do the same thing.

shmoove

Quote:Original post by shmoove
Quote:Original post by Oluseyi
a.add(b.add(c.sub(d.add(e.add(f)))));

I'd say there's more than style to it.
[grin]
But that's still style. The operator overloading way is more stylish, but in the end both ways do the same thing.
Readability? Maintainability? Comprehensibility? That's an incredibly simple example, and it's already much more challenging to understand. Create one that's an order of magnitude more complex and you have the potential for real damage - including having one misplaced parenthesis potentially alter the entire expression (imagine having the argument to one of those add operations be a compound expression, then misplacing an internal parenthesis).

style is substance, and we should employ every stylistic convention that improves code quality - and, where appropriate, codify them in the language spec.
Using << notation for stream output has two main benefits from my point of view, and they are closely linked.

1. It provides a concise and consistent interface for stream output. That means the syntax is EXACTLY the same for any object supporting stream output. You don't have to worry about calling the right function on an object if you implemented your operator correctly, nor do you have to worry about misplaced parentheses.

2. Once you have got used to the syntax, it emphasises the action, i.e. stream output, rather than how we got there, i.e. a function call because the << operator is used pretty much exclusively for text mode stream output. (i.e. any programmer worth his salt can spot it from a mile off)

As I said the two are closely tied.

EDIT: My spelling left much to be desired
Quote:Original post by ReneGade RG dev
Well.....there is something called a GOTO command


One piece of advice... unless you want your code to look like spaghetti never use goto. There are some rare exceptions, but don't use it unless you're sure you understand why you need to use it in a particular situation.

This topic is closed to new replies.

Advertisement