Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Operator () overloading, cannot return by value.

Reply
Thread Tools

Operator () overloading, cannot return by value.

 
 
fabian.lim@gmail.com
Guest
Posts: n/a
 
      09-08-2008
Hi All,

Im a newbie to C++, I am trying to customize the vector template
STL to a template Class. My code is shown below. Im confused about
something and maybe somebody here might be able to help me.

Lets say X is my vector, which contains 10 elements. I want to
assign elements indexed 2 to 5 to another vector Y, so I want to do it
like Y = X(2,5). So what I do is i overload the () operators, as
marked in (a) below. This creates a new intermidate Vector class Z,
via the constructor that is marked (b) below. The intermediate vector
class is returned to (a), which thens try to return by value. However
my memory always gets destroyed. This makes sense because I created a
variable which has scope only in (a).

However, when I add to vectors X + Y, again an intermediate result
is created. This is done in (c) below. The X (lhs) argument is used to
call another constructor which then creates a copy Z, and adds Y (rhs)
to that copy Z. Now Z is then returned but it does not get destroyed.

So to sum up, why does memory gets destroyed in the first case but
not in the second. And how to avoid this problem in the first case?

Thanks in advance.

/*------------------------------------------------
This is package for Vectors, Matrices (Template Class)
--------------------------------------------------*/
#include <iostream>
#include <vector>
#include <math.h>
#define to ,
using namespace std;

//********************** START: VECTOR *************************
template <typename TYPE>
class Vector {
vector<TYPE> v;
public:
//************************************************** ******
// Selectors
const TYPE& operator[](const int& k) const { return v[k]; } //default
[] indexing
const TYPE& operator()(const int& k) const {return v[k];}
int size() const {return v.size();} //returns the size

const Vector<TYPE> operator()(const int& i,const int& j) <---- (a)
return Vector<TYPE>(v, i, j);

//************************************************** ******
// Constructors and Destructor
Vector() { };
~Vector(){ };

//************************************************** ******
//plus operation
Vector<TYPE>& operator+=(const Vector<TYPE>& rhs){
for (int k = 0; k < rhs.size(); k++){
if (k < v.size())
v[k] += rhs[k];
else v.push_back(rhs[k]);
}
return *this;
}

//************************************************** ******
// Copiers
//copy constructors
Vector( const Vector<TYPE>& val) { //copy constructor for
intermediate results (like x + y)
for (int k = 0; k < val.size(); k++)
v.push_back(val[k]);
}
Vector( const Vector<TYPE>& val, const int& i, const int& j)
{ <----- (b)
//this constructor is used to create copy when indexing a range
for (int k = i; k <=j; k++){
if (k<val.size())
v.push_back(val[k]);
else v.push_back(0);
}
}

//assignments
Vector<TYPE>& operator=(const Vector<TYPE>& rhs) {
v.clear();
for (int k = 0; k < rhs.size(); k++)
v.push_back(rhs[k]);
return *this;
}
};

//************************************************** ******
//plus operation
template <typename TYPE>
inline const Vector<TYPE> operator+(const Vector<TYPE>& lhs, const
Vector<TYPE>& rhs)
{
return Vector<TYPE>(lhs) += rhs; <----- (c)
}


//********************** END: VECTOR *************************
 
Reply With Quote
 
 
 
 
maverik
Guest
Posts: n/a
 
      09-08-2008
Hi fabian.
May be difference in this:

const Vector<TYPE> operator()(const int& i,const int& j) <---- (a)
{...}
Vector<TYPE>& operator+=(const Vector<TYPE>& rhs){...}

Should you use:

const Vector<TYPE>& operator()(const int& i,const int& j) <---- (a)
{...}

instead

const Vector<TYPE> operator()(const int& i,const int& j) <---- (a)
{...}
 
Reply With Quote
 
 
 
 
maverik
Guest
Posts: n/a
 
      09-08-2008
Hi fabian.
May be difference in this:

