Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7900115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:53:13+00:00 2026-06-03T08:53:13+00:00

It is said that the arrow operator is applied recursively. But when I try

  • 0

It is said that the arrow operator is applied recursively. But when I try to execute the following code, it prints gibberish when it is supposed to print 4.

class dummy
{
public:
    int *p;

    int operator->()
    {
        return 4;
    }
};

class screen 
{
public:
    dummy *p;

    screen(dummy *pp): p(pp){}
    dummy* operator->()
    {
        return p;
    }
};

int main()
{
    dummy *d = new dummy;
    screen s(d);
    cout<<s->p;
    delete d;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:53:14+00:00Added an answer on June 3, 2026 at 8:53 am

    What Stanley meant by “recursive” is just that the operator is applied to every returned object until the returned type is a pointer.

    Which happens here on the first try: screen::operator -> returns a pointer. Thus this is the last call to an operator -> that the compiler attempts. It then resolves the right-hand sice of the operator (p) by looking up a member in the returned pointee type (dummy) with that name.

    Essentially, whenever the compiler finds the syntax aᵢ->b in code, it essentially applies the following algorithm:

    1. Is aᵢ of pointer type? If so, resolve member b of *aᵢ and call (*aᵢ).b.
    2. Else, try to resolve aᵢ::operator ->
      1. On success, set aᵢ₊₁ = aᵢ::operator ->(). Goto 1.
      2. On failure, emit a compile error.

    I’m hard-pressed to come up with a short, meaningful example where a chain of operator -> invocations even makes sense. Probably the only real use is when you write a smart pointer class.

    However, the following toy example at least compiles and yields a number. But I wouldn’t advise actually writing such code. It breaks encapsulation and makes kittens cry.

    #include <iostream>
    
    struct size {
        int width;
        int height;
        size() : width(640), height(480) { }
    };
    
    struct metrics {
        size s;
        size const* operator ->() const {
            return &s;
        }
    };
    
    struct screen {
        metrics m;
        metrics operator ->() const {
            return m;
        }
    };
    
    int main() {
        screen s;
        std::cout << s->width << "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Somebody said that when your PHP code and application use global variables then it
It is said that when a UIView object is created with programmer's code, then
Its said that property should not be Set only (Code analysis rule CA1044 )and
I am following this example: http://mkblog.exadel.com/2008/11/richfaces-built-in-sorting/ it its said that arrows should appear beside
It's said that you should not call GUI functions from a thread, but I'm
If one were to compile and run the following code, one would find that
Someone said that I might not be getting how to proper code in Haskell.
Its always said that the requirements should be traceable but when we talk about
I've heard it said that it is difficult to design for inheritance, but I've
I've heard it said that the Entity Framework is overkill or that it's difficult

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.