0. Why do your source code snippets look so bad?
Because you disabled scripts. Which is a good thing. Usually.

1. What’s delete[] used for?
It’s used for new[], as in

char *s = new char[80];
delete[] s;

because a “plain” delete won’t do.

2. What’s the meaning of new[0], as in …?

char *s = new char[0];

Beats me. But you had better delete[] it. [22]

3. What if I call delete twice on the same pointer? [5]
Don’t do that. It may try to call the destructor and free the same memory twice.

4. What if I call delete this; somewhere in my class? [21]
Suicide is usually not the answer. Don’t do that.

5. What if I call new twice on the same pointer?
Don’t do that. For every new there must be a delete.

(And if you think you have a smart idea, see question 3.)

6. What if I call new on a pointer, then change the pointer’s value?
Don’t do that. delete may do crazy things later.

7. Why isn’t the Derived class’ destructor called when I do polymorphism?
The Base class’ destructor must be declared virtual. [20]

8. How should I handle errors in destructors?
You don’t. In particular, don’t throw exceptions. Because your destructor itself may have been called as the result of a previous exception being thrown. [15]

9. Wow, six flavors of casts! Which one to use? [2] [3]
You shouldn’t use any of them, if you can help it.

10. Then how else can I subvert the type system?
It already does a good job at subverting itself, by implicit conversions. But if that’s not enough, use macros, or better yet templates (which are considered to be type-safe).

11. What’s the meaning of “type-safe”, as in code? [11]
It means the compiler does less implicit conversions without complaining. So you’re forced to get the type right. Kind of.

12. I just realized that the templates’ purpose really is to punch a hole through the type system. So how exactly does type safety apply to them?
Well type safety and type agnosticism aren’t mutually exclusive. Or are they? [13] [14]

13. What’s an explicit constructor? [4]
A constructor taking only one parameter can be used implicitly, for implicit conversions. Declaring such a constructor as explicit will prevent that.

14. What’s the “Most Vexing Parse” problem? [8]
It’s this:

TypeA object(TypeB());

It looks like a constructor call for a TypeA object, which gets a temporary TypeB parameter, but C++ considers it to be a function declaration. Fortunately there are ways to “fix” this.

TypeA object((TypeB())); // C++03 style
TypeA object{TypeB()};   // C++11 style, initializer list

15. What is the “Rule of Three”? [9]
It’s that if you need to write a custom destructor you probably also need to write a custom copy constructor and assignment (copy) operator. In C++11 you may also need to write a custom move constructor and move operator.

16. What’s an unnamed namespace? [12]
It’s… a namespace without a name. It makes its contents “invisible” outside the current file. In C, the keyword static has a similar role when applied to functions and global variables.

17. Must main() have a return statement?
No. It will

return 0;

by default if you omit the return statement. [19]

18. Why the archaic int main(int, char**), and not int main(std::list<std::string>&)?
Because std::list and std::string are templates which are part of the language’s library, and are not built-in into the language itself, duh.

19. Why do some people keep writing std:: over and over, instead of using a single …?

using namespace std;

The official answer is “to prevent namespace pollution”. [7] Unofficially, because it looks cool and professional.

20. What’s the difference between a class and a struct?
By default, a class has all members private, while a struct has all members public. They also inherit differently “by default”. Otherwise there’s no difference.

21. How come, no matter how many functions I add to myClass, sizeof (myClass) remains the same?
Because you’re adding non-virtual functions. If you add virtual functions, a hidden virtual function table (the “vtable”) would be added as a member variable, and increase the size.
The reason why “regular” functions don’t increase the size is that they’re not part of the class, to begin with. Under the hood, they simply receive a hidden parameter, pointing to the object they must work on. [1]

22. How can I make myClass printable to a file stream (such as std::cout)?
Overload the bitshift left operator:

std::ostream & operator << (std::ostream &, const myClass &);

as global function (and maybe add as friend to your class). [6]

23. How should I read binary files?
Since C++ doesn’t provide support for memory mapped files yet, you could use an

std::vector<char> buffer;

and read into it with

inputFile.read(&buffer.front(), buffer.size());

Another way is to read variables one by one:

float f;
inputFile.read(reinterpret_cast<char *> (&f), sizeof f); // or
inputFile.read(reinterpret_cast<char *> (&f), sizeof (float));

24. What’s a lambda function?
It’s a new feature of C++11, which allows you to define a function “where needed”. Lambdas are most convenient for use with the STL algorithms, some of which require being fed a function to work.
What does this question do here? Well lambda’s syntax is… weird, to say the least. [10]

25. How can I pass around a reference to an array? [16]
Are you sure about this?

// declaring
int ia[50]; // array
int (&ria)[50] = ia; // reference to array

// returning from a function
// (feeding to a function looks the same as declaring)
int (&get_ref_arr())[50]
    static int ia[50];
    return ia;

26. What’s your least favorite C++ feature?
There are so many to choose from, but I have to say it’s the alternative operator syntax:

#include <iostream>

int main()
    operator<<(std::cout, "Hello, World!")), "Nice day to C++!").

27a. Why do some people seem to hate me for using system("PAUSE");?
It’s a pet peeve that makes them feel superior to you. It is bad practice to use system() calls in production code. But if you’re only learning the language and experimenting, using system("PAUSE"); fits perfectly fine. The alternative would be what I call “input stream hackery”, dubious usage of cin.get() and cin.ignore(), which are called to have the side effect of pausing the program until the user hits Enter.

// in the end, it's your choice!
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// vs

27b. So what’s The Right Way to do things?
If your IDE doesn’t pause the output automatically (as it should), start up the Command Prompt (cmd.exe or command.com) and run your program in it. [18] Thus, pausing will no longer be necessary. If you’re using Linux, you have to run the program in the Shell anyway because it won’t create its own output window like in Windows. If the Command Prompt looks too ugly for you, check out the Console project. [17]


[1] http://en.wikipedia.org/wiki/Virtual_method_table
[2] http://www.cplusplus.com/doc/tutorial/typecasting/
[3] http://en.cppreference.com/w/cpp/language/dynamic_cast
[4] http://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-in-c-mean
[5] http://www.parashift.com/c++-faq-lite/freestore-mgmt.html
[6] http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.8
[7] http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5
[8] http://en.wikipedia.org/wiki/Most_vexing_parse
[9] http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29
[10] http://www.cprogramming.com/c++11/c++11-lambda-closures.html
[11] http://en.wikipedia.org/wiki/Type_safety
[12] http://stackoverflow.com/questions/154469/unnamed-anonymous-namespaces-vs-static-functions
[13] http://en.wikipedia.org/wiki/Concepts_%28C%2B%2B%29
[14] http://www.devx.com/cplus/Article/42365
[15] http://yosefk.com/c++fqa/exceptions.html#fqa-17.3
[16] http://heifner.blogspot.ro/2005/06/c-reference-to-array.html
[17] http://sourceforge.net/projects/console/
[18] http://www.computerhope.com/issues/chusedos.htm
[19] http://stackoverflow.com/questions/22239/why-does-int-main-compile
[20] http://yosefk.com/c++fqa/inheritance-virtual.html#fqa-20.7
[21] http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.15
[22] http://stackoverflow.com/questions/1087042/c-new-int0-will-it-allocate-memory