Effective C++ Ed. 3rd: Item 43: Know how to access names in templatized base classes

In derived class templates, refer to names in base class templates via
  1. a "this->" prefix
  2. using declarations or
  3. an explicit base class qualification like:
    Base::func()
The 3rd option should not be used in case of virtual functions.
prev | next

Effective C++ Ed. 3rd: Item 42. Understand the two meanings of typename

What's the difference between the following two declarations:
template<class T> class Widget;                 // uses "class"

template<typename T> class Widget;              // uses "typename"

When declaring template parameters, class and typename are interchangeable.
But use typename to identify nested dependent type names, except in base class lists or as a base class identifier in a member initialization list.
prev | next

Effective C++ Ed. 3rd: Item 41. Understand implicit interfaces and compile-time polymorphism

  • Object-oriented programming revolves around explicit interfaces and runtime polymorphism.
  • In template and generic programming, these are less important. Instead, implicit interfaces and compile-time polymorphism move to the fore.

Effective C++ Ed. 3rd: Item 40. Use multiple inheritance juriciously

With multiple inheritance (MI), you may have a class hierarchy with more than one path between a base class and a derived class. For example:
class basic_ios { ... };

class basic_istream: public basic_ios { ... };

class basic_ostream: public basic_ios { ... };

class basic_iostream: public basic_istream, public basic_ostream
{ ... };
To restrict inclusion of members of class basic_istream twice into class basic_iostream, use virtual inheritance. Declare the above classes as:
class basic_ios { ... };

class basic_istream: virtual public basic_ios { ... };

class basic_ostream: virtual public basic_ios { ... };

class basic_iostream: public basic_istream, public basic_ostream
{ ... };
But try to avoid virtual inheritance because:
  1. objects created using virtual inheritance are generally larger.
  2. Access to data members in virtual base classes is also slower than those in non-virtual base classes.
Cheers

Effective C++ Ed. 3rd: Item 39. Use private inheritance judiciously

A class "is-implemented-in-terms-of" might be achieved by:
  • composition (see prev. item for detail)
  • private inheritance. 
Now, which to use when? Former should be used whenever possible. Private inheritance would be handy when you need to redefine a virtual member function or need to access to protected base class members.
Beside the 2 alternatives mentioned above,
  • we may have a design something like:
class Widget {
private:
  class WidgetTimer: public Timer {
  public:
    virtual void onTick() const;
    ...

  };
   WidgetTimer timer;
  ...

};
In the third alternative's example, we need to redefine onTick() of class Timer.This design is appreciated over private inheritance because 1.the derived classes would not be able to refine onTick(). 2. In case of private inheritance, Widget get compilation dependencies on the definition of class Timer which would force us to #include timer.h Whereas in alternative 3, By defining WidgetTimer class out of the Widget class and placing a pointer to WidgetTimer would require just declaration of WidgetTimer class at the minimum.
prev | next
cheers

Effective C++ Ed. 3rd: Item 38. Model "has-a" or "is-implemented-in-terms-of" through composition

Composition is the relationship between types that arises when objects of one type contain objects of another type. Composition means either "has-a"(things related to application domain) or "is-implemented-in-terms-of."(things related to your software's implementation domain like mutexes, buffer, search trees etc.)

prev | next
Cheers

Effective C++ Ed. 3rd: Item 37. Never redefine a function's inherited default parameter value

Never redefine an inherited default parameter value, because these are statically bound unlike the virtual functions ( the only functions which should be overridding).
The following NVI technique is one of the alternatives if you want to redefine an inherited default parameter value:
class Shape {
public:
  enum ShapeColor { Red, Green, Blue };

  void draw(ShapeColor color = Red) const           // now non-virtual
  {
    doDraw(color);                                  // calls a virtual
  }

  ...

private:
  virtual void doDraw(ShapeColor color) const = 0;  // the actual work is
};                                                  // done in this func

class Rectangle: public Shape {
public:

  ...

private:
  virtual void doDraw(ShapeColor color) const;       // note lack of a
  ...                                                // default param val.
};

prev | next
Cheers

Effective C++ Ed. 3rd: Item 36. Never redefine an inherited non-virtual function

When base class pointer points to a derived class pointer and an inherited non-virtual function which is refined in derived class, the definition present in base class would execute. This happens because non-virtual functions are statically bind. So
  1. Either declare only those functions non-virtual which will remain invariant in derived classes
  2. Or use virtual functions or it's alternatives at defines here.
prev | next

Cheers

BSD sockets: Server Client Chat Program using TCP

Check latest version here.

Header File 1 for Server Program: SChat_register.h
#ifndef SCHAT_REGISTER_H
#define SCHAT_REGISTER_H
//SChat_register.h
#include "SChat_connect.h"

typedef short bool;
int* ar;//= (int*) malloc(sizeof(int));
int maxIndex;// = 0;
int top;// = -5;
bool reg_lock;// = false;

void getRegisteredList(){
 int i;
 for(i=0; i<=top; i++)
  printf("%d\t", ar[i]);
 printf("\n");
}
//call this before using any other function of this library
void init(){
 ar = (int*) calloc(1, sizeof(int));
 if(ar == NULL)
  Error("malloc error in init");
 top = -1;
 con_init();
 maxIndex = 0;
 reg_lock = false;
}

Effective C++ Ed. 3rd: Item 35. Consider alternatives to virtual functions

  • Non-virtual Interface Idiom (NVI): Provides an alternative to public virtual functions by making these public and accesible via a public non-virtual member function. It does NOT elimination the use of virtuals.
  • Function Pointers: To eliminate using virtual member functions, pointer to function can be kept as a member. It is good when all needed information for the function is available from public interface.
  • tr1::function: It is like function pointers but more generic and can even point to function objects or member functoins. So clients can use any compatible calling entity.
  • The "classic" Strategy Pattern: To move virtual functions from 1 inheritance hierarchy to other.
prev | next

Cheers

Ubuntu Server 10.04: What ports are open?

Run the following command:

netstat -anltp


Cheers.