tags:

views:

2803

answers:

36

What are the often misunderstood concepts in c++?

A: 

C++ or C/C++? I would say for both the most misunderstood parts are memory management and pointers. The former because people don't free appropriately (too early or not at all!) and the latter because a lot of people just don't "get" pointers (either in the pass by reference "guise" or in general in something like a linked list).

Steven Robbins
+21  A: 

In decreasing order:

  1. make sure to release pointers for allocated memory
  2. when destructors should be virtual
  3. how virtual functions work

Interestingly not many people know the full details of virtual functions, but still seem to be ok with getting work done.

Sesh
You've got my +1 for item #2
Paolo Tedesco
Thank you. It is indeed a pain point.
Sesh
1. Use smart pointers for allocated memory. It'll make your life so much easier.
David Thornley
@David - you are right. However the question is about misunderstood concepts in C++, and memory management tops the list at least for what I have seen.
Sesh
Guilty of #2. I think. =[ +1 anyway.
strager
Actually, once you know 3., 2. becomes easy. But you have to know what vtables are.
Eduardo León
@Sesh: How about "1. Use smart pointers, or make sure to delete raw pointers to allocated memory."?
David Thornley
@Sesh + @David: If you're really using C++, you should never be `delete`ing pointers manually (unless you yourself are implementing a smart pointer). Therefore I'd say that itself is a misunderstanding.
Billy ONeal
+8  A: 

Pointers.

Dereferencing the pointers. Through either . or ->

Address of using & for when a pointer is required.

Functions that take params by reference by specifing a & in the signature.

Pointer to pointers to pointers *** or pointers by reference void someFunc(int *& arg)

