arglib counted_ptr operator= question

M

mati-006

Hi
Why there is no "counted_ptr& operator= (pointee_type* p)" ?
This question has arisen when I was searching why following piece of
code won't compile:

arg::counted_ptr<test> c;
c = new test;

And compiler wasn't very helpful with this:

g++ -ansi -pedantic -Wall -W -c test.cpp
arglib/arg_shared.h: In member function `arg::counted_ptr<pointee_type>&
arg::counted_ptr<pointee_type>::eek:perator=(const
arg::typed_reference<pointee_type>&) [with pointee_type = test]':
test.cpp:8: instantiated from here
arglib/arg_shared.h:184: error: `void
arg::typed_reference<pointee_type>::increment_strong_references() const
[with pointee_type = test]' is protected
arglib/arg_shared.h:313: error: within this context

BTW Why the g++ hasn't just complain about wrong type?

Finally I've looked on the docs carefully and I see that there are only
following operator= defined:
counted_ptr& operator= (const base_type& rhs)
counted_ptr& operator= (const counted_ptr& rhs)
And only way to initialize the counted_ptr is to call the reset method
void reset (pointee_type* p)
//Delete existing contents (and take ownership of p).

So - is it arglib's author's oversight (I don't think so), am I
misunderstanding something (problably) or everything works as I
understand it and there is an explanation for not implementing
"operator= (pointee_type* p)"?
 
S

Salt_Peter

mati-006 said:
Hi
Why there is no "counted_ptr& operator= (pointee_type* p)" ?

Because if there was, you'ld defeat the purpose of smart pointers.
This question has arisen when I was searching why following piece of
code won't compile:

arg::counted_ptr<test> c;
c = new test;

You should be gratiously thankfull that failed.
You'ld leak the allocation in the rhv otherwise.
The compiler also describes the missing requirements, which makes sense
since this probably a reference counting smart pointer. Note the error
about the missing increment_strong_references()

arg::counted_ptr<test> c;
arg::counted_ptr<test> p(new test);
c = p; // this should work
Unlike an auto_ptr, the above allocation will only get deallocated once
both smart pointers are zapped.

By the way, this won't prevent you from passing the smart pointer
around.
instead of
void foo(test* const p) {...} // constant pointer to mutable

You define foo as so:
void foo(const arg::counted_ptr<test>& r_ptr) { /* check r_ptr != 0 */
}

//and call it:
arg::counted_ptr<test> p(new test);
foo(p); // the smart pointer is constant, what is *at* p is not !!

Again, note that
const arg::counted_ptr<T> p;
acts like T* const, not const T*
At least, thats how a smart pointer *should* work.
And compiler wasn't very helpful with this:

g++ -ansi -pedantic -Wall -W -c test.cpp
arglib/arg_shared.h: In member function `arg::counted_ptr<pointee_type>&
arg::counted_ptr<pointee_type>::eek:perator=(const
arg::typed_reference<pointee_type>&) [with pointee_type = test]':
test.cpp:8: instantiated from here
arglib/arg_shared.h:184: error: `void
arg::typed_reference<pointee_type>::increment_strong_references() const
[with pointee_type = test]' is protected
arglib/arg_shared.h:313: error: within this context

BTW Why the g++ hasn't just complain about wrong type?

Finally I've looked on the docs carefully and I see that there are only
following operator= defined:
counted_ptr& operator= (const base_type& rhs)
counted_ptr& operator= (const counted_ptr& rhs)
And only way to initialize the counted_ptr is to call the reset method
void reset (pointee_type* p)
//Delete existing contents (and take ownership of p).

So - is it arglib's author's oversight (I don't think so), am I
misunderstanding something (problably) or everything works as I
understand it and there is an explanation for not implementing
"operator= (pointee_type* p)"?
 
M

mati-006

Salt_Peter said:
Because if there was, you'ld defeat the purpose of smart pointers.

So the only way to initialize an existing smart pointer (from arglib)
with a new object is to call its reset member function, or to make a new
counted pointer and then call "=", but which method is better?
Code explaining what I mean:

arg::counted_ptr<test> c;
....
c.reset(new test(constructor args));

or
arg::counted_ptr<test> c;
....
You should be gratiously thankfull that failed.
You'ld leak the allocation in the rhv otherwise.

I do not understand why, afaik the new test returns a pointer to the
newly created "test" object, so what would leak here if the c would take
ownership of the pointee (in other words, if the
operator=(pointee_type*) would be defined in a way that reset member
function is)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top