More effective Flashcards

1
Q

When to use pointers and when references?

A

Pointers: null is possible and legit, pointee changes.
References: always not null, refers to the same object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Benefits of using C++ style casts?

A
  1. clear stand out in the code, can be found easily by human or tools.
  2. easy diagnostic of cast problems.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Passing polymorphic objects by value…

A

Don’t pass polymorphic objects by value. Never store them in arrays.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why avoid default constructors if you don’t need them?

A

If an object makes no sense without initializing it with some values the class should not have the default ctor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Problems for objects of classes without the default ctor?

A
  1. Creation of arrays.
  2. Cannot be used with many STL containers.
  3. Virtual base class w/o the def. ctor is the pain in the : most derived class must provide arguments for the virtual base class ctor.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What functions allow perform implicit type conversions of classes?

A
  1. Single-argument ctor (or multi-arg with defaults).
  2. Implicit type conversion operators, eg

operator double() const;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Usually don’t provide type conversion functions at all.

A
  1. Unexpected conversion to other type if you forget to provide the correct function (no error is reported but conversion is used implicitly).
  2. Mistypes (errors) can be hidden by using implicit type conversions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to prevent implicit type conversions?

A
  1. Use the “explicit” keyword.

2. Don’t implement the implicit type conversion operators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

If class A can be implicitly converted to class B and B can be converted to C will A be implicitly convertible to C?

A

No.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why ++i is preferred over i++?

A

i++ needs to create a temp object to store the old value which makes it inefficient if the old value isn’t needed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you define the ++i operator?