Tim Matthews
Sorry couldn't figure out SO's escape characters so may appear weird.
Tim Matthews
Here you go. =] Backticks (`) surround inline code.
strager
You can't have pointers to references, they're illegal. You can have references to pointers, though.
Adam Rosenfield
@Adam Rosenfield sorry it was about 2am when I posted this. I have rearanged the last example.
Tim Matthews
+10  A: 

Here are some:

  1. Using templates to implement polymorphism without vtables, à la ATL.
  2. Logical const-ness vs actual const-ness in memory. When to use the mutable keyword.


ACKNOWLEDGEMENT: Thanks for correcting my mistake, spoulson.

EDIT:

Here are more:

  1. Virtual inheritance (not virtual methods): In fact, I don't understand it at all! (by that, I mean I don't know how it's implemented)
  2. Unions whose members are objects whose respective classes have non-trivial constructors.
Eduardo León
Typo : "mutable" instead of "mutabile"
Klaim
I don't understand point 1 – or rather, I understand it, but I don't agree with it, at all.
Konrad Rudolph
i agree with 1., but i don't see how it is a misunderstood concept? it's rather a feature of c++ than a misunderstood thing. can u please add what's misunderstood there?
Johannes Schaub - litb
@Konrad: If you don't agree with point 1, then at least you or the author misunderstood it. That's a 50% misunderstanding. And that's often enough for it to be a good answer to this question. [Maybe my logic's a bit perverse... I think I need sleep].
Scott Langham
Scott i thought about the same, lol. If he says it's a valid point, and Konrad says it isn't, one of them didn't get it :p
Johannes Schaub - litb
+4  A: 

Memory Alignment.

Tim Matthews
I don't think this is a C++ concept per se.
Ismael
+1 because I don't even know what it means or why it matters, but I keep hearing about it
+12  A: 

The static keyword which can mean one of three distinct things depending on where it is used.

  1. It can be a static member function or member variable.
  2. It can be a static variable or function declared at namespace scope.
  3. It can be a static variable declared inside a function.
sharptooth
I can only remember two of those three things: declaring a static method and declaring a static variable, what's the third one?
Eduardo León
I meant, a static member, not necessarily a method.
Eduardo León
Edited to clarify.
sharptooth
Note that usage #2 is deprecated. Unnamed namespaces are preferred to the static keyword for this use case.
Brian Neal
What am I missing? If unnamed namespaces can replace #2 then how do you distinguish between 2 identically named functions/variables that in the past would have different namespaces to delineate between them?
Dunk
Dunk: What do you mean? If they are in an unnamed namespace, they're not visible outside the current compilation unit, so they won't conflict with identically named symbols in other compilation units. And if they're in the same compilation unit, it's a violation of ODR, and illegal.
jalf
what exactly is ODR?
Chris Kaminski
@Chris: http://en.wikipedia.org/wiki/One_definition_rule
Roger Pate
+8  A: 

Here is an important concept in C++ that is often forgotten:

C++ should not be simply used like an object oriented language such as Java or C#. Inspire yourself from the STL and write generic code.

Edouard A.
Why not? It was primarily designed to support the object oriented programming paradigm.
Scott Langham
Because it has no garbage collection. Also, because virtual functions are messed up.Also, it has no strings, so each library ships with its own string class.
hasen j
Because it's more generic oriented that object oriented. See the STL.
Edouard A.
C++ supports OO fine, the difference is that you have to do a lot of headachy work compared to, say C#. This doesn't mean you shouldn't use it for OO... just that its more difficult to do it safely. At least imho
jheriko
Of course it does, but that's not how you should use it. Write templates. Write generic code. Don't limit yourself to OOP. Perhaps this is poorly phrased...
Edouard A.
It is better if you restate your assertion: "C++ is much more than OO, if you look at it just as an OO language, you are missing important features like generic programming".
Ismael
Ismael
The underlying message is that you should not write your programs in C++ like you would in Java an C#.
Edouard A.
I still think looking only at Java or C# is too narrow, but is just my opinion I guess.
Ismael
I don't pretend to be able to sum up all the complexity of C++ in two sentences. ;)
Edouard A.
I actually think the point is well made.
Konrad Rudolph
I agree, the point is well made. C++'s OO features enable it's generic features, so use it's generic features too.
Rob K
I can't believe this got downvoted so many times!!guys, can anyone say with a straight face that you can write C#-Like code in C++????
hasen j
+1 from here. It's a good point. If you just want OOP, there are better languages out there. C++'s strengths is that it allows you to combine OOP with other paradigms, not least generic programming.
jalf
The STL will be the death of C++
jussij
+64  A: 

C++ is not C with classes!

And there is no language called C/C++. Everything goes downhill from there.

Konrad Rudolph
haha, too true. I like the one about C/C++.:)
jalf
There may not be C/C++, but I'm sure there is C/C++/C#!
MiseryIndex
A: 

Very nice resource I can't get tired to promote - C++ Frequently Questioned Answers

Alexander Prokofyev
Very cool link. Never seen this before... but a great read so far. :)
jheriko
That's a terrible link. Someone put a lot of time in that site just to bash a language.
Brian Neal
Somebody put a lot of time in that site just to bash a language, and wound up missing the point far too much. It may be fun to read, but you won't learn much from it.
David Thornley
Totally agree. And he summarizes Marshall Cline's C++ FAQ lite incorrectly all over the place.
Brian Neal
the fqa is written in a style that isn't quite serious. it's just downmodding the c++ language with weird reasoning and blatant words. better recommend the FAQ light instead!
Johannes Schaub - litb
The FQA is true in so many ways. I didn't read all of it but I agree with most of what I read.
hasen j
This is one of the most distorted and useless C++ links I've ever read, and it's sad that it's posted to SO virtually every time the string "C++" is included in a question.
Daniel Daranas
some points he mades in fqa are right i think. but there are so many stupid things he says in that fqa that i would never recommend to read it to anyone oO
Johannes Schaub - litb
C++ people are blinded by love!! lol, guys seriously C++ sucks.
hasen j
+20  A: 

The difference between assignment and initialisation:

string s = "foo";    // initialisation
s = "bar";           // assignment

Initialisation always uses constructors, assignment always uses operator=

anon
I always do initialization with parenthesis (string s("foo");), to help keep the difference obvious. The difference between init and assignment inside a constructor is also misunderstood.
Chris Smith
How is this a "misunderstood concept" ?
Nippysaurus
Another example might be Object o(1); vs Object o = 1; where Object has both a copy constructor and a single argument constructor that takes an int.
James Schek
+7  A: 

There are a few things that people seem to be constantly confused by or have no idea about:

  1. Pointers, especially function pointers and multiple pointers (e.g. int(*)(void*), void***)

  2. The const keyword and const correctness (e.g. what is the difference between const char*, char* const and const char* const, and what does void class::member() const; mean?)

  3. Memory allocation (e.g. every pointer new'ed should be deleted, malloc/free should not be mixed with new/delete, when to use delete [] instead of delete, why the C functions are still useful (e.g. expand(), realloc()))

  4. Scope (i.e. that you can use { } on its own to create a new scope for variable names, rather than just as part of if, for etc...)

  5. Switch statements. (e.g. not understanding that they can optimise as well (or better in some cases) than chains of ifs, not understanding fall through and its practical applications (loop unrolling as an example) or that there is a default case)

  6. Calling conventions (e.g. what is the difference between cdecl and stdcall, how would you implement a pascal function, why does it even matter?)

  7. Inheritance and multiple inheritance and, more generally, the entire OO paradigm.

  8. Inline assembler, as it is usually implemented, is not part of C++.

jheriko
While #6 may be important, it has nothing to do with standard C++. Rather it is an extension for a specific OS.
KeithB
Not to mention that #1, #2, #4, #5, and #8 apply just as much to C.
David Thornley
It is true that calling conventions are implementation specific, but I feel it is still important to understand their usage and why they may prevent code from functioning if not dealt with correctly since every implementation of C++ depends on them.Thanks for the feedback though. :)
jheriko
In-line assembler in the shape of the asm() declaration is part of Standard C++ (7.4/1)
anon
Isn't the language "assembly"? "Assembler" is the tool, like "compiler." =]
strager
@strager either "assembler" or "assembly language" but not "assembly" (speaking from 30 years assembler experience)
anon
thanks for the pointer. fixed #8 a little. :)
jheriko
+9  A: 

a classic among beginners to c++ from c:

confuse delete and delete[]

EDIT:

another classic failure among all levels of experience when using C API:

std::string helloString = "hello world";
printf("%s\n", helloString);

instead of:

printf("%s\n", helloString.c_str());

it happens to me every week. You could use streams, but sometimes you have to deal with printf-like APIs.

davidnr
Looks like that beginner is confusing arrays with std::Vector. ;)
Arafangion
I think beginners usually don't use the stl, just use C++ as C with classes.
davidnr
printf("%s\n", helloString); won't even compile, I predict.
mannicken
visual c++ allows as an extension to pass non-pod classes to var-arg functions, i read. for example passing a CString directly. but i forgot the specific semantics
Johannes Schaub - litb
@mannicken: According to the standard 5.2.2.7: "If the argument [matching an ellipsis] has non-POD class type, the behaviour is undefined."
j_random_hacker
-Wformat is your friend, to prevent this from happening (at least using GCC and probably ICC).
Tom
+14  A: 

The overuse of inheritance unrelated to polymorphism. Most of the time, unless you really do use runtime polymorphism, composition or static polymorphism (i.e., templates) is better.

KeithB
Nice one! Though maybe it's not limited to the C++ users.
xtofl
NOt limited to C++ for sure, although the use of templates gives more options than Java, for instance.
KeithB
Then again, Java does have generics now, but suffers from having to box/unbox primitive types...
Arafangion
Java generics aren't a replacement for the template system in C++. See this question: http://stackoverflow.com/questions/498317/c-templates-and-java-generics-difference
KeithB
This answer was related to *polymorphism*.
Arafangion
A: 

A big one is that the languages are not 100% syntactically compatible. C++ is fully link compatible with C, but some C++ style syntax will generate a complier error in C. Some compilers aren't picky and don't follow the C standard to the letter. The basics of these need to be learned when moving from one language to the other. Note I haven't read the latest C standard, but last I knew this was true.

tkotitan
C++ is a superset of C, so of course C++ code isn't going to pass the C compiler...
xan
Worth noting that sizeof('a') differs in C and C++ (assuming sizeof(int) != 1). This could break compatability for some wacky reason.
strager
strager: True, and there are a handful other incompatibilities as well. C++ isn't a strict superset of C, although it comes close.
jalf
C++ is link compatible with C only if you use extern "C".
Arafangion
@xan: C++ is NOT a superset of C, ever since C99 (thank goodness <stdint.h> usually works anyways - boo to MSVC for ignoring C99).
Tom
+7  A: 
  • Pointers to members and pointers to member functions.
  • Non-type template parameters.
  • Multiple inheritance, particularly virtual base classes and shared base objects.
  • Order of construction and destruction, the state of virtual functions in the middle of constructing an intermediate base class.
  • Cast safety and variable sizes. No, you can't assume that sizeof(void *) == sizeof(int) (or any other type for that matter, unless a portable header specifically guarantees it) in portable code.
  • Pointer arithmetic.
Chris Smith
I've gotten caught by your fourth bullet once or twice before. >_< Note: [u]intptr_t <stdint.h> must at least be the size of a pointer.
strager
A nice, tight collection of often-confused aspects of C++.
j_random_hacker
+2  A: 

C++ is not C with string and vector!

Igor Oks
Ha! A lot of people just use it like C, *without* using string or vector. :) I never want to see a strcmp or strcpy in C++ code again!
Brian Neal
And what if you can't use the stl or boost because of platform restriction, but you need OO? You write your own string library (hopefully) and use strlen etc. internally, or you use char*'s and be done with it.
xan
@xan - why can't you use STL? I can understand about not wanting or being able to run boost, but STL is standard with all major compiler vendors. It is a horrifically bad idea to write your own string library when you can use std::string (even with all its faults).
Brian Neal
Using C++ like C is a valid C++ style. It was the goal of the language to build upon C, not remove it.
cdv
cdv: No it isn't, and no it wasn't.
jalf
+1  A: 
  • That anonymous namespaces are almost always what is truly wanted when people are making static variables in C++
  • When making library header files, the pimpl idiom (http://www.gotw.ca/gotw/024.htm) should be used for almost all private functions and members to aid in dependency management
Spyplane
I'll retort with "believing that anonymous namespaces and the `static` keyword are interchangeable". They get similar results, but using different means, and sometimes you want both. `static` variables don't show up outside of the compilation unit, whereas anonymous-namespaced variables do. That can make a huge difference in shared library name binding performance.
Tom
+18  A: 

The most pernicious concept I've seen is that it should be treated as C with some addons. In fact, with modern C++ systems, it should be treated as a different language, and most of the C++-bashing I see is based on the "C with add-ons" model.

To mention some issues:

While you probably need to know the difference between delete and delete[], you should normally be writing neither. Use smart pointers and std::vector<>.

In fact, you should be using a * only rarely. Use std::string for strings. (Yes, it's badly designed. Use it anyway.)

RAII means you don't generally have to write clean-up code. Clean-up code is bad style, and destroys conceptual locality. As a bonus, using RAII (including smart pointers) gives you a lot of basic exception safety for free. Overall, it's much better than garbage collection in some ways.

In general, class data members shouldn't be directly visible, either by being public or by having getters and setters. There are exceptions (such as x and y in a point class), but they are exceptions, and should be considered as such.

And the big one: there is no such language as C/C++. It is possible to write programs that can compile properly under either language, but such programs are not good C++ and are not normally good C. The languages have been diverging since Stroustrup started working on "C with Classes", and are less similar now than ever. Using "C/C++" as a language name is prima facie evidence that the user doesn't know what he or she is talking about. C++, properly used, is no more like C than Java or C# are.

David Thornley
A C program that compiles as C++ is not normally good C? Nearly all C programs compile as C++, in my experience.
Imbue
Depends on what it does. "int * a = (int *)malloc(...)" isn't really good C, and "int * a = malloc(...)" doesn't compile under C++. Wherever there's compromises, the C++ side is usually not as good C as the C side.
David Thornley
`std::string` isn't badly designed -- it just doesn't have most of it's functionality as part of it's signature. Use STL style algorithms on it instead. Too many C++ programmers look at `std::string` as a crappy version of a Java or C# string, rather than as a full fledged STL container.
Billy ONeal
+9  A: 

Given this:

int x = sizeof(char);

what value is X?

The answer you often hear is dependant on the level of understanding of the specification.

  1. Beginner - x is one because chars are always eight bit values.
  2. Intermediate - it depends on the compiler implementation, chars could be UTF16 format.
  3. Expert - x is one and always will be one since a char is the smallest addressable unit of memory and sizeof determines the number of units of memory required to store an instance of the type. So in a system where a char is eight bits, a 32 bit value will have a sizeof of 4; but in a system where a char is 16 bits, a 32 bit value will have a sizeof of 2.

It's unfortunate that the standard uses 'byte' to refer to a unit of memory since many programmers think of 'byte' as being eight bits.

Skizz

Skizz
Your answer for '3' is wrong... It is true that x is will always be one - but your rationale is wrong! It is defined as one by the spec, not because it is the smallest addressable unit of memory. Some archs probably address bits, while others can't address less than 32 bits - x is still one!
Arafangion
OK, perhaps my wording wasn't great there. However, it's the compiler writer that decides the size of a byte/char, not the hardware. On an IA32, a byte could be 16 bits even though the hardware can address to 8 bit resolution. Obviously, the compiler writer would choose a size that was most efficent
Skizz
You're worrding is weird. You say x is always one, and yet sizeof(char) will return either 2 or 4? I don't grok?
Chris Kaminski
It's even more unfortunate that "char" refers to a unit of memory now that we have Unicode.
dan04
A: 
  1. The difference between pointer (*) and reference (&)
  2. Accesing multi dimentional arrays using pointers.
  3. Using * to acess the value of a pointer - i.e. *ptr = 5
  4. When to release allocated memory.
Dror Helper
+19  A: 

That C++ does have automatic resource management.

(Most people who claim that C++ does not have memory management try to use new and delete way too much, not realising that if they allowed C++ to manage the resource themselves, the task gets much easier).

Example: (Made with a made up API because I do not have time to check the docs now)

// C++
void DoSomething()
{
  File file("/tmp/dosomething", "rb");
  ... do stuff with file...
  // file is automatically free'ed and closed.
}

// C#
public void DoSomething()
{
  File file = new File("/tmp/dosomething", "rb");
  ... do stuff with file...

  // file is NOT automatically closed.
  // What if the caller calls DoSomething() in a tight loop?
  // C# requires you to be aware of the implementation of the File class
  // and forces you to accommodate, thus voiding implementation-hiding
  // principles.
  // Approaches may include:
  // 1) Utilizing the IDisposable pattern.
  // 2) Utilizing try-finally guards, which quickly gets messy.
  // 3) The nagging doubt that you've forgotten something /somewhere/ in your
  //    1 million loc project.
  // 4) The realization that point #3 can not be fixed by fixing the File
  //    class.
}
Arafangion
-1 please explain how and where doe sit have automatic resource management?
hasen j
hasen, search for RAII.
Luc Hermitte
It is ironic that the most correct answers to this question might be modded down the most, _because_ they are more often misunderstood.(That said, I should've mentioned several keywords to help people google the reference, such as RAII and the preference to allocate objects on the stack.)
Arafangion
Well, just edit the post, and justify the answer. imho, C++ really does *not* have automatic resource management, even if its proponents say so.
hasen j
@hasen j: C++ have an *optional* automatic resource management. In C++ the `auto` keyword is the default storage class for (stack-allocated) local variables. Heap-allocation, as marked by using the `new` keyword, are used to mark a resource for manual resource management. However, all too often, a programmer misused the `new` keyword and caused memory leaks; then they claim C++ does not have automatic resource management, when they just have disabled it.
Lie Ryan
+10  A: 

Arrays are not pointers

They are different. So &array is not a pointer to a pointer, but a pointer to an array. This is the most misunderstood concept in both C and C++ in my opinion. You gotta have a visit to all those SO answers that tell to pass 2-d arrays as type** !

Johannes Schaub - litb
+6  A: 

C++ is a multi-paradigm language. Many people associate C++ strictly with OOP.

cdv
+22  A: 

Free functions are not bad just because they are not within a class C++ is not an OOP language alone, but builds upon a whole stack of techniques.

I've heard it many times when people say free functions (those in namespaces and global namespace) are a "relict of C times" and should be avoided. Quite the opposite is true. Free functions allow to decouple functions from specific classes and allow reuse of functionality. It's also recommended to use free functions instead of member functions if the function don't need access to implementation details - because this will eliminate cascading changes when one changes the implementation of a class among other advantages.

This is also reflected in the language: The range-based for loop in C++0x (next C++ version released very soon) will be based on free function calls. It will get begin / end iterators by calling the free functions begin and end.

Johannes Schaub - litb
hey, weren't you arguing for member functions a week or two ago? ;)I agree though. And grats on your gold badge!By the way, isn't it still C++0x? Last I heard, they were still aiming to finalize the spec in 09.
jalf
jalf, well herb sutter said at oct'08 that there will be a second community draft soon at around october'09. then i think they will await comments again and have a meeting, after which finally the new standard will come out in 2010. that's what i heard and why i call it c++1x :)
Johannes Schaub - litb
I absolutely agree. Developers seem to put everything in a class, whether it belongs there or not.also I prefer c++0A instead of c++1X.
caspin
Yes! The idea of moving any method which does not depend on class internals to a free function (in an associated namespace of course) somehow runs contrary to most programmers' "feelings," and I initially felt the same way, but when you think about it it makes perfect sense.
j_random_hacker
It's called Interface Principle and it's the idea of Herb Sutter. It would be nice to give him some credit at least. http://www.gotw.ca/publications/mill08.htm
Piotr Dobrogost
@Piotr, it was not Herb Sutters' article that convinced me that free functions are superior, but it was Scott Meyers' "How non-member functions improve encapsulation" that convinced me: http://www.ddj.com/cpp/184401197 Without having looked up who has had the most influence on this, I don't think this is something you can attribute to some single person. Rather, the mechanism and ideas have grown up in the community as a whole, and all members of it somehow attribute to the things that come out of it.
Johannes Schaub - litb
+2  A: 

C++ is not a typical object oriented language.

Don't believe me? look at the STL, way more templates than objects.

It's almost impossible to use Java/C# ways of writing object oriented code; it simply doesn't work.

  • In Java/C# programming, there's alot of newing, lots of utility objects that implement some single cohesive functionality.
  • In C++, any object newed must be deleted, but there's always the problem of who owns the object
  • As a result, objects tend to be created on the stack
  • But when you do that, you have to copy them around all the time if you're going to pass them around to other functions/objects, thus wasting a lot of performance that is said to be achieved with the unmanaged environment of C++
  • Upon realizing that, you have to think about other ways of organizing your code
  • You might end up doing things the procedural way, or using metaprogramming idioms like smart pointers
  • At this point, you've realized that OO in C++ cannot be used the same way as it is used in Java/C#

Q.E.D.

If you insist on doing oop with pointers, you'll usually have large (gigantic!) classes, with clearly defined ownership relationships between objects to avoid memory leaks. And then even if you do that, you're already too far from the Java/C# idiom of oop.

Actually I made up the term "object-oriented", and I can tell you I did not have C++ in mind.
-- Alan Kay (click the link, it's a video, the quote is at 10:33)

Although from a purist point of view (e.g. Alan Kay), even Java and C# fall short of true oop

hasen j
Which is more expensive - copying memory, or constantly dereferencing references? I'm not sure if I would agree that copying the objects wastes alot of performance gains... If the object is large, though, then optimize it in the class! (Many std::strings use a reference, internally, afaik).
Arafangion
it doesn't matter which choice you follow, either way, you're already too far from the Java/C# idiom of oop.
hasen j
Arguably-what if you added a garbage collector, and only passed around smart pointers?Either way, it doesn't matter-you're abusing RAII, which is C++'s best feature, so you're right, even if someone somehow uses C# ways in C++, you've used the worst of both worlds.
Arafangion
A: 

Why is A[b] the same thing as b[A]?

Ok, not really a COMMON question, but it came up in a class I was teaching once...

Brian Postow
That's the same in C, so not really C++ specific.
André
True, but he never said it had to be C++ specific, just a C++ concept...
Brian Postow
+2  A: 

C structs VS C++ structs is often misunderstood.

turbovince
In what way? I understand that a C++ struct is identical to a class (except for default access specifier), but if you think of it as identical to a C struct, and use class for everything else, I think you end up with clearer code and no confusion.
KeithB
+6  A: 

Headers and implementation files

This is also a concept misunderstood by many. Questions like what goes into header files and why it causes link errors if function definitions appear multiple times in a program on the one side but not when class definitions appear multiple times on the other side.

Very similar to those questions is why it is important to have header guards.

Johannes Schaub - litb
+2  A: 

std::vector does not create elements when reserve is used

I've seen it that programmers argue that they can access members at positions greater than what size() returns if they reserve()'ed up to that positions. That's a wrong assumption but is very common among programmers - especially because it's quite hard for the compiler to diagnose a mistake, which will silently make things "work".

Johannes Schaub - litb
+2  A: 

A pointer is an iterator, but an iterator is not always a pointer

This is also an often misunderstood concept. A pointer to an object is a random access iterator: It can be incremented/decremented by an arbitrary amount of elements and can be read and written. However, an iterator class that has operator overloads doing that fulfill those requirements too. So it is also an iterator but is of course not a pointer.

I remember one of my past C++ teachers was teaching (wrongly) that you get a pointer to an element of a vector if you do vec.begin(). He was actually assuming - without knowing - that the vector implements its iterators using pointers.

Johannes Schaub - litb
@Johannes Schaub: IMO iterator is conceptually a pointer in the sense that an iterator points to a specific object. I agree though, that iterator is not a pointer in the sense of a "memory address".
Lie Ryan
+2  A: 

If a function accepts a pointer to a pointer, void* will still do it

I've seen that the concept of a void pointer is frequently confused. It's believed that if you have a pointer, you use a void*, and if you have a pointer to a pointer, you use a void**. But you can and should in both cases use void*. A void** does not have the special properties that a void* has.

It's the special property that a void* can also be assigned a pointer to a pointer and when cast back the original value is received.

Johannes Schaub - litb
Can you post some examples?
an example use case? `void f(void *p) { int **a = (int**)p; *a = some_int_ptr; }` instead of `void f(void **p);`
Johannes Schaub - litb
+1  A: 

I still don't get why vector doesn't have a pop_front and the fact that I can't sort(list.begin(), list.end())..

I think that's because both would be fairly inefficient. You could probably find lots of explanations of why that is on SO or elsewhere. If you really want to use pop_front, use a deque, and if you really want to sort something, it should be an array or vector (or a tree, or...)
MatrixFrog
+2  A: 

I think the most misunderstood concept about C++ is why it exists and what its purpose is. Its often under fire from above (Java, C# etc.) and from below (C). C++ has the ability to operate close to the machine to deal with computational complexity and abstraction mechanisms to manage domain complexity.

Nikhil
+1  A: 

I know this is old question but I thought object slicing / failure of polymorphism with objects on the stack worth mentioning. If I do not use C++ for six months, this one always comes out and bites me when I use the language again.

#include <iostream>

class base {
    public:
    base(int val) { var1 = val; };
    virtual void doSomething() { var1 *= 2; };
    int getVar1() { return var1; };
    virtual ~base() { };
    protected:
    int var1;
};

class deriv : public base {
    public:
    deriv(int val) : base(val) { };
    void doSomething() { var1 *= 4; };
};

void use_object_ptr(base *arg) {
    arg->doSomething();
    std::cout << arg->getVar1() << std::endl;
};

void use_object_copy(base arg) {
    arg.doSomething();
    std::cout << arg.getVar1() << std::endl;
};

int main(int argc, char **argv) {
    deriv d(42);

    deriv *p2d = new deriv(42);

    use_object_ptr(p2d); // calls deriv::doSomething(), prints 168

    use_object_copy(d); // calls base::doSomething(), prints 84

    use_object_ptr(&d); // calls deriv::doSomething(), prints 168

    return 0;
}
Eric M
By default, always inherit base classes from a noncopyable class. virtual destructor ? => inherit from boost_or_whatever::noncopyable
Luc Hermitte
+1  A: 
  1. Allocation of objects, stack or heap.
  2. Use of const.
  3. Use of friend
Appu
+1  A: 

NULL is always zero.

Many confuse NULL with an address, and think therefor it's not necessarily zero if the platform has a different null pointer address.

But NULL is always zero and it is not an address. It's an zero constant integer expression that can be converted to pointer types.

Johannes Schaub - litb