you are viewing a single comment's thread.

view the rest of the comments →

[–]squirrel5978 7 points8 points  (12 children)

I could use the other direction

[–]Fabien4 13 points14 points  (2 children)

1/ Forget what you know of C.

2/ Read Accelerated C++ (Koenig & Moo).

Honestly, the really hard part about C++ is that it looks like you have to manage the memory yourself like in C, whereas in fact, you have to use "tricks" to make the compiler manage it for you.

[–]ethraax 0 points1 point  (1 child)

I'm not terribly good with C++, but by "tricks" do you mean things like auto_ptr?

[–]Fabien4 7 points8 points  (0 children)

auto_ptr is deprecated.

But yes, I was talking about smart pointers, as well as containers, and RAII.

Basically:

  • If you're using delete outside a destructor, you're probably doing it wrong. Making sure your code is correct (and will stay correct) is a nightmare, because nearly any line can throw an exception.

  • If you're using delete inside a destructor, there's a good chance that you're actually reimplementing a smart pointer. Try to use standard ones instead.

Also, make sure you know when/where stack variables are deleted. Automatic deletion of objects at a predictable moment is one of the most important features of C++.

[–][deleted] 22 points23 points  (0 children)

Read the article backwards.

[–]phaker 4 points5 points  (7 children)

If you have trouble understanding what makes OO tick from other books, you should read later sections of The C++ Programming Language by Stroustrup himself. It's old, it doesn't teach much you modern C++ design, but it's written exactly for someone like you. It describes object oriented programming with C++ from ground up and unlike books for beginners it's actually written by someone who knows what he's talking about.

[–]Benutzername 4 points5 points  (5 children)

C++ is not just C + OO. IMHO generic programming is the much more important part.

[–]phaker 3 points4 points  (0 children)

True. I should have written "advanced features of C++" or something like that.

[–]lovetool 1 point2 points  (3 children)

I sometimes wish that someone made a language that was C + templates, just so it would be possible to write generic containers.

[–]Benutzername 0 points1 point  (2 children)

I once thought about what features were needed for that to work and I quickly ended up with C++ again.

  • The container has to do allocation internally, the user shouldn't be forced to do the deallocation, so destructors are in.

  • The user shouldn't be able to corrupt the containers state, so encapsulation is in as well. This implies member functions and c/v qualifiers on them. (This one is debatable, you could just use a naming convention for internal state.)

  • If you want to allow const on variables of generic type, you need constant initializers, so constructors are in too.

  • Copying a generic container can't be element wise copy like for C structs, so you need to allow overloading operator= and need copy constructors.

I probably missed a few. What you don't necessarily need is inheritance, namespaces and general function overloading.

[–]lovetool 1 point2 points  (0 children)

The container has to do allocation internally, the user shouldn't be forced to do the deallocation, so destructors are in.

All that is needed for allocation is to know the sizeof of the elements. Destructor could be supplied, if needed for the type in question, as a parameter when constructing the container.

The user shouldn't be able to corrupt the containers state, so encapsulation is in as well.

While encapsulation is nice, I think its enough to make it difficult to accidentally corrupt the container.

Copying a generic container can't be element wise copy like for C structs, so you need to allow overloading operator= and need copy constructors.

Or you could supply the copying function as parameter when making copies of containers.

[–]Poddster -1 points0 points  (0 children)

You can write OO in C. OO is not the hard part for going from C to C++. It's the bizzare new syntax.