const Vector<TYPE> operator()(const int& i,const int& j) <---- (a)
{...}
Vector<TYPE>& operator+=(const Vector<TYPE>& rhs){...}

Should you use:

const Vector<TYPE>& operator()(const int& i,const int& j) <---- (a)
{...}

instead

const Vector<TYPE> operator()(const int& i,const int& j) <---- (a)
{...}
 
Reply With Quote
 
newbarker@gmail.com
Guest
Posts: n/a
 
      09-08-2008
On 8 Sep, 08:53, fabian....@gmail.com wrote:
> Hi All,
>
> * *Im a newbie to C++, I am trying to customize the vector template
> STL to a template Class. My code is shown below. Im confused about
> something and maybe somebody here might be able to help me.
>
> * *Lets say X is my vector, which contains 10 elements. I want to
> assign elements indexed 2 to 5 to another vector Y, so I want to do it
> like Y = X(2,5). So what I do is i overload the () operators, as
> marked in (a) below. This creates a new intermidate Vector class Z,
> via the constructor that is marked (b) below. The intermediate vector
> class is returned to (a), which thens try to return by value. However
> my memory always gets destroyed. This makes sense because I created a
> variable which has scope only in (a).
>
> * *However, when I add to vectors X + Y, again an intermediate result
> is created. This is done in (c) below. The X (lhs) argument is used to
> call another constructor which then creates a copy Z, and adds Y (rhs)
> to that copy Z. Now Z is then returned but it does not get destroyed.
>
> * *So to sum up, why does memory gets destroyed in the first case but
> not in the second. And how to avoid this problem in the first case?
>
> Thanks in advance.
>
> /*------------------------------------------------
> This is package for Vectors, Matrices (Template Class)
> --------------------------------------------------*/
> #include <iostream>
> #include <vector>
> #include <math.h>
> #define to ,
> using namespace std;
>
> //********************** START: VECTOR *************************
> template <typename TYPE>
> class Vector {
> * * * * vector<TYPE> v;
> public:
> * * * * //************************************************** ******
> * * * * // * *Selectors
> * * * * const TYPE& operator[](const int& k) const { return v[k]; } //default
> [] indexing
> * * * * const TYPE& operator()(const int& k) const {return v[k];}
> * * * * int size() const {return v.size();} //returns the size
>
> * * * * const Vector<TYPE> operator()(const int& i,const int& j) <---- (a)
> * * * * * * * * return Vector<TYPE>(v, i, j);
>
> * * * * //************************************************** ******
> * * * * // * *Constructors and Destructor
> * * * * Vector() { };
> * * * * ~Vector(){ };
>
> * * * * //************************************************** ******
> * * * * //plus operation
> * * * * Vector<TYPE>& operator+=(const Vector<TYPE>& rhs){
> * * * * * * * * for (int k = 0; k < rhs.size(); k++){
> * * * * * * * * * * * * if (k < v.size())
> * * * * * * * * * * * * * * * * v[k] += rhs[k];
> * * * * * * * * * * * * else v.push_back(rhs[k]);
> * * * * * * * * }
> * * * * * * * * return *this;
> * * * * }
>
> * * * * //************************************************** ******
> * * * * // * *Copiers
> * * * * //copy constructors
> * * * * Vector( const Vector<TYPE>& val) { //copy constructor for
> intermediate results (like x + y)
> * * * * * * * * for (int k = 0; k < val.size(); k++)
> * * * * * * * * * * * * v.push_back(val[k]);
> * * * * }
> * * * * Vector( const Vector<TYPE>& val, const int& i, const int& j)
> { * *<----- (b)
> * * * * * * * * //this constructor is used to create copy when indexing a range
> * * * * * * * * for (int k = i; k <=j; k++){
> * * * * * * * * * * * * if (k<val.size())
> * * * * * * * * * * * * * * * * v.push_back(val[k]);
> * * * * * * * * * * * * else v.push_back(0);
> * * * * * * * * }
> * * * * }
>
> * * * * //assignments
> * * * * Vector<TYPE>& operator=(const Vector<TYPE>& rhs) {
> * * * * * * * * v.clear();
> * * * * * * * * for (int k = 0; k < rhs.size(); k++)
> * * * * * * * * * * * * v.push_back(rhs[k]);
> * * * * * * * * return *this;
> * * * * }
>
> };
>
> //************************************************** ******
> //plus operation
> template <typename TYPE>
> inline const Vector<TYPE> operator+(const Vector<TYPE>& lhs, const
> Vector<TYPE>& rhs)
> {
> * * * * return Vector<TYPE>(lhs) += rhs; * <----- (c)
>
> }
>
> //********************** END: VECTOR *************************


