473,657 Members | 2,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

arglib counted_ptr operator= question

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

arg::counted_pt r<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_p tr<pointee_type >&
arg::counted_pt r<pointee_type> ::operator=(con st
arg::typed_refe rence<pointee_t ype>&) [with pointee_type = test]':
test.cpp:8: instantiated from here
arglib/arg_shared.h:18 4: error: `void
arg::typed_refe rence<pointee_t ype>::increment _strong_referen ces() const
[with pointee_type = test]' is protected
arglib/arg_shared.h:31 3: 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
misunderstandin g 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 1579

mati-006 wrote:
Hi
Why there is no "counted_pt r& 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_pt r<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_stron g_references()

arg::counted_pt r<testc;
arg::counted_pt r<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_pt r<test>& r_ptr) { /* check r_ptr != 0 */
}

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

Again, note that
const arg::counted_pt r<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_p tr<pointee_type >&
arg::counted_pt r<pointee_type> ::operator=(con st
arg::typed_refe rence<pointee_t ype>&) [with pointee_type = test]':
test.cpp:8: instantiated from here
arglib/arg_shared.h:18 4: error: `void
arg::typed_refe rence<pointee_t ype>::increment _strong_referen ces() const
[with pointee_type = test]' is protected
arglib/arg_shared.h:31 3: 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
misunderstandin g 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_pt r& 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_pt r<testc;
....
c.reset(new test(constructo r args));

or
arg::counted_pt r<testc;
....
arg::counted_pt r<testtemp_p(ne w test(constructo r 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=(point ee_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_stron g_references()

arg::counted_pt r<testc;
arg::counted_pt r<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_pt r<test>& r_ptr) { /* check r_ptr != 0 */
}

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

Again, note that
const arg::counted_pt r<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
8026
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 something like this: 1) e = (a, b, c, d); // concatenate a,b,c,d into e 2) (a, b, c, d) = e; // get the bits of e into a,b,c, and d For example, in the second case, assume that a,b,c,d represent 2-bit integers, and e represents an 8-bit...
30
10420
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. However, I've come upon a question that I can neither answer from "The Book" or a Google search (so yes, at least I RTFBed). I'm hoping that someone in this news group might know the answer. Overloading the Operator Say I want to develop a...
2
5874
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 overloading. I just cannot find any explanation that clearly point out what the parts of the statement refer to. For example the book says: comp operator+(comp b)
7
2318
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 shown by the example: -- code fragment 1 ---------------------------------------------------- class MyClass
3
1902
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
2171
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 Debug_Level ddl;
67
8631
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 in low-level numerical computations, replacing Fortran in newer projects. Check, for example, sourceforge.net or freshmeat.net But neither language offers a primitive exp operator.
56
4750
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
1954
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 a, b, c; a = b + c;
0
8402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8315
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8734
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8608
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6172
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4164
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.