What exactly makes C++ difficult?

Started by
41 comments, last by Hodgman 14 years, 10 months ago
I am curious. What makes C++ more difficult than any other language. It's powerful and you have to keep track of everything, but that isn't hard to do. I should probably search more, but all I've seen on the subject is that C++ requires more memory management while easier languages do that for you. Most of my experience so far comes from learning C++. I've learned a little Java and VB. However, I haven't done much of the last two I mentioned. I was wondering if anyone can point me in the right direction to truly understanding the differences between the languages and what makes them harder and why?
Advertisement
That's the thing -- it's not difficult.

The problem lies in the number of pitfalls associated with the raw power of C++. While the syntax is generally easy to understand and pick up, it's the subtlties of the language grammer that catches beginners off guard.

The biggest pitfall with C++ (which it inherited from C) is the use of pointers. As powerful as they are, there are so many programmers that just don't understand how to properly use them and you start ending up with unitialized and dangling pointers both of which have the general effect of causing your program to train wreck.

Beyond the simple pitfalls is the OOP nature of C++. Many times beginners misuse and abuse classes and their most powerful features: inheritance and polymorphism. Couple this with pointer abuse and you have a recipe for disaster.

On top of all of this is the low-level memory mangement, bitwise operations and operator overloading that can be very confusing. Much of the memory management, however, is easily done for us through the use of the standard template library (STL) but its syntax can be quite intimidating to the unitiated.

The long and short of it is C++ is not a difficult language to learn. It's a difficult language to master due to all of its subtlties.

-Lead developer for OutpostHD

http://www.lairworks.com

The best way to learn the differences is to keep writing programs in all those languages. Along the way you'll write programs with bugs, and some of those bugs will be due to language deficiencies, and some of those bugs you'll spend days or weeks trying to figure out.

The language that results in the most frustration is your most difficult language.

1) C++ is a federation of languages.
It contains a syntax similar to plain C (with enhancements for Object Oriented programming), but it also contains a template meta programming language (which can act as a compile-time functional language!), a pre-processing language and the standard library (AKA the STL).

Many people only learn about half of these things when "learning C++".

2) C++ does not try to protect you from your own stupidity.

Whereas some newer languages have friendly abstractions like "This variable is a handle to an object", C++'s abstraction is more like "This variable holds a memory address, which could be interpreted as the address of an object".
This allows you to do things like pretend that one object is a different type (deliberately or accidentally), which could cause memory corruption or other strange behaviors.

3) C++ supports OOP, but doesn't force it upon you.
People who have learnt in languages like Java might find it hard to adjust to this.

Java-style multiple inheritance is a lot easier for your average coder to use than C++ virtual inheritance.

[edit]You've also got access to C's standard library as well!
Many people who use C++ actually use the C library rather than the C++ library.
You should read this thread. Particularly, my post in that thread contains a classic example of why C++ is hard. Also, Ravyne's post explains that the language isn't always the problem, but the non friendly error messages the compiler generates.

But there are other things you need to know in C++. For example, is the memory in std::string contiguous? Is it in std::vector? Do you know and understand the Rule of Three? Do you know how to properly handle exceptions and how to throw them, including how to throw one in a constructor and not leak any allocated memory? Do you know what undefined behavior implies (I'll answer this one: just because your program appears to be working doesn't mean you don't have a massive bug that is just waiting to blow up in your face after you've released your product)? In what range are you legally allowed to increment/decrement a pointer to a block of memory in? What identifiers are reserved for the implementation? How can you avoid the static initialization order fiasco? What is the static initialization order fiasco?

Aside from lots of these types of questions I could ask, there's also a lack of functionality in the C++ standard library. For example, Java and C# and VB all have ways for letting you connect to the internet, download things, send emails, easily handle multithreading, etc. Not so with the C++ standard library. You either have to roll your own code or google for a 3rd party library that can do all this, and then you have to take the time to get familiar with that library, probably build it yourself, link it, etc...

[edit]

