-
March 26th, 2009, 10:40 AM
#1
operator++ with templates
Code:
template <class T>
T operator++(T num)
{
cout<<"In overload function"<<endl;
return T result = num++;
}
int main()
{
int num=5;
int result = operator++(num); //whats the problem with this???
}
Why does this fail to compile?
-
March 26th, 2009, 11:20 AM
#2
Re: operator++ with templates
Because you're trying to declare a variable inside a return statement.
And by the way... I'd think for a minute before committing/submitting this... whatever it does.
-
March 26th, 2009, 11:24 AM
#3
Re: operator++ with templates
The return statement is not the problem here.
I am visual studio 2008 if thats any help.
-
March 26th, 2009, 11:43 AM
#4
Re: operator++ with templates
Oh, really?! Then you tell me why this fails to compile:
Code:
int main()
{
return int i = 0;
}
-
March 26th, 2009, 12:08 PM
#5
Re: operator++ with templates
No need to be smart, what i was saying was even when i change the return part around it still fails. The error i get is before it reaches the return part.
To make it clearer for you
Code:
template <class T>
T operator++(T num)
{
cout<<"In overload function"<<endl;
T result = num++;
return result
}
int main()
{
int num=5;
int result = operator++(num); //whats the problem with this???
}
This still fails.
-
March 26th, 2009, 12:25 PM
#6
Re: operator++ with templates
Originally Posted by newbie30
whats the problem with this?
For one thing, you cannot overload operator++ for int.
Basically, it looks like you are trying to implement postfix operator++ in terms of prefix operator++, which is weird: the opposite is more normal. Nonetheless, if you want an example:
Code:
#include <iostream>
class X
{
public:
X(int num) : num(num) {}
int get() const
{
return num;
}
X operator++(int)
{
X temp(*this);
++num;
return temp;
}
private:
int num;
};
template <class T>
T& operator++(T& x)
{
x++;
return x;
}
int main()
{
X x(5);
X result = operator++(x);
std::cout << x.get() << ' ' << result.get() << std::endl;
}
Personally, I feel that a more useful thing would be to define postfix operator++ in terms of prefix operator++, e.g.,
Code:
template<class T>
T operator++(T& x, int)
{
T temp(x);
++x;
return temp;
}
-
March 26th, 2009, 12:34 PM
#7
Re: operator++ with templates
Originally Posted by newbie30
No need to be smart, what i was saying was even when i change the return part around it still fails. The error i get is before it reaches the return part.
To make it clearer for you
Code:
template <class T>
T operator++(T num)
{
cout<<"In overload function"<<endl;
T result = num++;
return result
}
This still fails.
You are running into a circular definition problem. You have num defined as type T and operator++ for type T is using operator++ for type T inside itself.
You also missed the semi-colon on your return line.
-
March 26th, 2009, 12:41 PM
#8
Re: operator++ with templates
Originally Posted by jefranki
You are running into a circular definition problem. You have num defined as type T and operator++ for type T is using operator++ for type T inside itself.
No, notice that postfix operator++ is used in the implementation of the function template prefix operator++. Incidentally, note that my example also corrects the function signature and return type.
Last edited by laserlight; March 26th, 2009 at 12:43 PM.
-
March 26th, 2009, 12:59 PM
#9
Re: operator++ with templates
Originally Posted by laserlight
No, notice that postfix operator++ is used in the implementation of the function template prefix operator++. Incidentally, note that my example also corrects the function signature and return type.
I opened the editor before your post came up. At any rate, I missed the prefix vs. postfix element.
-
March 27th, 2009, 07:26 AM
#10
Re: operator++ with templates
Originally Posted by newbie30
Why does this fail to compile?
What does the compiler say?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|