Hi,

I took the code, added a method to populate the vector and then it
failed to compile. I changed the following:

const Vector<TYPE> operator()(const int& i,const int& j) <---- (a)
return Vector<TYPE>(v, i, j);

to this:

const Vector<TYPE> operator()(const int& i,const int& j) // <---- (a)
{
return Vector<TYPE>(*this, i, j); // <--- Changed to pass in
*this, NOT v
}

and it worked. Your code was passing in the member variable v but
there's no constructor on Vector for std::vector! I don't know how you
got it to compile??

The following program seemed to work okay and outputs "3,4,5" to the
standard output:

/*------------------------------------------------
This is package for Vectors, Matrices (Template Class)
--------------------------------------------------*/
#include <iostream>
#include <vector>
#include <math.h>
#define to ,
using namespace std;


//********************** START: VECTOR *************************
template <typename TYPE>
class Vector {
vector<TYPE> v;
public:
//************************************************** ******
// Selectors
const TYPE& operator[](const int& k) const { return v[k]; } //
default [] indexing
const TYPE& operator()(const int& k) const {return v[k];}
int size() const {return v.size();} //returns the size


const Vector<TYPE> operator()(const int& i,const int& j)
{
return Vector<TYPE>(*this, i, j);
}


//************************************************** ******
// Constructors and Destructor
Vector() { };
~Vector(){ };


//************************************************** ******
//plus operation
Vector<TYPE>& operator+=(const Vector<TYPE>& rhs){
for (int k = 0; k < rhs.size(); k++){
if (k < v.size())
v[k] += rhs[k];
else v.push_back(rhs[k]);
}
return *this;
}


//************************************************** ******
// Copiers
//copy constructors
Vector( const Vector<TYPE>& val) { //copy constructor for
intermediate results (like x + y)
for (int k = 0; k < val.size(); k++)
v.push_back(val[k]);
}
Vector( const Vector<TYPE>& val, const int& i, const int& j)
{
//this constructor is used to create copy when
indexing a range
for (int k = i; k <=j; k++){
if (k<val.size())
v.push_back(val[k]);
else v.push_back(0);
}
}


//assignments
Vector<TYPE>& operator=(const Vector<TYPE>& rhs) {
v.clear();
for (int k = 0; k < rhs.size(); k++)
v.push_back(rhs[k]);
return *this;
}

void add(const TYPE& t)
{
v.push_back(t);
}
};


//************************************************** ******
//plus operation
template <typename TYPE>
inline const Vector<TYPE> operator+(const Vector<TYPE>& lhs, const
Vector<TYPE>& rhs)
{
return Vector<TYPE>(lhs) += rhs;
}

int main()
{
Vector<int> vi;
vi.add(1);
vi.add(2);
vi.add(3);
vi.add(4);
vi.add(5);
vi.add(6);

Vector<int> copyVec = vi(2,4);

std::cout << copyVec[0] << ',' << copyVec[1] << ',' << copyVec[2] <<
std::endl;

}
 
Reply With Quote
 
newbarker@gmail.com
Guest
Posts: n/a
 
      09-08-2008
