returning an object by value using a non const copy constructorargument

R

Ralph

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();
}
 
F

Fran

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;
}
 
A

acehreli

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
 
R

Ralph

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top