Friday, June 10, 2011

C++ Advanced Concepts

1) Initialization List
http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/
Also, http://www.cprogramming.com/tutorial/initialization-lists-c++.html:
"Before the body of the constructor is run, all of the constructors for its parent class and then for its fields are invoked. By default, the no-argument constructors are invoked. Initialization lists allow you to choose which constructor is called and what arguments that constructor receives.

If you have a reference or a const field, or if one of the classes used does not have a default constructor, you must use an initialization list."

2) Overloading Function and Copy Constructor
http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/

3) Virtual Constructor Function
See Eckel "Thinking in C++":
"If you want to be able to call a virtual function inside the constructor and have it do the right thing, you must use a technique to simulate a virtual constructor. This is a conundrum. Remember, the idea of a virtual function is that you send a message to an object and let the object figure out the right thing to do. But a constructor builds an object. So a virtual constructor would be like saying, “I don’t know exactly what kind of object you are, but build the right type anyway.” In an ordinary constructor, the compiler must know which VTABLE address to bind to the VPTR, and even if it existed, a virtual constructor couldn’t do this because it doesn’t know all the type information at compile time. It makes sense that a constructor can’t be virtual because it is the one function that absolutely must know everything about the type of the object.

And yet there are times when you want something approximating the behavior of a virtual constructor..."

4) Virtual Destructor
Why we need virtual destructor is exactly because of polymorphism. Consider if we have BaseClass and ChildClass. If we do this:

BaseClass *p = new ChildClass;
delete p;

then we fail to delete the ChildClass object! Although the pointer of the BaseClass can point to an object of the ChildClass, the command delete could only get rid of a BaseClass object - unless it has a virtual destructor!

http://blogs.msdn.com/b/oldnewthing/archive/2004/05/07/127826.aspx

5) Avoiding changing value when passing by reference
Use the technique of passing by const reference (http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference):
"One of the major disadvantages of pass by value is that all arguments passed by value are copied to the parameters. When the arguments are large structs or classes, this can take a lot of time. References provide a way to avoid this penalty. When an argument is passed by reference, a reference is created to the actual argument (which takes minimal time) and no copying of values takes place. This allows us to pass large structs and classes with a minimum performance penalty.

However, this also opens us up to potential trouble. References allow the function to change the value of the argument, which in many cases is undesirable. If we know that a function should not change the value of an argument, but don’t want to pass by value, the best solution is to pass by const reference."

cf. http://iagtm.blogspot.com/2010/02/c-function-passing-by.html

No comments:

Post a Comment