Introduction to Effective C Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Effective C Programming

Description:

Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3 – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 15
Provided by: khko
Learn more at: http://deslab.mit.edu
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Effective C Programming


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

2
General 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.

3
General 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.

4
General Guidelines
  • Use multiple inheritance judiciously.

5
Ambiguities under Multiple Inheritance
  • Case 1

Derived d d.g() // OK d.h() // OK d.f()
// Ambiguous!!! d.Base1f() // OK d.Base2f()
// OK
Base 1 f() g()
Base 2 f() h()
Derived j() k()
6
Ambiguities under Multiple Inheritance
  • Case 2

Top int x
Left int y
Right int z
Bottom int a
7
Ambiguities under Multiple Inheritance
  • Case 2

Default inheritance mechanism -gt maintains
separate copies of the data members inherited
from all base classes.
Top int x
Left int y
Right int z
Bottom int a
8
Ambiguities under Multiple Inheritance
  • Case 2 (Non-virtual Base class)

Top int x
Top int x
Top int x
Top int x
Left int y
Right int z
Left int y
Right int z
Bottom int a
Bottom int a
9
Ambiguities under Multiple Inheritance
  • Case 2 Virtual Base Class

class Leftpublic virtual Top class
Rightpublic virtual Top
Top int x
virtual
virtual
Top int x
Left int y
Right int z
Left int y
Right int z
Bottom int a
Bottom int a
10
Ambiguities under Multiple Inheritance
  • Case 2 Virtual Base Class

Top int x
Inherently ambiguous!!! Ex) Bottom b b.x
-gt b.Leftx? b.Rightx?
b.Topx?
virtual
virtual
Left int y
Right int z
Bottom int a
11
Ambiguities under Multiple Inheritance
  • Case 2 Virtual Base Class

Top int x
  • Assignment for Topx happens twice.
  • Bottom-gtLeft-gtTop
  • Bottom-gtRight-gtTop

virtual
virtual
Left int y
Right int z
Bottom int a
12
Ambiguities under Multiple Inheritance
  • Case 2 Virtual Base Class

Top int x
  • Assignment for Topx happens twice.
  • Bottom-gtLeft-gtTop
  • Bottom-gtRight-gtTop

virtual
virtual
Left int y
Right int z
Solution???
Bottom int a
13
General 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.

14
General 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.
Write a Comment
User Comments (0)
About PowerShow.com