introduction to effective c++ programming kwanghee ko design laboratory department of ocean...

14
Introduction to Introduction to Effective C++ Effective C++ Programming Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

Upload: arleen-little

Post on 26-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

Introduction to Effective C++ Introduction to Effective C++ ProgrammingProgramming

Kwanghee Ko

Design LaboratoryDepartment of Ocean Engineering

Massachusetts Institute of TechnologyDay 3

Page 2: Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

General GuidelinesGeneral Guidelines

Avoid returning “handles” to internal data.Avoid member functions that return non-

const pointers or references to members less accessible than themselves.

Never return a reference to a local object or to a dereferenced pointer initialized by new with in the function (memory leakage).

Postpone variable definitions as long as possible.

Page 3: Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

General GuidelinesGeneral Guidelines

Public inheritance models : “isa” relation?– Ex. A bus is a vehicle?

Never redefine an inherited non-virtual function.

Never redefine an inherited default parameter value.

Avoid cast down the inheritance hierarchy.Model “has-a” or “is-implemented-in-

terms-of” through layering.

Page 4: Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

General GuidelinesGeneral Guidelines

Use multiple inheritance judiciously.

Page 5: Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

Ambiguities under Multiple Ambiguities under Multiple InheritanceInheritance

Case 1

Base 1

f(); g();

Base 2

f(); h();

Derived

j(); k();

Derived d;

d.g(); // OK

d.h(); // OK

d.f(); // Ambiguous!!!

d.Base1::f(); // OK

d.Base2::f(); // OK

Page 6: Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

Ambiguities under Multiple Ambiguities under Multiple InheritanceInheritance

Case 2

Left

int y;

Right

int z;

Bottom

int a

Top

int x;

Page 7: Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

Ambiguities under Multiple Ambiguities under Multiple InheritanceInheritance

Case 2

Left

int y;

Right

int z;

Bottom

int a

Default inheritance mechanism -> maintains separate copies of the data members inherited from all base classes.

Top

int x;

Page 8: Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

Ambiguities under Multiple Ambiguities under Multiple InheritanceInheritance

Case 2 (Non-virtual Base class)

Left

int y;

Right

int z;

Bottom

int a

Top

int x;

Top

int x; Top

int x;

Top

int x;

Left

int y;

Right

int z;

Bottom

int a

Page 9: Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

Ambiguities under Multiple Ambiguities under Multiple InheritanceInheritance

Case 2 : Virtual Base Class

Left

int y;

Right

int z;

Bottom

int a

Top

int x;

virtual virtual

class Left::public virtual Top{…}class Right::public virtual Top{…}

Top

int x;Left

int y;

Right

int z;

Bottom

int a

Page 10: Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

Ambiguities under Multiple Ambiguities under Multiple InheritanceInheritance

Case 2 : Virtual Base Class

Left

int y;

Right

int z;

Bottom

int a

Top

int x;

virtual virtual

Inherently ambiguous!!!

Ex) Bottom b;

b.x -> b.Left::x?

b.Right::x?

b.Top::x?

Page 11: Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

Ambiguities under Multiple Ambiguities under Multiple InheritanceInheritance

Case 2 : Virtual Base Class

Left

int y;

Right

int z;

Bottom

int a

Top

int x;

virtual virtual

Assignment for Top::x happens twice.

- Bottom->Left->Top

- Bottom->Right->Top

Page 12: Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

Ambiguities under Multiple Ambiguities under Multiple InheritanceInheritance

Case 2 : Virtual Base Class

Left

int y;

Right

int z;

Bottom

int a

Top

int x;

virtual virtual

Assignment for Top::x happens twice.

- Bottom->Left->Top

- Bottom->Right->Top

Solution???

Page 13: Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

General GuidelinesGeneral Guidelines

Use multiple inheritance judiciously. Before using virtual base classes, understand them

thoroughly.– Use an experimental program to understand its

behavior. If a public base class does not have a virtual

destructor, no derived class should have a destructor.

If a multiple inheritance hierarchy has any destructors, every base class should have a virtual destructor.

Page 14: Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

General GuidelinesGeneral Guidelines

Declare a copy constructor and an assignment operator for classes with dynamically allocated memory.

Prefer initialization to assignment in constructors. List members in an initialization list in the order in

which they are declared. Make sure base classes have virtual destructors. Have operator= return a reference to *this. Assign to all data members in operator=. Check for assignment to self in operator=. Overloading vs. default argument.