comp.lang.c++
http://groups.google.com/group/comp.lang.c++?hl=en
comp.lang.c++@googlegroups.com
Today's topics:
* implicit passing by reference - 14 messages, 9 authors
http://groups.google.com/group/comp.lang.c++/t/dddf6db3d26e03e4?hl=en
* Working with Large Values (double) - 10 messages, 7 authors
http://groups.google.com/group/comp.lang.c++/t/e42843c9cdf13724?hl=en
* OT: Problem building libc++ - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/177ee9b847d7540f?hl=en
* shared_ptr and unique_ptr related question - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/ae1f30d2345574fe?hl=en
==============================================================================
TOPIC: implicit passing by reference
http://groups.google.com/group/comp.lang.c++/t/dddf6db3d26e03e4?hl=en
==============================================================================
== 1 of 14 ==
Date: Thurs, Feb 20 2014 8:45 pm
From: jerry.jeremiah@gmail.com
In this topic
http://stackoverflow.com/questions/21924885/could-someone-please-just-tell-me-why-my-for-loop-will-not-count-all-the-way-u
The person that answers it says:
You have to remember that objects are sent to functions 'by reference'.
But the object being passed contains only one int member and the function the object is being passed to clearly takes it's parameter by value and stores it in a member value.
Do objects really always pass by reference regardless of what the parameter list says? And, if so, why would it do that? I would usderstand if the function took its parameter explicitly by reference...
Thanks for clearing up mu confusion.
Jerry
== 2 of 14 ==
Date: Thurs, Feb 20 2014 9:42 pm
From: Paavo Helde
jerry.jeremiah@gmail.com wrote in
news:0f7c3e54-364c-4e62-bdab-3a615ca3796e@googlegroups.com:
> In this topic
>
> http://stackoverflow.com/questions/21924885/could-someone-please-just-
> tell-me-why-my-for-loop-will-not-count-all-the-way-u
>
> The person that answers it says:
>
> You have to remember that objects are sent to functions 'by
> reference'.
>
> But the object being passed contains only one int member and the
> function the object is being passed to clearly takes it's parameter by
> value and stores it in a member value.
>
> Do objects really always pass by reference regardless of what the
> parameter list says? And, if so, why would it do that? I would
> usderstand if the function took its parameter explicitly by
> reference...
In C++ the function declarations specify exactly if the pass is by value
or by reference. The people at stackoverflow probably have confused C++
with some other language. Do not take stackoverflow too seriously, the
answers there are ranked by the person least qualified to judge the
correctness.
Cheers
Paavo
== 3 of 14 ==
Date: Fri, Feb 21 2014 3:44 am
From: Jorgen Grahn
On Fri, 2014-02-21, Paavo Helde wrote:
> jerry.jeremiah@gmail.com wrote in
> news:0f7c3e54-364c-4e62-bdab-3a615ca3796e@googlegroups.com:
>
>> In this topic
>>
>> http://stackoverflow.com/questions/21924885/could-someone-please-just-
>> tell-me-why-my-for-loop-will-not-count-all-the-way-u
>>
>> The person that answers it says:
>>
>> You have to remember that objects are sent to functions 'by
>> reference'.
>>
>> But the object being passed contains only one int member and the
>> function the object is being passed to clearly takes it's parameter by
>> value and stores it in a member value.
>>
>> Do objects really always pass by reference regardless of what the
>> parameter list says? And, if so, why would it do that? I would
>> usderstand if the function took its parameter explicitly by
>> reference...
>
> In C++ the function declarations specify exactly if the pass is by value
> or by reference.
Or you could say it's always by value -- but you can choose to pass a
reference or pointer to some object.
> The people at stackoverflow probably have confused C++
> with some other language.
Yes, and you have to be pretty confused to do that.
I tell my coworkers to avoid stackoverflow because of stuff like
this.
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
== 4 of 14 ==
Date: Fri, Feb 21 2014 8:36 am
From: Marcel Müller
On 21.02.14 12.44, Jorgen Grahn wrote:
> On Fri, 2014-02-21, Paavo Helde wrote:
>> In C++ the function declarations specify exactly if the pass is by value
>> or by reference.
>
> Or you could say it's always by value -- but you can choose to pass a
> reference or pointer to some object.
Strictly speaking it is up to the platform how references are
implemented. The option that they are binary compatible to pointers is
only a common solution.
>> The people at stackoverflow probably have confused C++
>> with some other language.
>
> Yes, and you have to be pretty confused to do that.
> I tell my coworkers to avoid stackoverflow because of stuff like
> this.
Well, in general it is not that bad. But it is more the home of the .NET
guys (where the statement about objects is true) and like any other
forum it is no editorially approved content.
Marcel
== 5 of 14 ==
Date: Fri, Feb 21 2014 1:28 pm
From: 88888 Dihedral
On Friday, February 21, 2014 1:42:57 PM UTC+8, Paavo Helde wrote:
> jerry.jeremiah@gmail.com wrote in
>
> news:0f7c3e54-364c-4e62-bdab-3a615ca3796e@googlegroups.com:
>
>
>
> > In this topic
>
> >
>
> > http://stackoverflow.com/questions/21924885/could-someone-please-just-
>
> > tell-me-why-my-for-loop-will-not-count-all-the-way-u
>
> >
>
> > The person that answers it says:
>
> >
>
> > You have to remember that objects are sent to functions 'by
>
> > reference'.
>
> >
>
> > But the object being passed contains only one int member and the
>
> > function the object is being passed to clearly takes it's parameter by
>
> > value and stores it in a member value.
>
> >
>
> > Do objects really always pass by reference regardless of what the
>
> > parameter list says? And, if so, why would it do that? I would
>
> > usderstand if the function took its parameter explicitly by
>
> > reference...
>
>
>
> In C++ the function declarations specify exactly if the pass is by value
>
> or by reference. The people at stackoverflow probably have confused C++
>
> with some other language. Do not take stackoverflow too seriously, the
>
> answers there are ranked by the person least qualified to judge the
>
> correctness.
>
>
>
> Cheers
>
> Paavo
Well, an object in C++ has some hidden
field to store the address of this object in the C sense.
== 6 of 14 ==
Date: Fri, Feb 21 2014 1:39 pm
From: Victor Bazarov
On 2/21/2014 4:28 PM, 88888 Dihedral wrote:
>[...]
> Well, an object in C++ has some hidden
> field to store the address of this object in the C sense.
I will probably regret this later, but... What the hell do you mean?
V
--
I do not respond to top-posted replies, please don't ask
== 7 of 14 ==
Date: Fri, Feb 21 2014 1:45 pm
From: "Osmium"
"Victor Bazarov" wrote:
> On 2/21/2014 4:28 PM, 88888 Dihedral wrote:
>>[...]
>> Well, an object in C++ has some hidden
>> field to store the address of this object in the C sense.
>
> I will probably regret this later, but... What the hell do you mean?
Now you did it. You reactivated him after a long dormant period.
== 8 of 14 ==
Date: Fri, Feb 21 2014 2:43 pm
From: Victor Bazarov
On 2/21/2014 4:45 PM, Osmium wrote:
> "Victor Bazarov" wrote:
>
>> On 2/21/2014 4:28 PM, 88888 Dihedral wrote:
>>> [...]
>>> Well, an object in C++ has some hidden
>>> field to store the address of this object in the C sense.
>>
>> I will probably regret this later, but... What the hell do you mean?
>
> Now you did it. You reactivated him after a long dormant period.
I did? Damn... I thought he was replying to Paavo. Ah, no matter.
Sorry! My fault! I really regret this! Etc. etc.
V
--
I do not respond to top-posted replies, please don't ask
== 9 of 14 ==
Date: Fri, Feb 21 2014 3:20 pm
From: red floyd
On 2/21/2014 2:43 PM, Victor Bazarov wrote:
> I did? Damn... I thought he was replying to Paavo. Ah, no matter.
> Sorry! My fault! I really regret this! Etc. etc.
>
You are hereby sentenced to 200 hours of listening to Justin Bieber
albums.
== 10 of 14 ==
Date: Fri, Feb 21 2014 3:48 pm
From: Paavo Helde
Victor Bazarov <v.bazarov@comcast.invalid> wrote in news:le8kr9$trj$1@dont-
email.me:
> On 2/21/2014 4:45 PM, Osmium wrote:
>> "Victor Bazarov" wrote:
>>
>>> On 2/21/2014 4:28 PM, 88888 Dihedral wrote:
>>>> [...]
>>>> Well, an object in C++ has some hidden
>>>> field to store the address of this object in the C sense.
>>>
>>> I will probably regret this later, but... What the hell do you mean?
>>
>> Now you did it. You reactivated him after a long dormant period.
>
> I did? Damn... I thought he was replying to Paavo. Ah, no matter.
> Sorry! My fault! I really regret this! Etc. etc.
Yes, sorry, seems to be my fault. Must have been some special combination
of letters I guess ;-)
"In C++ the function declarationS specify exAcTly if the pAss is by value
or by reference. The people at stackoverflow probably have coNfused C++
with some other lAnguage. Do not take stackoverfloW too seriously, the
Answers there are ranKed by the pErson least qualified to judge the
correctness."
P.
== 11 of 14 ==
Date: Fri, Feb 21 2014 4:21 pm
From: Ian Collins
red floyd wrote:
> On 2/21/2014 2:43 PM, Victor Bazarov wrote:
>
>> I did? Damn... I thought he was replying to Paavo. Ah, no matter.
>> Sorry! My fault! I really regret this! Etc. etc.
>>
>
> You are hereby sentenced to 200 hours of listening to Justin Bieber
> albums.
That, being a cruel and inhumane punishment, would be considered a
breach of Victor's human rights by the European court :)
--
Ian Collins
== 12 of 14 ==
Date: Fri, Feb 21 2014 4:47 pm
From: Victor Bazarov
On 2/21/2014 7:21 PM, Ian Collins wrote:
> red floyd wrote:
>> On 2/21/2014 2:43 PM, Victor Bazarov wrote:
>>
>>> I did? Damn... I thought he was replying to Paavo. Ah, no matter.
>>> Sorry! My fault! I really regret this! Etc. etc.
>>>
>>
>> You are hereby sentenced to 200 hours of listening to Justin Bieber
>> albums.
>
> That, being a cruel and inhumane punishment, would be considered a
> breach of Victor's human rights by the European court :)
I still probably deserve it. Besides, I don't reside in the
jurisdiction of the European court :-[
V
--
I do not respond to top-posted replies, please don't ask
== 13 of 14 ==
Date: Fri, Feb 21 2014 10:40 pm
From: red floyd
On 2/21/2014 4:47 PM, Victor Bazarov wrote:
> On 2/21/2014 7:21 PM, Ian Collins wrote:
>> red floyd wrote:
>>> On 2/21/2014 2:43 PM, Victor Bazarov wrote:
>>>
>>>> I did? Damn... I thought he was replying to Paavo. Ah, no matter.
>>>> Sorry! My fault! I really regret this! Etc. etc.
>>>>
>>>
>>> You are hereby sentenced to 200 hours of listening to Justin Bieber
>>> albums.
>>
>> That, being a cruel and inhumane punishment, would be considered a
>> breach of Victor's human rights by the European court :)
>
> I still probably deserve it. Besides, I don't reside in the
> jurisdiction of the European court :-[
Just giving you a hard time, Victor. The opportunity was too good
to pass up. :-D
== 14 of 14 ==
Date: Sat, Feb 22 2014 12:25 am
From: Jorgen Grahn
On Fri, 2014-02-21, Marcel Müller wrote:
> On 21.02.14 12.44, Jorgen Grahn wrote:
>> On Fri, 2014-02-21, Paavo Helde wrote:
>>> In C++ the function declarations specify exactly if the pass is by value
>>> or by reference.
>>
>> Or you could say it's always by value -- but you can choose to pass a
>> reference or pointer to some object.
>
> Strictly speaking it is up to the platform how references are
> implemented. The option that they are binary compatible to pointers is
> only a common solution.
Of course. Note that I didn't imply that references are pointers --
just that both can be used if you want "pass by reference" semantics.
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
==============================================================================
TOPIC: Working with Large Values (double)
http://groups.google.com/group/comp.lang.c++/t/e42843c9cdf13724?hl=en
==============================================================================
== 1 of 10 ==
Date: Thurs, Feb 20 2014 9:33 pm
From: Paavo Helde
mrc2323@cox.net (Mike Copeland) wrote in
news:MPG.2d704afdc9581daf989766@news.eternal-september.org:
> How can I express a large value as a double? (e.g. 1000000.3)
> Whereas this compiles and executes, when I try to convert it to a
> string value it converts as a scientific string (e.g. "1e+006", not
> "1000000.3"). I want to process all the characters of the value of
> the data as a std::string.
> Or is there a way to convert the double to assure it's not
> expressed
> in scientific notation? TIA
Yes, there are several ways to achieve that. Example:
#include <iostream>
#include <sstream>
#include <iomanip>
int main() {
double x = 10000.3e+34;
std::ostringstream oss;
oss << std::fixed << std::setprecision(100) << x;
std::string s = oss.str();
std::cout << s << "\n";
}
This prints:
"100003000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
hth
Paavo
== 2 of 10 ==
Date: Thurs, Feb 20 2014 10:00 pm
From: Robert Wessel
On Thu, 20 Feb 2014 17:25:50 -0700, mrc2323@cox.net (Mike Copeland)
wrote:
> How can I express a large value as a double? (e.g. 1000000.3)
> Whereas this compiles and executes, when I try to convert it to a
>string value it converts as a scientific string (e.g. "1e+006", not
>"1000000.3"). I want to process all the characters of the value of the
>data as a std::string.
> Or is there a way to convert the double to assure it's not expressed
>in scientific notation? TIA
Doubles are floating point and are inherently "in" scientific
notation.
But if you've converted a double to a character format, you can
usually specify a precision, if the default (commonly 6) is not what
you want. With streams, for example, you can use std::setprecision().
== 3 of 10 ==
Date: Fri, Feb 21 2014 6:51 am
From: scott@slp53.sl.home (Scott Lurndal)
mrc2323@cox.net (Mike Copeland) writes:
> How can I express a large value as a double? (e.g. 1000000.3)
> Whereas this compiles and executes, when I try to convert it to a
>string value it converts as a scientific string (e.g. "1e+006", not
>"1000000.3"). I want to process all the characters of the value of the
>data as a std::string.
> Or is there a way to convert the double to assure it's not expressed
>in scientific notation? TIA
man 3 snprintf
(Yes, it works fine in C++)
== 4 of 10 ==
Date: Fri, Feb 21 2014 10:33 am
From: mrc2323@cox.net (Mike Copeland)
In article <miqdg9p89gkk0v3cau5im48mddhjmejeu1@4ax.com>, robertwessel2
@yahoo.com says...
> > How can I express a large value as a double? (e.g. 1000000.3)
> > Whereas this compiles and executes, when I try to convert it to a
> >string value it converts as a scientific string (e.g. "1e+006", not
> >"1000000.3"). I want to process all the characters of the value of the
> >data as a std::string.
> > Or is there a way to convert the double to assure it's not expressed
> >in scientific notation? TIA
>
>
> Doubles are floating point and are inherently "in" scientific
> notation.
>
> But if you've converted a double to a character format, you can
> usually specify a precision, if the default (commonly 6) is not what
> you want. With streams, for example, you can use std::setprecision().
Yes, a combination of fixed, setw and setprecision do what I want:
double inVal = 10000000.3;
ostringstream ossw;
ossw.str(""), ossw << fixed << setw(10) << setprecision(2) << inVal
<< ends;
str = ossw.str();
Thanks!
---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com
== 5 of 10 ==
Date: Fri, Feb 21 2014 11:41 am
From: jacob navia
Le 21/02/2014 06:33, Paavo Helde a écrit :
> mrc2323@cox.net (Mike Copeland) wrote in
> news:MPG.2d704afdc9581daf989766@news.eternal-september.org:
>
>> How can I express a large value as a double? (e.g. 1000000.3)
>> Whereas this compiles and executes, when I try to convert it to a
>> string value it converts as a scientific string (e.g. "1e+006", not
>> "1000000.3"). I want to process all the characters of the value of
>> the data as a std::string.
>> Or is there a way to convert the double to assure it's not
>> expressed
>> in scientific notation? TIA
>
> Yes, there are several ways to achieve that. Example:
>
> #include <iostream>
> #include <sstream>
> #include <iomanip>
>
> int main() {
> double x = 10000.3e+34;
>
> std::ostringstream oss;
> oss << std::fixed << std::setprecision(100) << x;
> std::string s = oss.str();
>
> std::cout << s << "\n";
> }
>
> This prints:
> "100003000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
>
> hth
> Paavo
>
Sorry but compiling with gcc gives
100002999999999994323893219714356215808.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Which compiler did you use?
== 6 of 10 ==
Date: Fri, Feb 21 2014 11:54 am
From: Victor Bazarov
On 2/21/2014 2:41 PM, jacob navia wrote:
> Le 21/02/2014 06:33, Paavo Helde a écrit :
>> mrc2323@cox.net (Mike Copeland) wrote in
>> news:MPG.2d704afdc9581daf989766@news.eternal-september.org:
>>
>>> How can I express a large value as a double? (e.g. 1000000.3)
>>> Whereas this compiles and executes, when I try to convert it to a
>>> string value it converts as a scientific string (e.g. "1e+006", not
>>> "1000000.3"). I want to process all the characters of the value of
>>> the data as a std::string.
>>> Or is there a way to convert the double to assure it's not
>>> expressed
>>> in scientific notation? TIA
>>
>> Yes, there are several ways to achieve that. Example:
>>
>> #include <iostream>
>> #include <sstream>
>> #include <iomanip>
>>
>> int main() {
>> double x = 10000.3e+34;
>>
>> std::ostringstream oss;
>> oss << std::fixed << std::setprecision(100) << x;
>> std::string s = oss.str();
>>
>> std::cout << s << "\n";
>> }
>>
>> This prints:
>> "100003000000000000000000000000000000000.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
>>
>>
>> hth
>> Paavo
>>
>
> Sorry but compiling with gcc gives
> 100002999999999994323893219714356215808.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
>
>
> Which compiler did you use?
*I* get the crisp "3000" with VC++ 2013 Express, for instance. Not sure
about Paavo, of course.
V
--
I do not respond to top-posted replies, please don't ask
== 7 of 10 ==
Date: Fri, Feb 21 2014 12:20 pm
From: Paavo Helde
jacob navia <jacob@spamsink.net> wrote in
news:le8a54$8rn$1@speranza.aioe.org:
>>
>> #include <iostream>
>> #include <sstream>
>> #include <iomanip>
>>
>> int main() {
>> double x = 10000.3e+34;
>>
>> std::ostringstream oss;
>> oss << std::fixed << std::setprecision(100) << x;
>> std::string s = oss.str();
>>
>> std::cout << s << "\n";
>> }
>>
>> This prints:
>> "100003000000000000000000000000000000000.00000000000000000000000000000
>> 0000000000000000000000000000000000000000000000000000000000000000000000
>> "
>>
>> hth
>> Paavo
>>
>
> Sorry but compiling with gcc gives
> 100002999999999994323893219714356215808.0000000000000000000000000000000
> 000000000000000000000000000000000000000000000000000000000000000000000
>
> Which compiler did you use?
Microsoft VS2012 Professional, 64-bit mode.
I guess both compilers produce correct output. I like gcc behavior more,
it shows better what one can get when blindly following a whim like "I
don't like scientific notation".
Cheers
Paavo
== 8 of 10 ==
Date: Fri, Feb 21 2014 12:28 pm
From: Paavo Helde
scott@slp53.sl.home (Scott Lurndal) wrote in news:PJJNu.5151$7V3.3521
@fx18.iad:
>
> man 3 snprintf
>
> (Yes, it works fine in C++)
Except that it is part of C99 and not present in MSVC++, which is what Mike
is using (to add confusion, there is nonstandard _snprintf with slightly
different interface).
Cheers
Paavo
== 9 of 10 ==
Date: Fri, Feb 21 2014 1:14 pm
From: Geoff
On Fri, 21 Feb 2014 14:20:42 -0600, Paavo Helde
<myfirstname@osa.pri.ee> wrote:
>jacob navia <jacob@spamsink.net> wrote in
>news:le8a54$8rn$1@speranza.aioe.org:
>>>
>>> #include <iostream>
>>> #include <sstream>
>>> #include <iomanip>
>>>
>>> int main() {
>>> double x = 10000.3e+34;
>>>
>>> std::ostringstream oss;
>>> oss << std::fixed << std::setprecision(100) << x;
>>> std::string s = oss.str();
>>>
>>> std::cout << s << "\n";
>>> }
>>>
>>> This prints:
>>> "100003000000000000000000000000000000000.00000000000000000000000000000
>>> 0000000000000000000000000000000000000000000000000000000000000000000000
>>> "
>>>
>>> hth
>>> Paavo
>>>
>>
>> Sorry but compiling with gcc gives
>> 100002999999999994323893219714356215808.0000000000000000000000000000000
>> 000000000000000000000000000000000000000000000000000000000000000000000
>>
>> Which compiler did you use?
>
>Microsoft VS2012 Professional, 64-bit mode.
>
>I guess both compilers produce correct output. I like gcc behavior more,
>it shows better what one can get when blindly following a whim like "I
>don't like scientific notation".
>
Copeland is writing a check writing program, I think scientific
notation might not work in that case. :)
== 10 of 10 ==
Date: Fri, Feb 21 2014 5:28 pm
From: Robert Wessel
On Fri, 21 Feb 2014 13:14:58 -0800, Geoff <geoff@invalid.invalid>
wrote:
>On Fri, 21 Feb 2014 14:20:42 -0600, Paavo Helde
><myfirstname@osa.pri.ee> wrote:
>
>>jacob navia <jacob@spamsink.net> wrote in
>>news:le8a54$8rn$1@speranza.aioe.org:
>>>>
>>>> #include <iostream>
>>>> #include <sstream>
>>>> #include <iomanip>
>>>>
>>>> int main() {
>>>> double x = 10000.3e+34;
>>>>
>>>> std::ostringstream oss;
>>>> oss << std::fixed << std::setprecision(100) << x;
>>>> std::string s = oss.str();
>>>>
>>>> std::cout << s << "\n";
>>>> }
>>>>
>>>> This prints:
>>>> "100003000000000000000000000000000000000.00000000000000000000000000000
>>>> 0000000000000000000000000000000000000000000000000000000000000000000000
>>>> "
>>>>
>>>> hth
>>>> Paavo
>>>>
>>>
>>> Sorry but compiling with gcc gives
>>> 100002999999999994323893219714356215808.0000000000000000000000000000000
>>> 000000000000000000000000000000000000000000000000000000000000000000000
>>>
>>> Which compiler did you use?
>>
>>Microsoft VS2012 Professional, 64-bit mode.
>>
>>I guess both compilers produce correct output. I like gcc behavior more,
>>it shows better what one can get when blindly following a whim like "I
>>don't like scientific notation".
>>
>
>Copeland is writing a check writing program, I think scientific
>notation might not work in that case. :)
Attempting to use FP to represent currency is fundamentally doomed to
failure. I didn't see that that's what the OP was doing, but if it
is, he should definitely reconsider, unless he doesn't actually care
about the actual results (which may well be the case if this something
like a homework assignment).
==============================================================================
TOPIC: OT: Problem building libc++
http://groups.google.com/group/comp.lang.c++/t/177ee9b847d7540f?hl=en
==============================================================================
== 1 of 1 ==
Date: Fri, Feb 21 2014 6:50 am
From: scott@slp53.sl.home (Scott Lurndal)
woodbrian77@gmail.com writes:
>
>I was following these instructions
>
>http://solarianprogrammer.com/2013/01/17/building-clang-libcpp-ubuntu-linux/
>http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CCYQFjAA&url=http%3A%2F%2Fwiki.osdev.org%2FLibsupcxx&ei=o2YHU72EO4ex2wX2koDAAQ&usg=AFQjCNF6KTOFeb5SBX6SBywdREYfhy63ng
http://webcache.googleusercontent.com/search?q=cache:4D214uOlD3QJ:wiki.osdev.org/Libsupcxx+
$ locate libsup
/usr/lib/gcc/x86_64-redhat-linux/4.4.4/libsupc++.a
scott
==============================================================================
TOPIC: shared_ptr and unique_ptr related question
http://groups.google.com/group/comp.lang.c++/t/ae1f30d2345574fe?hl=en
==============================================================================
== 1 of 1 ==
Date: Fri, Feb 21 2014 5:28 pm
From: somenath
To get some understanding of shared_ptr and unique_ptr I wrote the following naive implementation of linked list.
#include<memory>
using namespace std;
template <class T>
class List {
private:
class ListItem {
public:
ListItem( T Val );
shared_ptr <ListItem > Next;
T Data;
};
shared_ptr< ListItem >Head;
public:
int CreateNode();
List() {
Head.reset();
}
void PushBack(T Val);
void Dump();
};
template<class T>
List<T>::ListItem::ListItem( T Val):Data(Val) {
Next.reset();
}
template<class T>
void List<T>::PushBack( T val)
{
unique_ptr<ListItem > NewItem (new ListItem(val));
if (!Head ) {
Head = move(NewItem);
}
else {
shared_ptr<ListItem> Curr(Head);
shared_ptr<ListItem> Prev(Head);
while( (Curr) ) {
Prev = Curr;
Curr = Curr->Next;
}
Prev->Next = move(NewItem);
}
}
template<class T>
void List<T>::Dump() {
shared_ptr<ListItem > Curr(Head);
while ( Curr) {
cout<<"Val = "<<Curr->Data<<endl;;
Curr = Curr->Next;
}
}
But I am not very clear of the correct uses of shared_ptr and auto_ptr yet. Could you please comment on the uses of smart pointers in the context of my linked list code. According to my understanding where I do not need to assign smart pointers to other one I use unique_ptr . Is this understanding correct?
Also I am not able to get convinced the benefit of shared_ptr and unique_ptr in the context of my code. I could have used even raw pointers to implement the same without losing much benefit . Is it not the case here?
I can use the above code as
int main(int argc,char *argv[] )
{
unique_ptr< List <int> > ilst (new List<int>());
ilst->PushBack(5);
ilst->PushBack(15);
ilst->PushBack(25);
ilst->Dump();
return 0;
}
Is the benefit of using smart pointers is, ilst need to be freed manually?
Please point me to some real code where smart pointers has been heavily used.
--
Somenath
==============================================================================
You received this message because you are subscribed to the Google Groups "comp.lang.c++"
group.
To post to this group, visit http://groups.google.com/group/comp.lang.c++?hl=en
To unsubscribe from this group, send email to comp.lang.c+++unsubscribe@googlegroups.com
To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.c++/subscribe?hl=en
To report abuse, send email explaining the problem to abuse@googlegroups.com
==============================================================================
Google Groups: http://groups.google.com/?hl=en
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment