Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > returning an object by value using a non const copy constructorargument

Reply
Thread Tools

returning an object by value using a non const copy constructorargument

 
 
Ralph
Guest
Posts: n/a
 
      06-11-2008
Hi,

I was wondering how to get the following working. I know std::auto_ptr
does something similar but how ?


Thanks,

Ralph.


class Test
{
public:
Test() : m_b(true) {};

// copy constructor which resets the object beeing copied
Test(Test &t) : m_b(t.m_b) { t.m_b = false; };

bool get() const { return m_b; };
private:
bool m_b;
};

Test f()
{
// this will not compile...
return Test();
}
 
Reply With Quote
 
 
 
 
Fran
Guest
Posts: n/a
 
      06-11-2008
On Jun 11, 3:07 pm, Ralph wrote:

> class Test
> {
> public:
> Test() : m_b(true) {};
>
> // copy constructor which resets the object being copied
> Test(Test &t) : m_b(t.m_b) { t.m_b = false; };


Why would you make your _copy_ constructor produce an object that is
not, in fact, a copy of the original object?

> bool get() const { return m_b; };
> private:
> bool m_b;
> };
>
> Test f()
> {
> // this will not compile...
> return Test();
> }


Try this instead:

Test f()
{
Test x;
return x;
}
 
Reply With Quote
 
 
 
 
acehreli@gmail.com
Guest
Posts: n/a
 
      06-11-2008
On Jun 11, 12:07 pm, Ralph <(E-Mail Removed)> wrote:
> Hi,
>
> I was wondering how to get the following working. I know std::auto_ptr
> does something similar but how ?


You may have already looked at your implementation of auto_ptr but it
uses an auxiliary class: auto_ptr_ref.

> Test f()
> {
> // this will not compile...
> return Test();
>
> }


Ali
 
Reply With Quote
 
Ralph
Guest
Posts: n/a
 
      06-12-2008
On Wed, 11 Jun 2008 12:15:07 -0700, Fran wrote:

> On Jun 11, 3:07 pm, Ralph wrote:
>
>> class Test
>> {
>> public:
>> Test() : m_b(true) {};
>>
>> // copy constructor which resets the object being copied
>> Test(Test &t) : m_b(t.m_b) { t.m_b = false; };

>
> Why would you make your _copy_ constructor produce an object that is
> not, in fact, a copy of the original object?


Well std::auto_ptr can do it and I was just curious how it works.


> Test f()
> {
> Test x;
> return x;
> }


Maybe, but the other should be possible, at least std::auto_ptr does it.
 
Reply With Quote
 
Ralph
Guest
Posts: n/a
 
      06-12-2008
On Wed, 11 Jun 2008 13:59:51 -0700, acehreli wrote:

>> I was wondering how to get the following working. I know std::auto_ptr
>> does something similar but how ?

>
> You may have already looked at your implementation of auto_ptr but it
> uses an auxiliary class: auto_ptr_ref.


I looked at an implementation but it did not made much sense to me,
however I found another one which actually has comments about the
auto_ptr_ref stuff and it makes more sense now.

I found it here:
http://gcc.gnu.org/onlinedocs/libstd...en/a01857.html

Ralph.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Const/non-const pointer returning method Jens Thoms Toerring C++ 11 05-26-2010 04:51 PM
Modifying non-const object from const function? fdm C++ 11 09-01-2009 03:14 PM
Re: Modifying non-const object from const function? Francesco C++ 0 08-31-2009 11:30 AM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM



Advertisments