example/RefCountPtr/cxx_main.cpp

Go to the documentation of this file.
00001 #include "Teuchos_RCP.hpp"
00002 #include "Teuchos_Version.hpp"
00003 
00004 class A { 
00005  public: 
00006    A() {}
00007    virtual ~A(){} 
00008    virtual void f(){} 
00009 };   
00010 class B1 : virtual public A {};
00011 class B2 : virtual public A {};
00012 class C : public B1, public B2 {};
00013 
00014 using namespace Teuchos;
00015 
00016 int main(int argc, char* argv[])
00017 {
00018 
00019   std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00020 
00021   // Create some reference-counted pointers.
00022   // Create a reference-counted NULL pointer of type A.
00023   RCP<A>             a_null_ptr;
00024   // Create a reference-counted pointer of non-const type A.
00025   RCP<A>             a_ptr   = rcp(new A);
00026   // Create a reference-counted pointer of const type A.
00027   RCP<const A>       ca_ptr  = rcp(new A);
00028   // Create a const reference-counted pointer of non-const type A.
00029   const RCP<A>       a_cptr  = rcp(new A); 
00030   // Create a const reference-counted pointer of const type A.
00031   const RCP<const A> ca_cptr = rcp(new A); 
00032 
00033   // Perform implicit conversions between a derived class and its base class.
00034   RCP<B1> b1_ptr  = rcp(new B1);
00035   RCP<A> a_ptr1 = b1_ptr;
00036 
00037   /* Other non-implicit type conversions like static, dynamic, or const casts
00038      can be taken care of by non-member template functions.
00039   */
00040   RCP<const C>  c_ptr     = rcp(new C);
00041   // Implicit cast from C to B2.
00042   RCP<const B2> b2_ptr    = c_ptr;                              
00043   // Safe cast, type-checked, from C to A.
00044   RCP<const A>  ca_ptr1   = rcp_dynamic_cast<const A>(c_ptr); 
00045   // Unsafe cast, non-type-checked, from C to A.
00046   RCP<const A>  ca_ptr2   = rcp_static_cast<const A>(c_ptr);  
00047   // Cast away const from B2.
00048   RCP<B2>       nc_b2_ptr = rcp_const_cast<B2>(b2_ptr);           
00049 
00050   /* Using a reference-counted pointer is very similar to using a raw C++ pointer.  Some
00051      of the operations that are common to both are:
00052   */
00053   RCP<A>
00054     a_ptr2 = rcp(new A),  // Initialize reference-counted pointers.
00055     a_ptr3 = rcp(new A);  // ""
00056   A  *ra_ptr2 = new A,    // Initialize non-reference counted pointers.
00057     *ra_ptr3 = new A;     // ""
00058   a_ptr2 = rcp(ra_ptr3);  // Assign from a raw pointer (only do this once!)
00059   a_ptr3 = a_ptr1;        // Assign one smart pointer to another.
00060   a_ptr2 = rcp(ra_ptr2);  // Assign from a raw pointer (only do this once!)
00061   a_ptr2->f();            // Access a member of A using ->
00062   ra_ptr2->f();           // ""
00063   *a_ptr2 = *a_ptr3;      // Dereference the objects and assign.
00064   *ra_ptr2 = *ra_ptr3;    // ""
00065 
00066   // Get the raw C++ pointer.
00067   A* true_ptr = 0;
00068   true_ptr = a_ptr1.get();
00069 
00070   return 0;
00071 
00072 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Tue Oct 20 10:13:59 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1