|
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 // Redistribution and use in source and binary forms, with or without 00012 // modification, are permitted provided that the following conditions are 00013 // met: 00014 // 00015 // 1. Redistributions of source code must retain the above copyright 00016 // notice, this list of conditions and the following disclaimer. 00017 // 00018 // 2. Redistributions in binary form must reproduce the above copyright 00019 // notice, this list of conditions and the following disclaimer in the 00020 // documentation and/or other materials provided with the distribution. 00021 // 00022 // 3. Neither the name of the Corporation nor the names of the 00023 // contributors may be used to endorse or promote products derived from 00024 // this software without specific prior written permission. 00025 // 00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00037 // 00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00039 // 00040 // *********************************************************************** 00041 // @HEADER 00042 */ 00043 00044 #include "Teuchos_RCP.hpp" 00045 #include "Teuchos_Version.hpp" 00046 00047 class A { 00048 public: 00049 A() {} 00050 virtual ~A(){} 00051 virtual void f(){} 00052 }; 00053 class B1 : virtual public A {}; 00054 class B2 : virtual public A {}; 00055 class C : public B1, public B2 {}; 00056 00057 using namespace Teuchos; 00058 00059 int main(int argc, char* argv[]) 00060 { 00061 00062 std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl; 00063 00064 // Create some reference-counted pointers. 00065 // Create a reference-counted NULL pointer of type A. 00066 RCP<A> a_null_ptr; 00067 // Create a reference-counted pointer of non-const type A. 00068 RCP<A> a_ptr = rcp(new A); 00069 // Create a reference-counted pointer of const type A. 00070 RCP<const A> ca_ptr = rcp(new A); 00071 // Create a const reference-counted pointer of non-const type A. 00072 const RCP<A> a_cptr = rcp(new A); 00073 // Create a const reference-counted pointer of const type A. 00074 const RCP<const A> ca_cptr = rcp(new A); 00075 00076 // Perform implicit conversions between a derived class and its base class. 00077 RCP<B1> b1_ptr = rcp(new B1); 00078 RCP<A> a_ptr1 = b1_ptr; 00079 00080 /* Other non-implicit type conversions like static, dynamic, or const casts 00081 can be taken care of by non-member template functions. 00082 */ 00083 RCP<const C> c_ptr = rcp(new C); 00084 // Implicit cast from C to B2. 00085 RCP<const B2> b2_ptr = c_ptr; 00086 // Safe cast, type-checked, from C to A. 00087 RCP<const A> ca_ptr1 = rcp_dynamic_cast<const A>(c_ptr); 00088 // Unsafe cast, non-type-checked, from C to A. 00089 RCP<const A> ca_ptr2 = rcp_static_cast<const A>(c_ptr); 00090 // Cast away const from B2. 00091 RCP<B2> nc_b2_ptr = rcp_const_cast<B2>(b2_ptr); 00092 00093 /* Using a reference-counted pointer is very similar to using a raw C++ pointer. Some 00094 of the operations that are common to both are: 00095 */ 00096 RCP<A> 00097 a_ptr2 = rcp(new A), // Initialize reference-counted pointers. 00098 a_ptr3 = rcp(new A); // "" 00099 A *ra_ptr2 = new A, // Initialize non-reference counted pointers. 00100 *ra_ptr3 = new A; // "" 00101 a_ptr2 = rcp(ra_ptr3); // Assign from a raw pointer (only do this once!) 00102 a_ptr3 = a_ptr1; // Assign one smart pointer to another. 00103 a_ptr2 = rcp(ra_ptr2); // Assign from a raw pointer (only do this once!) 00104 a_ptr2->f(); // Access a member of A using -> 00105 ra_ptr2->f(); // "" 00106 *a_ptr2 = *a_ptr3; // Dereference the objects and assign. 00107 *ra_ptr2 = *ra_ptr3; // "" 00108 00109 // Get the raw C++ pointer. 00110 A* true_ptr = 0; 00111 true_ptr = a_ptr1.get(); 00112 TEUCHOS_ASSERT_EQUALITY(true_ptr, b1_ptr.get()); 00113 00114 return 0; 00115 00116 }
1.7.6.1