inaccessible constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cpp_novice@yahoo.com

    #1

    inaccessible constructor

    I have the following C++ declaration:

    struct S {

    private:

    S();

    };

    struct T {

    S s;

    };

    Why isnt it illegal C++?
    It gives the false impression that a member function of S can
    instantiate
    an object of the type T.

    If an object of type T cannot be constructed under any
    circumstance, then
    why allow the type declaration to pass in the first place?

  • Mehturt@gmail.com

    #2
    Re: inaccessible constructor


    cpp_novice@yaho o.com napĂ­sal(a):
    I have the following C++ declaration:
    >
    struct S {
    >
    private:
    >
    S();
    >
    };
    >
    struct T {
    >
    S s;
    >
    };
    >
    Why isnt it illegal C++?
    It is illegal, i.e. the gcc output:

    x.cc: In constructor 'T::T()':
    x.cc:8: error: 'S::S()' is private
    x.cc:12: error: within this context
    It gives the false impression that a member function of S can
    instantiate
    an object of the type T.
    >
    If an object of type T cannot be constructed under any
    circumstance, then
    why allow the type declaration to pass in the first place?

    Comment

    • sun1991@gmail.com

      #3
      Re: inaccessible constructor

      Because the "s" in struct T hasn't been initial. When compile meet
      something like: "T t", then it look for the comstructor for S, and
      report an error.


      cpp_novice@yaho o.com wrote:
      I have the following C++ declaration:
      >
      struct S {
      >
      private:
      >
      S();
      >
      };
      >
      struct T {
      >
      S s;
      >
      };
      >
      Why isnt it illegal C++?
      It gives the false impression that a member function of S can
      instantiate
      an object of the type T.
      >
      If an object of type T cannot be constructed under any
      circumstance, then
      why allow the type declaration to pass in the first place?

      Comment

      • Thomas Tutone

        #4
        Re: inaccessible constructor

        cpp_novice@yaho o.com wrote:
        I have the following C++ declaration:
        >
        struct S {
        private:
        S();
        };
        >
        struct T {
        S s;
        };
        >
        Why isnt it illegal C++?
        You're mistaken. It will cause a compile error if you try to
        instantiate T.
        It gives the false impression that a member function of S can
        instantiate
        an object of the type T.
        No it doesn't. If you try to instantiate T, you'll get a compile
        error.
        If an object of type T cannot be constructed under any
        circumstance, then
        why allow the type declaration to pass in the first place?
        You're mistaken.

        Try compiling the following, then please report back on what your
        compiler says:

        struct S {
        private:
        S();
        };

        struct T {
        S s;
        };

        int main()
        {
        T t;
        }


        Here's what Comeau online says:

        "ComeauTest .c", line 7: error: "S::S()" is inaccessible
        S s;
        ^
        detected during implicit generation of "T::T()" at line 12

        1 error detected in the compilation of "ComeauTest .c".

        Hope that helps.

        Best regards,

        Tom

        Comment

        • cpp_novice@yahoo.com

          #5
          Re: inaccessible constructor

          All that is done mate. Please explain why only the following should
          compile?

          struct S {
          private:
          S();
          };

          struct T {
          S s;
          };

          If it compiles, then obviously the language designers have thought that
          T is useful in some future context.

          Care to explain what that might be apart from
          creating pointers, assigning null to them and
          comparing them (which could be accomplished
          just as well with void pointers)?

          Thomas Tutone wrote:
          cpp_novice@yaho o.com wrote:
          >
          I have the following C++ declaration:

          struct S {
          private:
          S();
          };

          struct T {
          S s;
          };

          Why isnt it illegal C++?
          >
          You're mistaken. It will cause a compile error if you try to
          instantiate T.
          >
          It gives the false impression that a member function of S can
          instantiate
          an object of the type T.
          >
          No it doesn't. If you try to instantiate T, you'll get a compile
          error.
          >
          If an object of type T cannot be constructed under any
          circumstance, then
          why allow the type declaration to pass in the first place?
          >
          You're mistaken.
          >
          Try compiling the following, then please report back on what your
          compiler says:
          >
          struct S {
          private:
          S();
          };
          >
          struct T {
          S s;
          };
          >
          int main()
          {
          T t;
          }
          >
          >
          Here's what Comeau online says:
          >
          "ComeauTest .c", line 7: error: "S::S()" is inaccessible
          S s;
          ^
          detected during implicit generation of "T::T()" at line 12
          >
          1 error detected in the compilation of "ComeauTest .c".
          >
          Hope that helps.
          >
          Best regards,
          >
          Tom

          Comment

          • Thomas Tutone

            #6
            Re: inaccessible constructor

            cpp_nov...@yaho o.com wrote:
            All that is done mate.
            OK. Then didn't you post the results like I asked? I'm taking the
            trouble to answer your question. Won't you do me the courtesy of
            responding to my reasonable request?
            Please explain why only the following should
            compile?
            >
            struct S {
            private:
            S();
            };
            >
            struct T {
            S s;
            };
            Because you didn't instantiate T. Until you do so, your compiler
            doesn't see the error. Other compilers may issue a diagnostic there.
            Yours doesn't. Either live with it or switch compilers.
            If it compiles, then obviously the language designers have thought that
            T is useful in some future context.
            Are you ABSOLUTELY sure? Is it POSSIBLE that the compiler writer for
            the compiler you were using decided it was easier not to check such
            things until you tried to instantiate the class in question, since it
            would only be a problem if you instantiated the class?
            Care to explain what that might be apart from
            creating pointers, assigning null to them and
            comparing them (which could be accomplished
            just as well with void pointers)?
            Different compilers exhibit different behaviors. If you want a
            compiler that gives you a diagnostic for the code you posted without
            instantiating T, then switch to a compiler that does so, or ask the
            writer of your compiler to make that change. In fact, I'm sure that if
            you offer them enough money for their services, the compiler writers
            will be happy to make that change for you. But that seems like a poor
            use of your money, if you ask me. If you try to instantiate T, you get
            a compile error, and if you don't try to instantiate T, then your
            program never tries to access the inaccessible constructor.

            Best regards,

            Tom

            Comment

            • Default User

              #7
              Re: inaccessible constructor

              sun1991@gmail.c om wrote:
              Because the "s" in struct T hasn't

              See below.



              Brian

              --
              Please don't top-post. Your replies belong following or interspersed
              with properly trimmed quotes. See the majority of other posts in the
              newsgroup, or the group FAQ list:
              <http://www.parashift.c om/c++-faq-lite/how-to-post.htm>

              Comment

              • Default User

                #8
                Re: inaccessible constructor

                cpp_novice@yaho o.com wrote:
                All that is done mate.
                See below.



                Brian

                --
                Please don't top-post. Your replies belong following or interspersed
                with properly trimmed quotes. See the majority of other posts in the
                newsgroup, or the group FAQ list:
                <http://www.parashift.c om/c++-faq-lite/how-to-post.htm>

                Comment

                Working...