Flylib.com

Books Software

 
 
 

FAQ 19.15 How do objects of a class receive stream input?

FAQ 19.15 How do objects of a class receive stream input?

Objects of a class normally receive stream input via a friend function called operator>> . Here is an example of such a friend function.

#include <iostream>
using namespace std;

class Fred {
public:
  friend istream& operator>> (istream& istr, Fred& x) throw();
protected:
  int i_;
};

istream& operator>> (istream& istr, Fred& x) throw()
{
  istr >> x.i_;
  return istr;
}

The Fred argument of operator>> is passed by reference (as opposed to const reference). This allows operator>> to change the caller's Fred , which is, of course, the whole point of stream input.

Chapter 20. Constructors and Destructors

FAQ 20.01 What is the purpose of a constructor?

FAQ 20.02 What is C++'s constructor discipline?

FAQ 20.03 What is the purpose of a destructor?

FAQ 20.04 What is C++'s destructor discipline?

FAQ 20.05 What happens when a destructor is executed?

FAQ 20.06 What is the purpose of a copy constructor?

FAQ 20.07 When is a copy constructor invoked?

FAQ 20.08 What is the "default constructor"?

FAQ 20.09 Should one constructor call another constructor as a primitive?

FAQ 20.10 Does the destructor for a derived class need to explicitly call the destructor of its base class?

FAQ 20.11 How can a local object be destructed before the end of its function?

FAQ 20.12 What is a good way to provide intuitive, multiple constructors for a class?

FAQ 20.13 When the constructor of a base class calls a virtual function, why isn't the override called?

FAQ 20.14 When a base class destructor calls a virtual function, why isn't the override called?

FAQ 20.15 What is the purpose of placement new ?

FAQ 20.01 What is the purpose of a constructor?

The constructor turns a pile of incoherent, arbitrary bits into a living object. It initializes the object's internal data members , but it may also allocate resources (memory, files, semaphores, sockets, and so on). The word "ctor" is shorthand for the word "constructor."

The constructors for class X are member functions named X . Here is an example.

class Battery {
public:
  Battery(int initialCharge) throw();
  void drain() throw();
protected:
  int charge_;
};

Battery::Battery(int initialCharge) throw()
: charge_(initialCharge) { }

void Battery::drain() throw()
{
  charge_ -= 5;
  if (charge_ < 0)
    charge_ = 0;
}

int main()
{
  Battery yourDiscountBattery(20); //A Battery object
  Battery myNameBrandBattery(30);  //Another Battery object
}

There can be more than one constructor for a class. Each constructor has the same name , so the compiler uses their signatures to uniquely identify them.

FAQ 20.02 What is C++'s constructor discipline?

A constructor is automatically called at the moment an object is created.

Descartes said, "I think, therefore I am." The C++ variation of Descartes' statement is "I am, therefore I can think." In other words, every object that exists ("I am") has been initialized by one of the class's constructors ("I can think"). Except for pathological cases, by the time an object is accessible, it has already been initialized by its constructor.

The developers of a class provide a set of constructors that define how objects of that class can be initialized. When users create objects of that class, they must provide arguments that match the signature of one of the class's constructors. Constructors enhance encapsulation since they force users to create objects in one of the officially supported ways. Users cannot initialize an object's state directly, because this might place the object in an incoherent or illegal state.

In the example from the previous FAQ, the constructor for class Battery is the member function Battery::Battery(int initialCharge) . This constructor initializes the protected: data member charge_ to the value passed as the parameter to the constructor. The constructor is called twice in main() , once when yourDiscountBattery is created and once when myNameBrandBattery is created.