Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
core/example/RefCountPtr/cxx_main.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Teuchos_RCP.hpp"
11 #include "Teuchos_Version.hpp"
12 
13 class A {
14  public:
15  A() {}
16  virtual ~A(){}
17  virtual void f(){}
18 };
19 class B1 : virtual public A {};
20 class B2 : virtual public A {};
21 class C : public B1, public B2 {};
22 
23 using namespace Teuchos;
24 
25 int main(int argc, char* argv[])
26 {
27 
28  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
29 
30  // Create some reference-counted pointers.
31  // Create a reference-counted NULL pointer of type A.
32  RCP<A> a_null_ptr;
33  // Create a reference-counted pointer of non-const type A.
34  RCP<A> a_ptr = rcp(new A);
35  // Create a reference-counted pointer of const type A.
36  RCP<const A> ca_ptr = rcp(new A);
37  // Create a const reference-counted pointer of non-const type A.
38  const RCP<A> a_cptr = rcp(new A);
39  // Create a const reference-counted pointer of const type A.
40  const RCP<const A> ca_cptr = rcp(new A);
41 
42  // Perform implicit conversions between a derived class and its base class.
43  RCP<B1> b1_ptr = rcp(new B1);
44  RCP<A> a_ptr1 = b1_ptr;
45 
46  /* Other non-implicit type conversions like static, dynamic, or const casts
47  can be taken care of by non-member template functions.
48  */
49  RCP<const C> c_ptr = rcp(new C);
50  // Implicit cast from C to B2.
51  RCP<const B2> b2_ptr = c_ptr;
52  // Safe cast, type-checked, from C to A.
53  RCP<const A> ca_ptr1 = rcp_dynamic_cast<const A>(c_ptr);
54  // Unsafe cast, non-type-checked, from C to A.
55  RCP<const A> ca_ptr2 = rcp_static_cast<const A>(c_ptr);
56  // Cast away const from B2.
57  RCP<B2> nc_b2_ptr = rcp_const_cast<B2>(b2_ptr);
58 
59  /* Using a reference-counted pointer is very similar to using a raw C++ pointer. Some
60  of the operations that are common to both are:
61  */
62  RCP<A>
63  a_ptr2 = rcp(new A), // Initialize reference-counted pointers.
64  a_ptr3 = rcp(new A); // ""
65  A *ra_ptr2 = new A, // Initialize non-reference counted pointers.
66  *ra_ptr3 = new A; // ""
67  a_ptr2 = rcp(ra_ptr3); // Assign from a raw pointer (only do this once!)
68  a_ptr3 = a_ptr1; // Assign one smart pointer to another.
69  a_ptr2 = rcp(ra_ptr2); // Assign from a raw pointer (only do this once!)
70  a_ptr2->f(); // Access a member of A using ->
71  ra_ptr2->f(); // ""
72  *a_ptr2 = *a_ptr3; // Dereference the objects and assign.
73  *ra_ptr2 = *ra_ptr3; // ""
74 
75  // Get the raw C++ pointer.
76  A* true_ptr = 0;
77  true_ptr = a_ptr1.get();
78  TEUCHOS_ASSERT_EQUALITY(true_ptr, b1_ptr.get());
79 
80  return 0;
81 
82 }
T * get() const
Get the raw C++ pointer to the underlying object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
std::string Teuchos_Version()
int main(int argc, char *argv[])
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
This macro is checks that to numbers are equal and if not then throws an exception with a good error ...
Reference-counted pointer class and non-member templated function implementations.