C++ Language Lawyer Question

Joel Sherrill joel.sherrill@OARcorp.com
Tue Feb 10 20:39:00 GMT 2004


Gabriel Dos Reis wrote:

> Joel Sherrill <joel.sherrill@OARcorp.com> writes:
> 
> | Hi,
> | 
> | I was surprised that a particular piece of code compiled
> | (with gcc 2.96 and 3.2.3 on RedHat 7.3) and wanted a
> | language lawyer to tell me if this code should have.
> | The issue is that I accidentally made a type definition
> | private when it was used as an argument to a public method.
> | 
> | namespace N {
> |    class C {
> |      private:
> |        typedef int T1_t;
> |      protected:
> |        typedef int T2_t;
> |      public:
> |        C();
> |        ~C();
> |        int f1(T1_t);  // private type used for public method
> |        int f2(T2_t);  // protected type used for public method
> |    };
> | }
> | 
> | N::C::C() { ; }
> | N::C::~C() { ; }
> | int N::C::f1(T1_t x) { return x+1; }
> | int N::C::f2(T2_t x) { return x+2; }
> | 
> | Should this work?
> 
> Yes, the program is well-formed.  Access control is applied to names,
> and the check is done in their contexts of use.  You may even be able
> to call N::C::f1() with an argument of type int with no error.
> 
> | If so, what is the rationale for allowing it.
> 
> I'm afraid I don't understand what you mean here.
> The general philosophy is that you can use a name everywhere it is
> visible and accessible.  N::C::T1_t is usable and accessible in the
> scope of  N::C::f.

I think you guys have definitely give me reason to believe that there
are good reasons for it being legal even if it is a bit non-intuitive
at first glance.  Using the output of a function returning a private
type like T1_t and using that as input makes sense.

Thanks.

--joel

> -- Gaby




More information about the Gcc mailing list