473,322 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Problem about overload operator ++

This is a program just for testing operator overloading. But I found
the operator ++ doesn't act like on built-in types. For detail:
When
int array[10];
Ptr_to_T<intsmart_ptr(&array[0], array, 10);
*smart_ptr++ = 10; // I want to modify array[0],but this sentence
modifies array[1]

Do I make myself clear?
Could some body tell me how to fix it ?

#include <iostream>
using namespace std;
template<typename T>
class Ptr_to_T
{
public:
Ptr_to_T(T* p, T* array, int size):_p(p),_array(array),_size(size)
{}
Ptr_to_T(T* p):_p(p){}
Ptr_to_T& operator++(){//prefix
_p += 1;
return *this;
}
Ptr_to_T& operator++(int){//postfix
cout << "Entering Operator++" << endl;
T* temp = _p;
_p += 1;
return *this;
}
T& operator*(){
cout << "Entering Operator*" << endl;
return *_p;
}
private:
T* _p;
T* _array;
int _size;
};
int main()
{
int array[10];
Ptr_to_T<intsmart_ptr(&array[0], array, 10);
*smart_ptr++ = 10;
cout << array[0] << endl;
cout << array[1] << endl;
}
Result:

Entering Operator++
Entering Operator*
4246640
10

Oct 5 '08 #1
5 1617
Hill wrote:
Ptr_to_T& operator++(int){//postfix
cout << "Entering Operator++" << endl;
T* temp = _p;
_p += 1;
return *this;
}
Here you want to return a copy of the object before the increment, but
you are returning the object after the increment. temp does not serve
any purpose.

--
Ian Collins.
Oct 5 '08 #2
Hill wrote:
This is a program just for testing operator overloading. But I found
the operator ++ doesn't act like on built-in types. For detail:
When
int array[10];
Ptr_to_T<intsmart_ptr(&array[0], array, 10);
*smart_ptr++ = 10; // I want to modify array[0],but this sentence
modifies array[1]

Do I make myself clear?
Could some body tell me how to fix it ?

#include <iostream>
using namespace std;
template<typename T>
class Ptr_to_T
{
public:
Ptr_to_T(T* p, T* array, int size):_p(p),_array(array),_size(size)
{}
Ptr_to_T(T* p):_p(p){}
Ptr_to_T& operator++(){//prefix
_p += 1;
return *this;
}
Ptr_to_T& operator++(int){//postfix
cout << "Entering Operator++" << endl;
T* temp = _p;
_p += 1;
return *this;
Did you mean:

Ptr_to_T temp ( _p, _array, _size );
_p += 1;
return temp;

}
T& operator*(){
cout << "Entering Operator*" << endl;
return *_p;
}
private:
T* _p;
T* _array;
int _size;
};
int main()
{
int array[10];
Ptr_to_T<intsmart_ptr(&array[0], array, 10);
*smart_ptr++ = 10;
cout << array[0] << endl;
cout << array[1] << endl;
}
Result:

Entering Operator++
Entering Operator*
4246640
10
Oct 5 '08 #3
On Sat, 04 Oct 2008 18:29:54 -0700, Hill wrote:
This is a program just for testing operator overloading. But I found the
operator ++ doesn't act like on built-in types. For detail: When
int array[10];
Ptr_to_T<intsmart_ptr(&array[0], array, 10); *smart_ptr++ = 10; //
I want to modify array[0],but this sentence
modifies array[1]

Do I make myself clear?
Could some body tell me how to fix it ?
Did you even read the replies you got the last time?

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_in_English
Oct 5 '08 #4
On 10ÔÂ5ÈÕ, ÏÂÎç3ʱ37·Ö, Obnoxious User <O...@127.0..0.1wrote:
On Sat, 04 Oct 2008 18:29:54 -0700, Hill wrote:
This is a program just for testing operator overloading. But I found the
operator ++ doesn't act like on built-in types. For detail: When
int array[10];
Ptr_to_T<intsmart_ptr(&array[0], array, 10); *smart_ptr++ = 10;//
I want to modify array[0],but this sentence
modifies array[1]
Do I make myself clear?
Could some body tell me how to fix it ?

