ptr_cast Struct Template Reference
[Conversion Library]

#include <stlsoft/conversion/ptr_cast.hpp>

List of all members.


Detailed Description

template<typename Target>
struct stlsoft::ptr_cast< Target >

A cast that throws bad_cast for pointer cast failures, as well as for reference casts.

Assume the following class hierarchy, and variables:

class Parent
{
public:
  virtual ~Parent();
};

class A
  : public Parent
{
public:
  ~A();
};

class B
  : public Parent
{
public:
  ~B();
};

. . .

A   a;
B   b;

The following code segments illustrate the similarities and differences between dynamic_cast and ptr_cast:

(i) dynamic cast of reference that succeeds

A   &ra1 = dynamic_cast<A&>(a);
A   &ra2 = stlsoft::ptr_cast<A&>(a);

assert(&ra1 == &ra2);

(ii) dynamic cast of pointer that succeeds

A   *pa = dynamic_cast<A*>(&a);

assert(NULL != pa);
assert(stlsoft::ptr_cast<A*>(&a) == pa);

(iii) dynamic cast of reference that fails

bool bCastFailed1 = false;

try
{
  B &b1 = dynamic_cast<B&>(a);

  . . . // Will never get here
}
catch(std::bad_cast &)
{
  bCastFailed1 = true;
}
assert(bCastFailed1);

bool bCastFailed2 = false;

try
{
  B &rb = stlsoft::ptr_cast<B&>(a);

  . . . // Will never get here
}
catch(std::bad_cast &)
{
  bCastFailed2 = true;
}
assert(bCastFailed2);

(iv) dynamic cast of pointer that fails

assert(NULL == dynamic_cast<B*>(&a));

bool bCastFailed = false;

try
{
  B &rb = stlsoft::ptr_cast<B&>(&a);

  . . . // Will never get here
}
catch(std::bad_cast &)
{
  bCastFailed = true;
}
assert(bCastFailed);

Note:
This is described in detail in chapter 19 of Imperfect C++.

Public Types

typedef Target target_type
 The target type.
typedef ptr_cast< Target > class_type
 The current instantiation of the type.
typedef
stlsoft::base_type_traits
< Target >::cv_type 
target_base_type
 The target base type.
typedef target_base_typereference_type
 The reference type.
typedef target_base_typepointer_type
 The pointer type.

Public Member Functions

template<typename Source>
 ptr_cast (Source &s)
 Constructor used when casting a reference.
template<typename Source>
 ptr_cast (Source *s)
 Constructor used when casting a pointer.
 operator reference_type () const
 Converts an instance of the cast class to a reference.
 operator pointer_type () const
 Converts an instance of the cast class to a pointer.

Member Typedef Documentation

typedef Target target_type

The target type.

typedef ptr_cast<Target> class_type

The current instantiation of the type.

typedef stlsoft::base_type_traits<Target>::cv_type target_base_type

The target base type.

The reference type.

The pointer type.


Constructor & Destructor Documentation

ptr_cast ( Source &  s  )  [inline]

Constructor used when casting a reference.

ptr_cast ( Source *  s  )  [inline]

Constructor used when casting a pointer.

References stlsoft_ns_qual_std.


Member Function Documentation

operator reference_type (  )  const [inline]

Converts an instance of the cast class to a reference.

operator pointer_type (  )  const [inline]

Converts an instance of the cast class to a pointer.


The documentation for this struct was generated from the following file:

Generated on Thu Jun 10 08:58:45 2010 for STLSoft by  doxygen 1.5.6