Overloading operator method names.

I am writing a custom iterator template and template container class.

Below is an example set of methods I am using. In my main method, however, when I want to invoke these methods. I am using the full name rather than just the operator, take _RBIterator& operator++() for example.

1
2
3
4
5
6
7
8
9

auto s = rb.begin();
s.operator++();

Rather than :

auto s = rb.begin();
++s;


Second example using ==

1
2
3
4
5
6
7
8
9
10
11
12

 auto one = rb.begin();
auto two = rb.begin();

std::cout << " Equal? : " << one.operator==(two);

Rather than:

 auto one = rb.begin();
auto two = rb.begin();

std::cout << " Equal? : " << one == two ;




Is my syntax correct? I have to call the method full name as shown in the first example rather than the second. Looking at examples online others have overloaded there operators in this way ( writing operator in front over the actual operator symbols ) see :

https://stackoverflow.com/questions/3582608/how-to-correctly-implement-custom-iterators-and-const-iterators


1
2
3
4
5
6
7
8
9
10
11
12
13
_RBIterator& operator=(const _RBIterator<T, P, R>& pattern)	
Reference operator*() const
Pointer operator->() const
bool operator==(const _RBIterator& rhs) const
bool operator!=(const _RBIterator& rhs) const
_RBIterator& operator++()
_RBIterator operator++(int)
_RBIterator& operator--()
_RBIterator operator--(int)
_RBIterator& operator+=(difference_type n)
_RBIterator operator+(difference_type n)
difference_type operator-(const _RBIterator& rhs) const
Reference operator[](difference_type n)
Last edited on
I don't see any examples of writing out the "full name" (as you call it) in your stackoverflow link. It is pointless to do so, however. Just write it normally.

And your first example isn't even correct since s.operator++() is pre-increment whereas your "rather than" example shows a post-increment.
Yes sorry, my mistake re the post and pre-increment and The StackOverflow is probably not a good example as they are not showing the main method where they are implementing their template.

It is pointless to do so, however. Just write it normally.

This is my point, I am unable to write it "normally".

The main class will only compile if I write out

.operator++()

rather than

++rb;

and the same goes with the rest of the methods
Last edited on
If something isn't compiling, try to produce a minimal example that reproduces the compile errors.

Edit: For example,
1
2
3
4
5
6
7
8
9
10
11
12
// Example program
#include <iostream>
#include <string>

int main()
{
    std::string name = "Billy";
    auto one = name.begin();
    auto two = name.begin();

    std::cout << one == two;
}

I'll admit the error it produces is convoluted, but the issue is operator precedence.
This is an unfortunate consequence of Stroustrup overloading the bit shift operators for stream operators.

Change it to:
1
2
3
4
5
6
7
8
9
10
11
12
// Example program
#include <iostream>
#include <string>

int main()
{
    std::string name = "Billy";
    auto one = name.begin();
    auto two = name.begin();

    std::cout << (one == two);
}


And be aware of operator precedence rules:
https://en.cppreference.com/w/cpp/language/operator_precedence
<< has higher precedence than ==
Last edited on
> This is my point, I am unable to write it "normally".
> The main class will only compile if I write out
> .operator++()
> rather than
> ++rb;
that should have been your first post
and if you get an error message, then post it (with templates the message may be quite long, grep the line that says error)
Topic archived. No new replies allowed.