c++ scoping weirdness - Why do inner-type parameters of member functions do not need scoping?

StackOverflow https://stackoverflow.com/questions/4071579

  •  28-09-2019
  •  | 
  •  

Question

Let's say we're given this class with an inner struct.

class Some {
   public:
    struct Crap{};
    Crap Process(Crap& c);
}


Some::Crap Some::Process(Crap& crap) {   Crap moreCrap = ..  }

It makes sense to me that the return type (Some::Crap) in "Process" is scoped.

It makes sense to me that no scoping is needed inside the body of "Process".

I do not understand why the inner struct parameter of Process (Crap& crap) does not require scoping.

Does anyone have any insight on this? Thanks

Was it helpful?

Solution

In fact, the parameter type does need to be qualified (unless your example is incorrect and you intend for Process to be a member function of Some).

OTHER TIPS

Assuming the following snippet:

class Some
{
public:
    struct Crap{};
    Crap Process(Crap&);
};

Some::Crap Some::Process(Crap& crap) { ... }

Basically, Crap does not require scoping inside the function parameter list and body for the same reason you don't need this-> to access data members and member functions: it is implicit because it is at the same scope.

The reason return type is needed is simply because the parser encounters the return type before the method name and cannot (is not required to?) deduce scope at that moment.

The question title says 'member function', but your example contains no member function. Assuming you meant Some::Process rather than just Process:

The class scope for the member function definition begins at the (, not at the {. I assume the reasoning is exactly so that things like this can be typed shorter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top