C++ = bad language to learn? I need some cheering up!

Started by
81 comments, last by Telastyn 15 years, 9 months ago
I don't know why people think C++ is so horrid for a beginner. I started with X86 assembler. THATS bad for a beginner although not horrible.

Ive been playing in C++ for awhile now and I rather enjoy it. The only pitfalls I run into is when I cant find a clear code example on a concept.
Advertisement
Quote:Original post by Chrono1081
I don't know why people think C++ is so horrid for a beginner. I started with X86 assembler. THATS bad for a beginner although not horrible.

Ive been playing in C++ for awhile now and I rather enjoy it. The only pitfalls I run into is when I cant find a clear code example on a concept.


So I take it you have passed all of Washu's quizes?
Best regards, Omid
Quote:Original post by Chrono1081
I don't know why people think C++ is so horrid for a beginner.


And this thread or the half dozen other C++ threads you've been party to in the past... 3 months or so didn't spell out exactly what other people's thoughts are? Even if you don't agree with people's reasoning, you can at least comprehend what they argue, right?
Dear monkey christ can people please stop writing and refering to "C/C++" there is no such thing! C and C++ are distinct languages; I program in C++ and I'd never claim to 'know' C.

*rages*
Quote:Original post by DevFred
Can you spot the errors in the following snippet?
int& f(int a){    int x = g(a++, a++);    return x;}


You forgot to define the g function.
Placeholder for better sig.
Quote:Original post by lol
Quote:Original post by DevFred
Can you spot the errors in the following snippet?
int& f(int a){    int x = g(a++, a++);    return x;}


You forgot to define the g function.


You can assume it is already defined. The real problems, as mentioned before, relate to the improper use of a sequence point and reference to a stack variable.
Quote:Original post by phantom
Dear monkey christ can people please stop writing and refering to "C/C++" there is no such thing! C and C++ are distinct languages; I program in C++ and I'd never claim to 'know' C.

*rages*


int main(int argc, char* argv)
{
printf("Hi bob\n");
return 0;
}

so is that a C or a C++ fragment? While C isn't a proper subset of C++ it is close enough that most C code will compile with a C++ compiler with no modification and little modification for the rest.
Quote:Original post by stonemetal
Quote:Original post by phantom
Dear monkey christ can people please stop writing and refering to "C/C++" there is no such thing! C and C++ are distinct languages; I program in C++ and I'd never claim to 'know' C.

*rages*


int main(int argc, char* argv)
{
printf("Hi bob\n");
return 0;
}

so is that a C or a C++ fragment? While C isn't a proper subset of C++ it is close enough that most C code will compile with a C++ compiler with no modification and little modification for the rest.

The use of printf() implies C in my opinion, I guess*. They're similar, but phantom definitely has a point that they are distinct languages. I mean, you don't say Java/C#, do you?

*I could be wrong. I know C++, not C...
Quote:Original post by stonemetal
so is that a C or a C++ fragment?


"Hello world"


so is that an ML, LISP, Scheme, OCaml, Caml Light or Haskell fragment? As a side note, your code is a C fragment: a C++ fragment would have to #include <cstdio> to work this way, and (had it been production code instead of a simple "hello world") would not have used printf.

Quote:While C isn't a proper subset of C++ it is close enough that most C code will compile with a C++ compiler with no modification and little modification for the rest.


Whether a compiler can handle the code is irrelevant (there are cross-compilers available for several languages). What matters is whether a C++ programmer can easily maintain and extend your code. And, if your code happens to be written in the C language, any attempt to extend it in C++ will result in substantial rewrites to accomodate RAII, exception-safety and standard library changes.

EDIT: to emphasize a little bit on the difference of idioms between C and C++ (ignoring for a moment the various syntax differences between the two) consider the example of a tree that you wish to traverse in prefix order to apply a function to every node.

The C++ idioms are to either create a generic traversal function (using a function template) which takes a functor as an argument and calls that functor on every element of the tree in the right order, or to create a functor interface and have the traversal function take a reference to an instance implementing that interface to call its correct function on every element.

The C version would be to use a callback-and-void* idiom, with which the vast majority of C++ programmers are woefully unfamiliar. The result being:

// C++, generictemplate <typename F>void traverse(const Tree &t, F f){  // On every element  f(element);}// C++, inheritancestruct TreeVisitor {  virtual void visit(const Element &elem) = 0;};void traverse(const Tree &t, TreeVisitor &visitor){  // On every element  visitor.visit(element);}/* C, callback-and-void* */typedef void (*TreeVisitCallback)(Element*, void*);void traverse(Tree *t, TreeVisitCallback callback, void* data){  /* On every element */  callback(element, data);}


A similar reasoning can be applied to data hiding in C and C++. C++ hides data using the private access modifier, while C hides data by defining an empty pointer type with construction-destruction functions (see, for instance, FILE*).

The manipulation of strings also varies: C tends to work with functions that return a not-to-be-modified string buffer, functions that return a you-own-it string buffer, and functions that fill a provided string buffer. By contrast, C++ uses std::string and thus avoids the varying call conventions. The same goes for arrays of values, too.

[Edited by - ToohrVyk on July 21, 2008 1:14:39 PM]
Quote:Original post by DevFred
C++ is not a bad language to learn, but it is a bad language to start with. The main reason is that there are many possibilities to write code that appears to do what you want (and when you compile and run you get the desired output), but that code might still be completely broken. If you try and run the code on some other compiler, computer or day of week, it may not do what you want anymore, and that can be very frustrating for a beginner. I am talking of course of "undefined behavior". Can you spot the errors in the following snippet?
int& f(int a){    int x = g(a++, a++);    return x;}

I believe this snippet of code might cause the programmer next to you (who has to help maintain such code) to clobber you with their copy of Code Complete.
Quit screwin' around! - Brock Samson

This topic is closed to new replies.

Advertisement