473,382 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

arglib counted_ptr operator= question

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<testc;
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>::operator=(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_stro ng_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)"?

--
mati
Nov 11 '06 #1
2 1547

mati-006 wrote:
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<testc;
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<testc;
arg::counted_ptr<testp(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<testp(new test);
foo(p); // the smart pointer is constant, what is *at* p is not !!

Again, note that
const arg::counted_ptr<Tp;
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>::operator=(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_stro ng_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)"?

--
mati
Nov 11 '06 #2
Salt_Peter wrote:
mati-006 wrote:
>>Hi
Why there is no "counted_ptr& operator= (pointee_type* p)" ?

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<testc;
....
c.reset(new test(constructor args));

or
arg::counted_ptr<testc;
....
arg::counted_ptr<testtemp_p(new test(constructor args);
c = temp_p;
>>This question has arisen when I was searching why following piece of
code won't compile:

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

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)?
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<testc;
arg::counted_ptr<testp(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<testp(new test);
foo(p); // the smart pointer is constant, what is *at* p is not !!

Again, note that
const arg::counted_ptr<Tp;
acts like T* const, not const T*
At least, thats how a smart pointer *should* work.


--
mati
Nov 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
2
by: victor75040 | last post by:
Before you all start flaming me, I am not a student and this is not for any homework. Just someone learing c++ on their own. I am now up to the chapter in my book that describes operator...
7
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding Herb Sutter's idiom of implementation of operator= via nonthrowing swap() member function, to guarantee strict exception safety. The idea of the idiom is...
3
by: Alicia | last post by:
Hello, I am trying to figure out how to call an overloaded operator<< inherited from a base class. #ifndef PHONECALL #define PHONECALL #include "time.h" #include "interval.h"
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
15
by: Jeroen | last post by:
Hi all, I've got a very specific question about the evaluation order in C++. Assume some kind of custom array class, with an overloaded subscript operator. In the following code: { my_array...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.