C++ sucks

Peter Korsten EMAIL HIDDEN
Thu Nov 22 19:37:32 CET 2007


Martin Naef schreef:

> After how many hours of trying something new?

Three days now, and several months a few years back.

> Ahh, here we have it - three days. After you've spent several years 
> coding with something else. What the heck do you expect? Becoming an 
> expert in a different language after just three days?

No, that would take a couple of weeks. But as I mentioned, I've been 
looking at C++, with several long interludes, for the best part of ten 
years now. Probably more than that, but in real development time, a bit 
less than a year.

> Erm, you can return objects (might not always be a good idea, but it 
> works), you can nicely separate interface and implementation (either 
> through simple or multiple inheritance), and I've yet to find a feature 
> that I *really* miss (no, I don't think garbage collection is the answer 
> to all your memory issues...).

Garbage collection and return-by-reference makes your life as a 
programmer just a lot easier. As does automatic bounds checking.

But try this in C++:

public interface A
{
	int a();
}

public class B implements A
{
	private int b;
	public int a() { return b; }
}

public class C
{
	private List<A> l = new ArrayList<A>();
	public void c( A a ) { l.add( a ); }

	public static void main( String[] args )
	{
		C instance = new C();
		instance.c( new B() );
	}
}

This is a simplified version of what happened to me when I tried to use 
an abstract base class as the type for a template class (vector, to be 
precise).

C++ doesn't have interfaces. It only has classes. Hence, you cannot 
completely separate interface and implementation.

> Of course, Java has been designed years after C++ and hence profits from 
> a bit of hindsight - it would be rather sad if it didn't do a few things 
> more elegantly.

What worries me is that both Java and C++ come from C, but that Java 
made the right choices - mostly, at least - from the start. Stroustrup
could have made the same choices, but instead he decided to retain 
backwards compatibility, and came up with this horrible syntax. And this 
was not necessary, because the only things that differ between the 
syntax of C and Java is that everything has to be a class, the use of 
the 'new' keyword, and the absence of pointers.

>> It's like you've been driving a nice family car for years, and then one 
>> morning you discover that someone has replaced it with a mule and cart. 
>> And a very stubborn mule at that.
> 
> No, you've replaced the family car with a Ferrari. It's not easy to 
> handle, and certainly temperamental, but it doesn't have the extra 
> baggage of other systems that holds you back when you have to get down 
> to the nitty gritty stuff.

If we keep up the comparison to vehicles, I'd rather compare C++ to a 
Centurion tank. A Ferrari is pleasing to the eye; C++ most certainly 
isn't. You can also make an awful mess with a tank, which seems rather 
appropriate for C++ (segmentation fault, core dumped).

>> Really, if you value your sanity, give C++ a wide berth and either use C 
>> or Java. C++ is like the unholy offspring of the two.
> 
> No. Choose the right tool for the task, and learn it properly.

Which in this particular case is C.

With Java, you're the driver. With C++, you're the driver, technician, 
petrol station attendant and tow service all in one. I'd rather just be 
driving, instead of worrying how this is going to leak memory or corrupt it.

- Peter



More information about the music-bar mailing list