copy constructor

S

Stephane Vollet

Hi,

Can someone explain this to me, please. Why is it m_values[] that takes
what's in v.m_values[] ?

I don't understand because it's Tvector& v in parameter...

Maybe it's too obvious to be explain...

Tvector(const Tvector& v)
{
m_values=new long [m_capacity];
for(int i=0;i<m_capacity;i++)
m_values=v.m_values;
}

thanks
 
D

Dave

Stephane Vollet said:
Hi,

Can someone explain this to me, please. Why is it m_values[] that takes
what's in v.m_values[] ?

I don't understand because it's Tvector& v in parameter...

Maybe it's too obvious to be explain...

Tvector(const Tvector& v)
{
m_values=new long [m_capacity];
for(int i=0;i<m_capacity;i++)
m_values=v.m_values;
}

thanks


This code is trying to make a new Tvector that is an exact copy of an
existing Tvector.

One point:

You're using m_capacity without having initialized it. It needs to be set
to be equal to the m_capacity of the Tvector you're copying.

Also, if Tvector is supposed to have the same semantics as std::vector<>,
you don't want to copy the entire capacity. Only copy the array elements
that are actually in use. But if these are the right semantics for this
class (meaning that it's not like std::vector<>), please disregard this
comment.
 
E

Efrat Regev

Stephane Vollet said:
Hi,

Can someone explain this to me, please. Why is it m_values[] that takes
what's in v.m_values[] ?

I don't understand because it's Tvector& v in parameter...

Maybe it's too obvious to be explain...

Tvector(const Tvector& v)
{
m_values=new long [m_capacity];
for(int i=0;i<m_capacity;i++)
m_values=v.m_values;
}

thanks


TVector's constructor has an argument:
const Tvector &v

which means that v is a *const reference*. What does that mean? I suggest
you check in "The C++ Programming Language" by Stroustrup. However, this is
*very very roughly* equivalent to
Tvector(const Tvector *const pv)
{
m_values=new long [m_capacity];
for(int i=0;i<m_capacity;i++)
m_values=pv->m_values;
}

I hope this second version is more understandable.

In your place, I think I would do the following:
1. Read Stroustrup's book, at least for the definition of a *reference*
(since the translation I performed above from
const Tvector &v
to
const Tvector *const pv
is not quite correct (it is just for your understanding of your code
snippet)).
2. Read any book on the STL (once again, Stroustrup's book should be fine),
and consider whether you really need (among others)
a. To define a Tvector (instead of using std::vector)
b. To allocate memory using new[] (instead of using std::vector)
c. To write explicit loops like for(int i=0;i<m_capacity;i++) instead of
using std::copy
 

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
474,199
Messages
2,571,045
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top