keywords:
Bookmark and Share



Front Back
How could C++ be viewed as a "federation of languages"?
C++ is comprised of primarily four sub-languages: 1. C 2. Object Oriented C++ 3. Template C++ 4. STL Rules may change as you pass from one language to another. Knowing the behavior of the four parts that make up C++ can improve efficiency.
What are some better alternatives to #define?
const, enum, inline

Using const, enum and inlined template functions instead of #define allows for more robust typechecking.
Why should const be used whenever possible?
It decreases the possibility of misusing values.
What is an initialization list?
It is an method of initializing member variables in the constructor, and is often the most efficient method. For example, AClass::AClass() classVar1(5), classVar2("set") { }
When must an initialization list be used?
When a class has const or reference variables.
What is often the most efficient method of passing a built-in type?
Pass by value
What is usually the best way to pass an object?
Pass by reference to constant.
What is the best method for passing STL iterarors and function objects?
Since iterarors and function objects are modeled on pointers in C, the C pass-by-value rule applies.
If const appears before the pointer symbol, what does it mean?
It means the object pointed to is constant.
If const appears after the pointer symbol, what does it mean?
It means the pointer itself is constant.
If const appears before the type, what does it mean?
The same as if you put the const after the type.
What is bitwise constness?
No bits were changed.
What is logical constness?
The constness created using the keyword "mutable".
In what order are variables listed in an initialization list initialized?
In the order they are declared in the class, starting with base classes.
What is the initialization order of non-local static objects defined in different translation units?
It is undefined.
Describe what a non-local static object is.
A static object that is not declared inside of a function, such as static class and global variables.
What is a translation unit?
A set of source code giving rise to a single object file.
What functions will C++ silently create for user defined classes if necessary?
Default constructor, copy constructor, destructor, and copy assignment operator. Copy assignment operators are only created if every data member of the class has an appropriate copy assignment operator.
How can you prevent the compiler from generating unwanted standard compiler-generated functions?
Define them yourself, but define them in the private section of the class and do not use them.
(7) What happens when a derived class object is deleted through a pointer to a base class?
If the base class has a virtual destructor, the derived object will be destroyed properly. If not, the results are undefined. Often, the derived portion of the class is never destroyed.
(8) Why should destructors never emit exceptions?
It creates the possibility of two or more active exceptions due to the fact that destructors can be called during exception unwinding. It is programmically illegal to have two active exceptions.
(9) Why should you never call a virtual function during construction or destruction?
Such calls will never go to a more derived class than that of the currently executing constructor or destructor.
(10) Why would it be a good idea to return a reference to "*this" in an assignment operators?
Often assignment operators are chained, for example, "x=y=5;".
(11) How should a copy-to-self be checked and handled?
Detect the situation at the start of the function, or perform a copy and swap.
(13) The delete function should only appear in what type of classes?
Object management classes.
(13) What does RAII stand for and mean?
"Resource acquisition is initialization". An object's resources are acquired in the object's construction and released in the object's destruction.
(13) Give an example of an RAII object?
std::auto_ptr
(14) What are some suggested ways of dealing with copying resource-managing objects?
* Prohibit copying. * Reference-count the underlying resources. * Copy the underlying resourcess. * Transfer ownership of the underlying resources.
(16) What would be the proper way of deleting "intList = new int[5];"?
delete[] intList;
What order do compilers execute procedures contained in a parameter list?
Undefined.
In a class, what should be the visibility of data members?
Private
Why should non-member non-friend functions be preferred over other types of functions?
These are the most encapsulated types of functions possible.
(25) What is the "pimpl idiom", and what is it's purpose?
"Pointer-to-implementation". A pointer in the class points to all the data used by the class. This improves encapsulation.
What is the disadvantage of declaring a variable inside of a loop?
The variable will be constructed and destructed every iteration of the loop.
What is the advantage of defining a variable as late as possible?
Decreases possible misuse, and eliminates unnecessary construction/destruction, since the variable is not constructed/destructed until it is encountered in the code.
What are the four C++ style typecastings and what are thier purposes?
1. static_cast - A normal conversion between types. 2. const_cast - Removes the constness of a variable. 3. dynamic_cast - Converts and runtime checks the conversion of a pointer from one type to another. 4. reinterpret_cast - A forceful cast from one type to another.
Why is it poor practice to return "handles" (pointers, refereneces, and handles) to object internals (private data members)?
This breaks the encapsulation imposed by declaring the variable private.
What does inlining guarantee?
Absolutely nothing.
Where are inline functions written?
In the header file.
What will happen if a class that inherits from a base class has a variable with the same name as a variable in the base class?
Storage will be allocated for both variables, yet the base class's variable will not be accessible.
Give an example of inheritance of interface and mandatory implementation.
Inherriting a non-virtual function.
Name a design pattern that can replace virtual functions.
Bridge. A bridge defines an interface, and a pointer to an implementation, the same basic functionality of a virtual function.
What happens if a derived class defines a function with the same signature as an inherited function?
The function called will depend on the type of the pointer used.
What happens if a derived class redefines an inherited function's default parameters?
Nothing, besides confusion.� The function will always use the base class's default parameters.
Composition should model what type of relationship?
A "has-a" or "is-implemented-in-terms-of" relationsip.
Public inheritance should model what type of relationship?
An "is-a" relationship.
Private inheritance should model what type of relationship, and when should it be used?
An "is-implemented-in-terms-of" relationship.  This relationship is similar to composition, but allows the inherited class access to protected data members.
Why should an inherited non-virtual function not be redefined?
Which function is called depends on the pointer to the object, therefore, behaivor is unpredictable.
If a pointer of a base class type points to a derived class, and a virtual function in a base class gives a default value for a parameter, and the derived class gives a different default value for the same parameter, which value will be used.
C++ statically binds default parameter values for runtime efficiency, therefore, the base class pointer will use the base class defined default value.
What is "composition", and what situation does it model?
Composition is containing one class as an element of another, rather than inheriting from another class.  This models a "has-a" or "is-implemented-in-terms-of".
x of y cards Next >|