On 8 Sep, 08:53, fabian....@gmail.com wrote:
> Hi All,
>
> * *Im a newbie to C++, I am trying to customize the vector template
> STL to a template Class. My code is shown below. Im confused about
> something and maybe somebody here might be able to help me.
>
> * *Lets say X is my vector, which contains 10 elements. I want to
> assign elements indexed 2 to 5 to another vector Y, so I want to do it
> like Y = X(2,5). So what I do is i overload the () operators, as
> marked in (a) below. This creates a new intermidate Vector class Z,
> via the constructor that is marked (b) below. The intermediate vector
> class is returned to (a), which thens try to return by value. However
> my memory always gets destroyed. This makes sense because I created a
> variable which has scope only in (a).
>
> * *However, when I add to vectors X + Y, again an intermediate result
> is created. This is done in (c) below. The X (lhs) argument is used to
> call another constructor which then creates a copy Z, and adds Y (rhs)
> to that copy Z. Now Z is then returned but it does not get destroyed.
>
> * *So to sum up, why does memory gets destroyed in the first case but
> not in the second. And how to avoid this problem in the first case?
>
> Thanks in advance.
>
> /*------------------------------------------------
> This is package for Vectors, Matrices (Template Class)
> --------------------------------------------------*/
> #include <iostream>
> #include <vector>
> #include <math.h>
> #define to ,
> using namespace std;
>
> //********************** START: VECTOR *************************
> template <typename TYPE>
> class Vector {
> * * * * vector<TYPE> v;
> public:
> * * * * //************************************************** ******
> * * * * // * *Selectors
> * * * * const TYPE& operator[](const int& k) const { return v[k]; } //default
> [] indexing
> * * * * const TYPE& operator()(const int& k) const {return v[k];}
> * * * * int size() const {return v.size();} //returns the size
>
> * * * * const Vector<TYPE> operator()(const int& i,const int& j) <---- (a)
> * * * * * * * * return Vector<TYPE>(v, i, j);
>
> * * * * //************************************************** ******
> * * * * // * *Constructors and Destructor
> * * * * Vector() { };
> * * * * ~Vector(){ };
>
> * * * * //************************************************** ******
> * * * * //plus operation
> * * * * Vector<TYPE>& operator+=(const Vector<TYPE>& rhs){
> * * * * * * * * for (int k = 0; k < rhs.size(); k++){
> * * * * * * * * * * * * if (k < v.size())
> * * * * * * * * * * * * * * * * v[k] += rhs[k];
> * * * * * * * * * * * * else v.push_back(rhs[k]);
> * * * * * * * * }
> * * * * * * * * return *this;
> * * * * }
>
> * * * * //************************************************** ******
> * * * * // * *Copiers
> * * * * //copy constructors
> * * * * Vector( const Vector<TYPE>& val) { //copy constructor for
> intermediate results (like x + y)
> * * * * * * * * for (int k = 0; k < val.size(); k++)
> * * * * * * * * * * * * v.push_back(val[k]);
> * * * * }
> * * * * Vector( const Vector<TYPE>& val, const int& i, const int& j)
> { * *<----- (b)
> * * * * * * * * //this constructor is used to create copy when indexing a range
> * * * * * * * * for (int k = i; k <=j; k++){
> * * * * * * * * * * * * if (k<val.size())
> * * * * * * * * * * * * * * * * v.push_back(val[k]);
> * * * * * * * * * * * * else v.push_back(0);
> * * * * * * * * }
> * * * * }
>
> * * * * //assignments
> * * * * Vector<TYPE>& operator=(const Vector<TYPE>& rhs) {
> * * * * * * * * v.clear();
> * * * * * * * * for (int k = 0; k < rhs.size(); k++)
> * * * * * * * * * * * * v.push_back(rhs[k]);
> * * * * * * * * return *this;
> * * * * }
>
> };
>
> //************************************************** ******
> //plus operation
> template <typename TYPE>
> inline const Vector<TYPE> operator+(const Vector<TYPE>& lhs, const
> Vector<TYPE>& rhs)
> {
> * * * * return Vector<TYPE>(lhs) += rhs; * <----- (c)
>
> }
>
> //********************** END: VECTOR *************************