Did you even read the replies you got the last time?

--
OU
Remember 18th of June 2008, Democracy died that afternoon.http://frapedia..se/wiki/Information_in_English
Thanks all!
template<typename T>
class Ptr_to_T
{
public:
class Range{};

Ptr_to_T(T* p, T* array, int size):_p(p),_array(array),_size(size)
{
}
Ptr_to_T(T* p):_p(p){}
Ptr_to_T& operator++(){//prefix
_p += 1;
return *this;
}
const Ptr_to_T operator++(int){//postfix
T* temp = _p;
_p += 1;
return Ptr_to_T(temp, temp, _array + _size - temp);
}
Ptr_to_T& operator--(){//prefix
_p--;
return *this;
}
const Ptr_to_T operator--(int){//postfix
T* temp = _p;
_p -= 1;
return Ptr_to_T(temp, temp, _array + _size -temp);
}
T& operator*(){
check();
return *_p;
}
private:
void check(){
if( _p - _array >= _size || _p < _array){
cout << _p - _array << endl;
throw Range();
}
}

T* _p;
T* _array;
int _size;
};
Oct 6 '08 #5
ba*********@gmail.com wrote:
On 10?5?, ??3?37?, Obnoxious User <O...@127.0.0.1wrote:
>On Sat, 04 Oct 2008 18:29:54 -0700, Hill wrote:
This is a program just for testing operator overloading. But I found
the
operator ++ doesn't act like on built-in types. For detail: When
int array[10];
Ptr_to_T<intsmart_ptr(&array[0], array, 10); *smart_ptr++ = 10;
// I want to modify array[0],but this sentence
modifies array[1]
Do I make myself clear?
Could some body tell me how to fix it ?

Did you even read the replies you got the last time?

--
OU
Remember 18th of June 2008, Democracy died that
afternoon.http://frapedia.se/wiki/Information_in_English

Thanks all!
template<typename T>
class Ptr_to_T
{
public:
class Range{};

Ptr_to_T(T* p, T* array, int size):_p(p),_array(array),_size(size)
{
}
Ptr_to_T(T* p):_p(p){}
Ptr_to_T& operator++(){//prefix
_p += 1;
return *this;
}
const Ptr_to_T operator++(int){//postfix
T* temp = _p;
_p += 1;
return Ptr_to_T(temp, temp, _array + _size - temp);
Huh?

Why is the returned value different from the old value? I would have
expected

return Ptr_to_T( temp, _array, _size );

}
Ptr_to_T& operator--(){//prefix
_p--;
return *this;
}
const Ptr_to_T operator--(int){//postfix
T* temp = _p;
_p -= 1;
return Ptr_to_T(temp, temp, _array + _size -temp);
Same here.

}
T& operator*(){
check();
return *_p;
}
private:
void check(){
if( _p - _array >= _size || _p < _array){
cout << _p - _array << endl;
throw Range();
}
}

T* _p;
T* _array;
int _size;
};

Best

Kai-Uwe Bux
Oct 6 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Chiller | last post by:
Ok, this is a continuation of a problem I posted on an earlier thread. I've started another thread because my problem has progressed from the initial constructor problem into a general method...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
6
by: Chiller | last post by:
I'm in the process of writing a class that performs functions on a Distance object. The object is created by entering details as "Distance a (5, km)" or "Distance b (3, cm)" etc. I wish to write...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
6
by: junw2000 | last post by:
Hi, I wrote a simple code about operator overloading. But it can not compile. Below is the code: #include <iostream> using namespace std;
2
by: Simon | last post by:
platform: Borland C++ 5.5 free Winxp C++ source from: C++ primer Plus 5 Edition. Complie Error: "operator >>" not implemented in type 'istream' for arguments of type "STRING" in function...
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
2
by: Markjan | last post by:
I have a problem with classes and structures in classes (C++) I have to overload operator . class Data { public: class Proxy { //for overload operator Data& _a; int...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.