Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing Smart Pointer - Dynamic Allocation with templates
    primarykey
    data
    text
    <p>I'm in the process of writing a smart pointer countedptr and I've hit a speed bump. The basic function of countedptr is to work like any other smart pointer and also have a count of how many pointers are pointing to a single object. So far, the code is:</p> <p>[SOLVED]</p> <pre><code>#include "std_lib_facilities.h" template &lt;class T&gt; class counted_ptr{ private: T* pointer; int* count; public: counted_ptr(T* p = 0, int* c = new int(1)) : pointer(p), count(c) {} // default constructor explicit counted_ptr(const counted_ptr&amp; p) : pointer(p.pointer), count(p.count) { ++*count; } // copy constructor ~counted_ptr() { --*count; delete pointer; } counted_ptr&amp; operator=(const counted_ptr&amp; p) { pointer = p.pointer; count = p.count; ++*count; return *this; } T* operator-&gt;() const{ return pointer; } T&amp; operator*() const { return *pointer; } int Get_count() const { return *count; } }; int main() { counted_ptr&lt;double&gt; one; counted_ptr&lt;double&gt;two(one); int a = one.Get_count(); cout &lt;&lt; a &lt;&lt; endl; } </code></pre> <p>When I try to do something like</p> <pre><code>one-&gt;pointer = new double(5); </code></pre> <p>then I get a compiler error saying "request for member 'pointer' in '*(&amp;one)->counted_ptr::operator->with T = double' which is of non-class type double".</p> <p>I considered making a function to do this, and while I could make a function to allocate an array of T's, I can't think of a way of making one for allocating actual objects. Any help is appreciated, thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload