Topic: Class name as scope qualifier inside class declaration


Author: cliffg@my-deja.com
Date: 2000/02/10
Raw View
Please forgive the waste of bandwidth (human and machine) if this is a
commonly known problem (my search at Deja didn't show anything
obviously related).

The following compiles on VC++ 6.0, but is flagged as a syntax error on
Metrowerks CW Pro 5:

class Test {
public:
  Test () : x(0) { }
  int Test::getX() const; // illegal on CW 5, not on VC++
private:
  int x;
};

int Test::getX() const {
  return x;
}

The syntax error from MW CW 5 is:

Error   : illegal access/using declaration
test.cpp line 4     int Test::getX() const;

I suspect it is illegal C++, but it's not obvious to me from my reading
of the C++ standard.

I always have a few students that insist on submitting assignments with
the class name as a scope qualifier in the class declaration (I assume
to closer match the syntax of the member function definitions in the
implementation file). As a style guideline, I highly recommend against
it, but I'd like to add this to my list of "illegal C++ usages allowed
by VC++" and disallow it in my students assignments.

Cliff


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Darin Adler <darin@bentspoon.com>
Date: 2000/02/10
Raw View
In article <87qg0v$f7p$1@nnrp1.deja.com>, cliffg@my-deja.com wrote:

> The following compiles on VC++ 6.0, but is flagged as a syntax error on
> Metrowerks CW Pro 5:
>
> class Test {
> public:
>   Test () : x(0) { }
>   int Test::getX() const; // illegal on CW 5, not on VC++
> private:
>   int x;
> };
>
> int Test::getX() const {
>   return x;
> }
>
> The syntax error from MW CW 5 is:
>
> Error   : illegal access/using declaration
> test.cpp line 4     int Test::getX() const;
>
> I suspect it is illegal C++, but it's not obvious to me from my reading
> of the C++ standard.

The rule is in 8.3/1: "A declarator-id shall not be qualified
except for the definition of a member function (9.3) or static data
member (9.4) or nested class (9.7) outside of its class, the definition
or explicit instantiation of a function, variable or class member of a
namespace outside of its namespace, or the definition of a previously
declared explicit specialization outside of its namespace, or the
declaration of a friend function that is a member of another class or
namespace (11.4)."

    -- Darin

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]