5

I saw this code but I couldn't understand what it does:

inline S* O::operator->() const
{
    return ses; //ses is a private member of Type S*
}

so what happens now if I used ->?

1
  • BTW, this is C++ only as the C language does not allow function nor operator overloading. This is another issue that differentiates the two languages. Commented May 25, 2010 at 17:16

5 Answers 5

11

Now if you have

O object;
object->whatever()

first the overloaded operator-> will be called, which will return ses stored inside the object, then operator-> (built-in in case of S*) will be called again for the returned pointer.

So

object->whatever();

is equivalent to pseudocode:

object.ses->whatever();

the latter would be of course impossible since O::ses is private - that's why I call it pseudocode.

With such overload you can create a wrapper around a pointer - such wrapper is typically called smart pointer.

Sign up to request clarification or add additional context in comments.

2 Comments

"With suce overload" => Did you meant "such" or is "suce" the name of the -> operator ? (non native-english speaker asking)
@ereOn: Fixed, that were typos.
2

Is you have an instance of class O and you do

obj->func()

then the operator-> returns ses and then it uses the returned pointer to call func().

Full example:

struct S
{
    void func() {}
};

class O
{
public:
    inline S* operator->() const;
private:
    S* ses;
};

inline S* O::operator->() const
{
    return ses;
}

int main()
{
    O object;
    object->func();
    return 0;
}

6 Comments

what happens if the func() of S was declared as such: virtual void func() = 0;
virtual functions should at least be protected, but preferably private. It is the responsibility of class S to make sure ses is pointing to a valid object.
virtual private functions? Unless you're full of friend class declarations, I think private and virtual are mostly mutually exclusive.
@luiscubal: Actually having private virtual is a good technique (used extensively in the streaming libraries). But it is not always appropriate If you just want a class to override a pure virtual method that there is no need to make it private, but if you want the base class to do some work before the virtual call then it is usefull. Saying it should be protected or private is going to far that is just a technique that can be used.
@Martin York: I see there are already questions about this topic(virtual private) on stack overflow. I'll have to investigate this weird topic.
|
0

It's an overloaded operator that would return a pointer to some member of type S.

Like, if you write

O object;
(object->)...

the part (object->) would become your pointer.

Comments

0

Anytime an object of type O uses the -> operator a pointer to ses will be returned.

Comments

-1

It overloads the operator -> of the class O, that returns now an S* instead of an O*

1 Comment

By default, class O does not have a -> operator that returns an O*.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.