|
Teuchos Package Browser (Single Doxygen Collection) Version of the Day
|
00001 /* 00002 // @HEADER 00003 // *********************************************************************** 00004 // 00005 // Teuchos: Common Tools Package 00006 // Copyright (2004) Sandia Corporation 00007 // 00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00009 // license for use of this work by or on behalf of the U.S. Government. 00010 // 00011 // This library is free software; you can redistribute it and/or modify 00012 // it under the terms of the GNU Lesser General Public License as 00013 // published by the Free Software Foundation; either version 2.1 of the 00014 // License, or (at your option) any later version. 00015 // 00016 // This library is distributed in the hope that it will be useful, but 00017 // WITHOUT ANY WARRANTY; without even the implied warranty of 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 // Lesser General Public License for more details. 00020 // 00021 // You should have received a copy of the GNU Lesser General Public 00022 // License along with this library; if not, write to the Free Software 00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00024 // USA 00025 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00026 // 00027 // *********************************************************************** 00028 // @HEADER 00029 */ 00030 00031 #include "Teuchos_RCP.hpp" 00032 #include "Teuchos_Version.hpp" 00033 00034 class A { 00035 public: 00036 A() {} 00037 virtual ~A(){} 00038 virtual void f(){} 00039 }; 00040 class B1 : virtual public A {}; 00041 class B2 : virtual public A {}; 00042 class C : public B1, public B2 {}; 00043 00044 using namespace Teuchos; 00045 00046 int main(int argc, char* argv[]) 00047 { 00048 00049 std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl; 00050 00051 // Create some reference-counted pointers. 00052 // Create a reference-counted NULL pointer of type A. 00053 RCP<A> a_null_ptr; 00054 // Create a reference-counted pointer of non-const type A. 00055 RCP<A> a_ptr = rcp(new A); 00056 // Create a reference-counted pointer of const type A. 00057 RCP<const A> ca_ptr = rcp(new A); 00058 // Create a const reference-counted pointer of non-const type A. 00059 const RCP<A> a_cptr = rcp(new A); 00060 // Create a const reference-counted pointer of const type A. 00061 const RCP<const A> ca_cptr = rcp(new A); 00062 00063 // Perform implicit conversions between a derived class and its base class. 00064 RCP<B1> b1_ptr = rcp(new B1); 00065 RCP<A> a_ptr1 = b1_ptr; 00066 00067 /* Other non-implicit type conversions like static, dynamic, or const casts 00068 can be taken care of by non-member template functions. 00069 */ 00070 RCP<const C> c_ptr = rcp(new C); 00071 // Implicit cast from C to B2. 00072 RCP<const B2> b2_ptr = c_ptr; 00073 // Safe cast, type-checked, from C to A. 00074 RCP<const A> ca_ptr1 = rcp_dynamic_cast<const A>(c_ptr); 00075 // Unsafe cast, non-type-checked, from C to A. 00076 RCP<const A> ca_ptr2 = rcp_static_cast<const A>(c_ptr); 00077 // Cast away const from B2. 00078 RCP<B2> nc_b2_ptr = rcp_const_cast<B2>(b2_ptr); 00079 00080 /* Using a reference-counted pointer is very similar to using a raw C++ pointer. Some 00081 of the operations that are common to both are: 00082 */ 00083 RCP<A> 00084 a_ptr2 = rcp(new A), // Initialize reference-counted pointers. 00085 a_ptr3 = rcp(new A); // "" 00086 A *ra_ptr2 = new A, // Initialize non-reference counted pointers. 00087 *ra_ptr3 = new A; // "" 00088 a_ptr2 = rcp(ra_ptr3); // Assign from a raw pointer (only do this once!) 00089 a_ptr3 = a_ptr1; // Assign one smart pointer to another. 00090 a_ptr2 = rcp(ra_ptr2); // Assign from a raw pointer (only do this once!) 00091 a_ptr2->f(); // Access a member of A using -> 00092 ra_ptr2->f(); // "" 00093 *a_ptr2 = *a_ptr3; // Dereference the objects and assign. 00094 *ra_ptr2 = *ra_ptr3; // "" 00095 00096 // Get the raw C++ pointer. 00097 A* true_ptr = 0; 00098 true_ptr = a_ptr1.get(); 00099 00100 return 0; 00101 00102 }
1.7.4