A
class Class{
  Class& operator++();
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you define the i++ operator?

A
class Class{
  const Class operator++(int);
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What type should ++i operator return to be consistent with int?

A

A reference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What type should i++ operator return to be consistent with int?

A

A const object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What should i++++ return?

A

It shouldn’t compile: i++++ (i++)++ but i++ returns a const object and cannot be incremented.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you ensure that i++ and ++i operators do increment consistently?

A

Write i++ in terms of ++i. (It is also easier to maintain).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Why should you never overload operators &&, ||, and ,(comma)?

A

There are special rules for use of these operators with the built-in types: short circuit (&&, ||) and left-to-right evaluation (,). Overloading replaces the operators with function (method) calls which cannot reproduce the built-in behaviour. This would be a surprise for a programmer,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the new operator?

A

It’s a built-in operator which does two things:
1. Allocates enough memory using
void *operator new(size_t);
2. Calls a constructor to initialize an instance at the allocated memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the operator new?

A

It is the overloadable operator which allocates the memory:

void *operator new(size_t);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Can operator new have more than 1 argument?

A

It can. But the first one must have type size_t.
void *operator new(size_t a1, T2 a2, T3 a3);
To use:
void *raw = operator new(sizeof(T1), a2, a3);
or:
T1 *p = new(a2,a3) T1(…);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is placement new?

A

It’s a version of operator new with 2 arguments: the second arg has type void*. It doesn’t allocate any memory only returns the value of its second argument:
void *operator new(size_t void *loc){
return loc;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What does delete operator do?

A

Calls the destructor then releases the memory:
T *p; … ; delete p;

T *p; … ; p->~T(); operator delete(p);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How do you declare operator delete for overloading?

A

void operator delete(void *loc);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Why exceptions are better than error codes?

A

Exceptions cannot be ignored.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

A good rule to prevent resource leaks in case of exceptions.

A

Use the smart pointers.

26
Q

Can auto_ptr - like objects be used with arrays?

A

No, because they use delete (not delete[]) in their destructors.

27
Q

Is it safe to delete a NULL pointer?

A

Yes, C++ guarantees that.

28
Q

If a constructor throws an exception will the destructor be called?

A

No. C++ destroys only fully constructed objects.

29
Q

What is the exception safe way to design constructors?

A
  1. Catch all possible exceptions.
  2. Clean up.
  3. Re-throw the exceptions.
30
Q

Why mustn’t the destructor throw?

A
  1. If a destructor throws when another exception is active (uncaught) then C++ calls terminate(). All open resources are lost.
  2. The destructor won’t finish ==> resource leak.
31
Q

A good rule for destructor exception safety.

A

Make sure the catch-blocks in the destructor themselves don’t throw: better leave them empty than let them throw.

32
Q

Is there a similarity between throwing an exception and calling a function?

A

Yes. Throwing is similar to calling a function with one argument - the exception object. A catch block is similar to a function definition.

33
Q

What is the difference between a function call and throwing and catching an exception?

A

When an exception is thrown with
throw localObject;
the thrown object is copied and the catch block gets its copy. So there is no way of changing the original object even if the exception gets caught by non-cost reference.

34
Q

What is the difference between
1. catch(T& w){ throw; }
and
2. catch(T& w){ throw w; } ?

A

In (1) the exception is re-throw meaning that it gets passed as it is. If w’s type is T1 inherited from T then T1 is re-thrown.
In (2) a copy of w is thrown. If the dynamic type of w is T1 the thrown object will have type T not T1.

35
Q

What implicit conversions can be applied when testing an object in a catch block?

A
  1. Inheritance based: test for a parent type catches a child type.
  2. If a pointer is thrown then catch(void*){…} catches pointers of any type.
  3. No other implicit conversions are tried: built-in or user defined.
36
Q

How can a series of catch clauses trying for types from the same inheritance tree be compared with a virtual method call?

A

The exceptions are tried in the order of the catch blocks in the source code while a virtual method uses the closest match type.

37
Q

Why should exceptions be caught by reference?

A
  1. Pointers are bad because you don’t know whether to delete it afterwards or not. Plus some standard exceptions cannot be caught by pointer: bad_alloc, bad_cast, bad_typeid, bad_exception.
  2. Values are bad because of “slicing” if caught via the base class. Plus, it involves additional copying.
38
Q

What are the exception specifications good for?

A
  1. Documentation of types of exceptions a function may throw.
  2. If the specification violation is unacceptable and the program must terminate - C++ does this by default.
39
Q

What are the drawbacks of exception specifications?

A
  1. Compilers cannot detect all possible specification violations especially when 3rd party libraries are used (they may not use the specs at all or violate them themselves) or registered callback mechanism is used (a callback may not use or violate the specs).
  2. Exc. specs can prevent the higher level code from handling exceptions even if it’s able to do it.
40
Q

What is the 80-20 rule?

A

The overall (80%) performance of the software is almost always determined by a small part (20%) of its constituent code.

41
Q

What is the extended version of the 80-20 rule?

A

80% of program’s resources are used by about 20% of the code:

  • 80% of the runtime is spent in 20% of the code;
  • 80% of the memory is used by 20% of the code;
  • 80% of the disk accesses are preformed for 20% of the code;
  • 80% of the maintenance effort is devoted to 20% of the code;
42
Q

What is a bottleneck?

A

A piece of code within a program (~20%) that slows it down (or gives other performance problems: too much memory/disk space/bandwidth is used).

43
Q

How to find a bottleneck?

A

Use a profiler. Measure the right quantity (time, etc). Run the profiler with data sets that cover 80% of the actual user cases.

44
Q

What is lazy evaluation?

A

Performing computations only at the point where their results are needed. If the results are never required, the computations are never performed.

45
Q

What is the alternative to lazy evaluation?

A

Eager evaluation.

46
Q

Over-eager evaluation

A

Doing things before you are asked to do them because you know that you will almost certainly have to.

47
Q

What are the techniques of the over-eager evaluation?

A

Caching and prefetching (from a database for example).

48
Q

When are temporary objects created by C++?

A
  1. When an implicit type conversion is applied to make a function call succeed.
  2. When functions return objects.
49
Q

What is “return value optimization”?

A

It’s a optimization allowed to C++ compilers to eliminate temporary objects when a function returns an object by value. It is possible only if the function returns a temporary object (not a local variable).

50
Q

Which operator is more efficient, += or + ?

A

+=. Because it avoids creation of temporary objects.

51
Q

Should you provide only assignment versions of operators (like +=)?

A

No. The stand-alone versions (like +) should also be provided giving the user a choice between efficiency (+=) and convenience (+-versions are easier to read, debug and maintain).

52
Q

What are “virtual constructors”?

A

Functions returning pointers to objects of different dynamic types depending on the argument values.

53
Q

What is a “virtual copy constructor”?

A

A virtual method like colne().

54
Q

What is multiple dispatch?

A

A function call virtual on multiple parameters. Not supported by C++.

55
Q

Why would you want to avoid having a virtual destructor?

A

To make objects smaller.

56
Q

What does dynamic_cast(p) do?

A

It returns a pointer to the beginning of the memory for the object. It can be different from p is the class has multiple or virtual base classes

57
Q

How to prevent objects to be created on the stack?

A

Make the destructor private. Create them on the heap with a friend function.

58
Q

How to prevent creating objects on the heap?

A

Make operators new and delete private.

59
Q

What is partial assignment?

A

It’s a type of slicing when assignment is done via a base class pointer: only the base class part is copied.

60
Q

How to prevent function name mangling?

A

Enclose the function declarations in extern “C” {…}

61
Q

What is static initialization (destruction)?

A

It’s a process of initialization (destruction) of static class objects and objects at global, namespace and file scope. Initialization is done before any user code in main() function. Static destruction is done after the last user code in main().

62
Q

What to remember if you want to mix C and C++ code?

A
  1. Make sure the C++ and C compilers produce compatible object files.
  2. Declare functions to be used by both languages extern “C”.
  3. Write main() in C++.
  4. Always use delete with memory from new; always use free() with memory from malloc().
  5. Limit what you pass between the two languages to data structures that compile under C; the C++ version of structs may contain non-virtual member functions.