Hi,

I took the code, added a method to populate the vector and then it
failed to compile. I changed the following:

const Vector<TYPE> operator()(const int& i,const int& j) <---- (a)
return Vector<TYPE>(v, i, j);

to this:

const Vector<TYPE> operator()(const int& i,const int& j) // <---- (a)
{
return Vector<TYPE>(*this, i, j); // <--- Changed to pass in
*this, NOT v
}

and it worked. Your code was passing in the member variable v but
there's no constructor on Vector for std::vector! I don't know how you
got it to compile??

The following program seemed to work okay and outputs "3,4,5" to the
standard output:

/*------------------------------------------------
This is package for Vectors, Matrices (Template Class)
--------------------------------------------------*/
#include <iostream>
#include <vector>
#include <math.h>
#define to ,
using namespace std;


//********************** START: VECTOR *************************
template <typename TYPE>
class Vector {
vector<TYPE> v;
public:
//************************************************** ******
// Selectors
const TYPE& operator[](const int& k) const { return v[k]; } //
default [] indexing
const TYPE& operator()(const int& k) const {return v[k];}
int size() const {return v.size();} //returns the size


const Vector<TYPE> operator()(const int& i,const int& j)
{
return Vector<TYPE>(*this, i, j);
}


//************************************************** ******
// Constructors and Destructor
Vector() { };
~Vector(){ };


//************************************************** ******
//plus operation
Vector<TYPE>& operator+=(const Vector<TYPE>& rhs){
for (int k = 0; k < rhs.size(); k++){
if (k < v.size())
v[k] += rhs[k];
else v.push_back(rhs[k]);
}
return *this;
}


//************************************************** ******
// Copiers
//copy constructors
Vector( const Vector<TYPE>& val) { //copy constructor for
intermediate results (like x + y)
for (int k = 0; k < val.size(); k++)
v.push_back(val[k]);
}
Vector( const Vector<TYPE>& val, const int& i, const int& j)
{
//this constructor is used to create copy when
indexing a range
for (int k = i; k <=j; k++){
if (k<val.size())
v.push_back(val[k]);
else v.push_back(0);
}
}


//assignments
Vector<TYPE>& operator=(const Vector<TYPE>& rhs) {
v.clear();
for (int k = 0; k < rhs.size(); k++)
v.push_back(rhs[k]);
return *this;
}

void add(const TYPE& t)
{
v.push_back(t);
}
};


//************************************************** ******
//plus operation
template <typename TYPE>
inline const Vector<TYPE> operator+(const Vector<TYPE>& lhs, const
Vector<TYPE>& rhs)
{
return Vector<TYPE>(lhs) += rhs;
}

int main()
{
Vector<int> vi;
vi.add(1);
vi.add(2);
vi.add(3);
vi.add(4);
vi.add(5);
vi.add(6);

Vector<int> copyVec = vi(2,4);

std::cout << copyVec[0] << ',' << copyVec[1] << ',' << copyVec[2] <<
std::endl;

}
 
Reply With Quote
 
newbarker@gmail.com
Guest
Posts: n/a
 
      09-08-2008
On 8 Sep, 10:02, maverik <maverik.m...@gmail.com> wrote:
> Hi fabian.
> May be difference in this:
>
> const Vector<TYPE> *operator()(const int& i,const int& j) <---- (a)
> {...}
> * * * Vector<TYPE>& operator+=(const Vector<TYPE>& rhs){...}
>
> Should you use:
>
> const Vector<TYPE>& *operator()(const int& i,const int& j) <---- (a)
> {...}
>
> instead
>
> const Vector<TYPE> *operator()(const int& i,const int& j) <---- (a)
> {...}


No. If he changed operator() to return a reference, it would return a
reference to the temporary which would be out of scope once operator()
has completed. The reference would be referring to a dead object.
 