ninja'd += 2;
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Learning object oriented programming?

I originally learnt C, it was a great low level language but large programs could get messy, not to mention rogue pointers. then came along Objected Oriented programming which made designing programs cleaner.

These days you shouldn't even need to learn a language, to develop a program just plug in the right components you want into the proper development tool and HEY PRESTO! you have something like what you wanted! .. maybe? Well that's what I want anyway! LOL!

padlock.

Also noteworthy is that C/C++ is very low level and powerful. That means you can do anything with it, everyone has source code for it, and it is "compatable" with every device.
But it is NOT a RAD language out of the box. A RAD language like Python or C# gives you a lot of extra features, and covers a lot of extra cases so you don't have to think about them. This makes prototyping things very fast. Expecially when some RAD languages actually look almost like what is termed "pseudo code".
C/C++ usually means there is a lot of base work and setup involved in getting even the smallest project running. Hop over to python and you can have the same app (maybe not as fast, extensible, or complete) up and running in a usable form in only a few hours work.

It is similar to the comparison of Windows/iProduct/Wii type things vs PS3/linux type things. One may have all the features, but you need a manual to figure it out. The other holds your hand through all the common stuff, but makes your life a pain in the ass once you want to dig deeper.
Quote:Original post by KulSeran
and it is "compatable" with every device.


Not exactly. I can't use C++ on an iPhone, for instance. That uses Objective-C which is totally different. I couldn't use C++ on a Nintendo 64 because that uses its own special ASM instruction set.

My toaster isn't 'compatible' with C++ either.

What it boils down to is that any environment with a C++ compiler that follows ANSI standards is likely to be able to compile any program you write so long as you don't have platform specific code in it (e.g., trying to use MessageBoxEx() on Linux won't work just as trying to use signal() won't work on Windows).

Quote:Original post by KulSeran
2) C++ does not try to protect you from your own stupidity.


This is thee biggest point. C++ doesn't save you from yourself unlike languages like BASIC, PHP, JAVA and so on. With power comes responsibility and abuse of that responsibility results in programs that fail. C++ does exactly what you tell it to do regardless of whether the instruction is erronous or not. This often leads programs to train wreck spectacularly and just 15 years ago could easily crash an operating system (which for now will remain nameless). It is because of the fact that C++ doesn't try to correct for errors that it appears 'difficult' and many bugs come from the improper use of pointers. Probably the most annoying thing and most difficult to find error is the 'dangling pointer' -- this is an error in which the programmer has freed memory from a specific location but continues to attempt to access the memory. The program may continue for many seconds or even minutes before you get what's called a seg-fault. This is a particular problem with beginners and if you're not intimately familiar with your debugger can take weeks to track down and a great deal of frustration. This, alone, is what gives C++ its reputation of being difficult to learn (in my humble opinion, of course).

Anyway, that's far more specific than you probably cared to hear and since you're asking what makes C++ difficult you probably don't understand most of it. Suffice it to say that you'll be spending months figuring out what made your seemingly innocent program blow up which could be frustrating but once it clicks it suddenly becomes an amazing language with very little in the way of limitations.

-Lead developer for OutpostHD

http://www.lairworks.com

Quote:Original post by leeor_net
Not exactly. I can't use C++ on an iPhone, for instance. That uses Objective-C which is totally different. I couldn't use C++ on a Nintendo 64 because that uses its own special ASM instruction set.

Sure you could. If you had a C++ compiler for the Nintendo 64 system you'd be able to use C++. And you can, actually, use C++ for the iPhone, though Objective-C is what is mainly used.

Quote:Original post by leeor_net
My toaster isn't 'compatible' with C++ either.

You should upgrade your toaster then. It's out of date [smile]
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Quote:Original post by leeor_net
Not exactly. I can't use C++ on an iPhone, for instance.
Yes you can ;p
Obj-C is only required for parts of iPhone dev ;)
[edit]dammit, ninjad!

This topic is closed to new replies.

Advertisement