Reply With Quote
 
newbarker@gmail.com
Guest
Posts: n/a
 
      09-08-2008
On 8 Sep, 10:02, maverik <maverik.m...@gmail.com> wrote:
> Hi fabian.
> May be difference in this:
>
> const Vector<TYPE> *operator()(const int& i,const int& j) <---- (a)
> {...}
> * * * Vector<TYPE>& operator+=(const Vector<TYPE>& rhs){...}
>
> Should you use:
>
> const Vector<TYPE>& *operator()(const int& i,const int& j) <---- (a)
> {...}
>
> instead
>
> const Vector<TYPE> *operator()(const int& i,const int& j) <---- (a)
> {...}


No. If he changed operator() to return a reference, it would return a
reference to the temporary which would be out of scope once operator()
has completed. The reference would be referring to a dead object.
 
Reply With Quote
 
fabian.lim@gmail.com
Guest
Posts: n/a
 
      09-08-2008
Hi newbar and Maverik,

Thanks you both for your prompt reply. Newbar, I cant believe it
actually works. Yes you are right about lacking a constructor for
std:vector<TYPE>, actually I modified my code when i was posting and
made a mistake there.

The funny thing is I actually tried out the same thing you suggested
(before I posted), and I thought I saw that it didnt work, however
when I tried again after I read your post, it seemed to work. The only
thing is that I was programming on Windows before, and now I tried It
on a Mac.

I will go back to school tomorrow to try it out again on Windows.


On Sep 7, 11:22 pm, newbar...@gmail.com wrote:
> On 8 Sep, 10:02, maverik <maverik.m...@gmail.com> wrote:
>
>
>
> > Hi fabian.
> > May be difference in this:

>
> > const Vector<TYPE> operator()(const int& i,const int& j) <---- (a)
> > {...}
> > Vector<TYPE>& operator+=(const Vector<TYPE>& rhs){...}

>
> > Should you use:

>
> > const Vector<TYPE>& operator()(const int& i,const int& j) <---- (a)
> > {...}

>
> > instead

>
> > const Vector<TYPE> operator()(const int& i,const int& j) <---- (a)
> > {...}

>
> No. If he changed operator() to return a reference, it would return a
> reference to the temporary which would be out of scope once operator()
> has completed. The reference would be referring to a dead object.




 
Reply With Quote
 
fabian.lim@gmail.com
Guest
Posts: n/a
 
      09-08-2008
Hi newbar and Maverik,

Thanks you both for your prompt reply. Newbar, I cant believe it
actually works. Yes you are right about lacking a constructor for
std:vector<TYPE>, actually I modified my code when i was posting and
made a mistake there.

The funny thing is I actually tried out the same thing you suggested
(before I posted), and I thought I saw that it didnt work, however
when I tried again after I read your post, it seemed to work. The only
thing is that I was programming on Windows before, and now I tried It
on a Mac.

I will go back to school tomorrow to try it out again on Windows.


On Sep 7, 11:22 pm, newbar...@gmail.com wrote:
> On 8 Sep, 10:02, maverik <maverik.m...@gmail.com> wrote:
>
>
>
> > Hi fabian.
> > May be difference in this:

>
> > const Vector<TYPE> operator()(const int& i,const int& j) <---- (a)
> > {...}
> > Vector<TYPE>& operator+=(const Vector<TYPE>& rhs){...}

>
> > Should you use:

>
> > const Vector<TYPE>& operator()(const int& i,const int& j) <---- (a)
> > {...}

>
> > instead

>
> > const Vector<TYPE> operator()(const int& i,const int& j) <---- (a)
> > {...}

>
> No. If he changed operator() to return a reference, it would return a
> reference to the temporary which would be out of scope once operator()
> has completed. The reference would be referring to a dead object.




 
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
Member operators operator>>() and operator<<() Alex Vinokur C++ 3 03-20-2005 03:11 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous: Alex Vinokur C++ 4 11-26-2004 11:46 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57