class vs namespace?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dean

    class vs namespace?

    Hi all,

    We have a few existing classes that we want to put into either another
    class or a namespace, so that we can avoid naming conflicts. As far as
    I can see the syntax is identical from a usage perspective.

    Does anyone have an opinion on this either way?

    Thanks

    Dean

  • Jakob Bieling

    #2
    Re: class vs namespace?

    dean <deanbrown3d@ya hoo.comwrote:
    Hi all,
    >
    We have a few existing classes that we want to put into either another
    class or a namespace, so that we can avoid naming conflicts. As far as
    I can see the syntax is identical from a usage perspective.
    >
    Does anyone have an opinion on this either way?
    For your purpose, namespaces are the way to go. You can reopen a
    namespace and add stuff across translation units. You cannot do this
    with classes. In addition, using a class implies that you can create an
    instance of that class, which, as I understood it, is not the case.

    hth
    --
    jb

    (reply address in rot13, unscramble first)


    Comment

    • Rolf Magnus

      #3
      Re: class vs namespace?

      Jakob Bieling wrote:
      dean <deanbrown3d@ya hoo.comwrote:
      >Hi all,
      >>
      >We have a few existing classes that we want to put into either another
      >class or a namespace, so that we can avoid naming conflicts. As far as
      >I can see the syntax is identical from a usage perspective.
      >>
      >Does anyone have an opinion on this either way?
      >
      For your purpose, namespaces are the way to go. You can reopen a
      namespace and add stuff across translation units. You cannot do this
      with classes. In addition, using a class implies that you can create an
      instance of that class, which, as I understood it, is not the case.
      Also, you can use using-declarations with namespaces, and AFAIK that's not
      possible with classes, at least not if you don't derive from them.

      Comment

      • dean

        #4
        Re: class vs namespace?

        So what to do about member variables of the class, if we use a
        namespace instead? I mean, how can one hide a private variable and just
        allow accessor (get and set) functions? Put the member variable outside
        of the namespace but in the same unit as the namespace?


        Rolf Magnus wrote:
        Jakob Bieling wrote:
        >
        dean <deanbrown3d@ya hoo.comwrote:
        Hi all,
        >
        We have a few existing classes that we want to put into either another
        class or a namespace, so that we can avoid naming conflicts. As far as
        I can see the syntax is identical from a usage perspective.
        >
        Does anyone have an opinion on this either way?
        For your purpose, namespaces are the way to go. You can reopen a
        namespace and add stuff across translation units. You cannot do this
        with classes. In addition, using a class implies that you can create an
        instance of that class, which, as I understood it, is not the case.
        >
        Also, you can use using-declarations with namespaces, and AFAIK that's not
        possible with classes, at least not if you don't derive from them.

        Comment

        • Fred Zwarts

          #5
          Re: class vs namespace?

          "dean" <deanbrown3d@ya hoo.comwrote in message news:1158332731 .153079.82250@b 28g2000cwb.goog legroups.com...
          So what to do about member variables of the class, if we use a
          namespace instead? I mean, how can one hide a private variable and just
          allow accessor (get and set) functions? Put the member variable outside
          of the namespace but in the same unit as the namespace?
          I would think the other way around.
          Put the private variable in the same namespace but in another unit
          (the unit in which you write the body of the get and set functions).
          The advantage above a class implementation is that the private variables
          are not even declared in the header file.
          The header file discloses nothing about the implementation,
          it has only the interface

          Fred.Zwarts.
          Rolf Magnus wrote:
          >Jakob Bieling wrote:
          >>
          dean <deanbrown3d@ya hoo.comwrote:
          >Hi all,
          >>
          >We have a few existing classes that we want to put into either another
          >class or a namespace, so that we can avoid naming conflicts. As far as
          >I can see the syntax is identical from a usage perspective.
          >>
          >Does anyone have an opinion on this either way?
          >
          For your purpose, namespaces are the way to go. You can reopen a
          namespace and add stuff across translation units. You cannot do this
          with classes. In addition, using a class implies that you can create an
          instance of that class, which, as I understood it, is not the case.
          >>
          >Also, you can use using-declarations with namespaces, and AFAIK that's not
          >possible with classes, at least not if you don't derive from them.
          >

          Comment

          • Jakob Bieling

            #6
            Re: class vs namespace?

            dean <deanbrown3d@ya hoo.comwrote:
            Rolf Magnus wrote:
            >Jakob Bieling wrote:
            >>dean <deanbrown3d@ya hoo.comwrote:
            >>>We have a few existing classes that we want to put into either
            >>>another class or a namespace, so that we can avoid naming
            >>>conflicts. As far as I can see the syntax is identical from a
            >>>usage perspective.
            >>>>
            >>>Does anyone have an opinion on this either way?
            >> For your purpose, namespaces are the way to go. You can reopen a
            >>namespace and add stuff across translation units. You cannot do this
            >>with classes. In addition, using a class implies that you can
            >>create an instance of that class, which, as I understood it, is not
            >>the case.
            >Also, you can use using-declarations with namespaces, and AFAIK
            >that's not possible with classes, at least not if you don't derive
            >from them.
            So what to do about member variables of the class, if we use a
            namespace instead? I mean, how can one hide a private variable and
            just allow accessor (get and set) functions? Put the member variable
            outside of the namespace but in the same unit as the namespace?
            I think I am not following. You said you have several classes and
            want to put them into a namespace (or class, but let's just stick to the
            namespace now) to avoid naming conflicts:

            class A { int a; };
            class B { int b; };

            becomes

            namespace dean
            {
            class A { int a; };
            class B { int b; };
            };

            As you can see, your classes do not change. Their members stay right
            where they are. You only introduce a new namespace and put the stuff in
            there.

            Apologies, if I am misunderstandin g you.

            hth
            --
            jb

            (reply address in rot13, unscramble first)


            Comment

            • Default User

              #7
              Re: class vs namespace?

              dean wrote:
              So what to do about member variables of the class


              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.html>




              Brian

              Comment

              • dean

                #8
                Re: class vs namespace?

                I have no idea what to top-post means. And anyway, what are you adding
                to this discussion?


                Default User wrote:
                dean wrote:
                >
                So what to do about member variables of the class
                >
                >
                >
                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.html>
                >
                >
                >
                >
                Brian

                Comment

                • Noah Roberts

                  #9
                  Re: class vs namespace?


                  dean wrote:
                  I have no idea what to top-post means. And anyway, what are you adding
                  to this discussion?
                  You need to read the webpage to which you where directed.

                  Comment

                  • dean

                    #10
                    Re: class vs namespace?

                    Rolf - for better or worse, the company agreed not to use "using
                    namespace", but rather add the scope prefix onto each member, e.g.
                    CMyClass::MyFun c or CMyNamespace::M yFunc.

                    Dean

                    Rolf Magnus wrote:
                    Jakob Bieling wrote:
                    >
                    dean <deanbrown3d@ya hoo.comwrote:
                    Hi all,
                    >
                    We have a few existing classes that we want to put into either another
                    class or a namespace, so that we can avoid naming conflicts. As far as
                    I can see the syntax is identical from a usage perspective.
                    >
                    Does anyone have an opinion on this either way?
                    For your purpose, namespaces are the way to go. You can reopen a
                    namespace and add stuff across translation units. You cannot do this
                    with classes. In addition, using a class implies that you can create an
                    instance of that class, which, as I understood it, is not the case.
                    >
                    Also, you can use using-declarations with namespaces, and AFAIK that's not
                    possible with classes, at least not if you don't derive from them.

                    Comment

                    • dean

                      #11
                      Re: class vs namespace?

                      Fred - that is a very good idea. Nice implementation hiding.

                      Thanks

                      Dean



                      Fred Zwarts wrote:
                      "dean" <deanbrown3d@ya hoo.comwrote in message news:1158332731 .153079.82250@b 28g2000cwb.goog legroups.com...
                      So what to do about member variables of the class, if we use a
                      namespace instead? I mean, how can one hide a private variable and just
                      allow accessor (get and set) functions? Put the member variable outside
                      of the namespace but in the same unit as the namespace?
                      >
                      I would think the other way around.
                      Put the private variable in the same namespace but in another unit
                      (the unit in which you write the body of the get and set functions).
                      The advantage above a class implementation is that the private variables
                      are not even declared in the header file.
                      The header file discloses nothing about the implementation,
                      it has only the interface
                      >
                      Fred.Zwarts.
                      >
                      Rolf Magnus wrote:
                      Jakob Bieling wrote:
                      >
                      dean <deanbrown3d@ya hoo.comwrote:
                      Hi all,
                      >
                      We have a few existing classes that we want to put into either another
                      class or a namespace, so that we can avoid naming conflicts. As far as
                      I can see the syntax is identical from a usage perspective.
                      >
                      Does anyone have an opinion on this either way?

                      For your purpose, namespaces are the way to go. You can reopen a
                      namespace and add stuff across translation units. You cannot do this
                      with classes. In addition, using a class implies that you can create an
                      instance of that class, which, as I understood it, is not the case.
                      >
                      Also, you can use using-declarations with namespaces, and AFAIK that's not
                      possible with classes, at least not if you don't derive from them.

                      Comment

                      • Kai-Uwe Bux

                        #12
                        Re: class vs namespace?

                        dean wrote [top posting corrected]
                        Default User wrote:
                        >dean wrote:
                        >>
                        So what to do about member variables of the class
                        >>
                        >>
                        >>
                        >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.html>
                        >>
                        I have no idea what to top-post means.
                        Top posting is to put your answer above the text that you are replying to.
                        It is frowned upon in this news group -- that's a cultural thing.

                        And anyway, what are you adding to this discussion?
                        E.g., that you might benefit from reading the FAQ (generally a good idea and
                        part of netiquette).


                        Best

                        Kai-Uwe Bux

                        Comment

                        • dean

                          #13
                          Re: class vs namespace?

                          Gosh! Clearly this is a very important issue for some people so I
                          apologise to them. I was replying to two people at the same time, not
                          just one - both of them had good responses. Should I have just quoted
                          one of them?



                          Kai-Uwe Bux wrote:
                          dean wrote [top posting corrected]
                          >
                          Default User wrote:
                          dean wrote:
                          >
                          So what to do about member variables of the class
                          >
                          >
                          >
                          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.html>
                          >
                          I have no idea what to top-post means.
                          >
                          Top posting is to put your answer above the text that you are replying to.
                          It is frowned upon in this news group -- that's a cultural thing.
                          >
                          >
                          And anyway, what are you adding to this discussion?
                          >
                          E.g., that you might benefit from reading the FAQ (generally a good idea and
                          part of netiquette).
                          >
                          >
                          Best
                          >
                          Kai-Uwe Bux

                          Comment

                          • Default User

                            #14
                            Re: class vs namespace?

                            dean wrote:
                            I have no idea what to top-post means.
                            1. I explained it. Here it is again: Your replies belong following or
                            interspersed with properly trimmed quotes.

                            2. The FAQ goes over it as well. Did you read it?
                            And anyway, what are you adding to this discussion?
                            I'm helping you to get help by posting correctly. It so happens that
                            I've automated things a bit, so it's easy for me to quickly post the
                            information. I do that so others don't.




                            Brian


                            Comment

                            • Noah Roberts

                              #15
                              Re: class vs namespace?


                              dean wrote:
                              Gosh! Clearly this is a very important issue for some people so I
                              apologise to them.
                              Usually when you want to apologise to someone you don't do the thing
                              you are apologising for at the same time; makes you appear inconcere.
                              If you aren't sorry then don't apologise (many here will end up
                              killfiling you) and if you are sorry then you won't do it again and
                              you'll try to learn how to communicate in the group effectively.
                              You've been given the resources to learn and now know that yes, this is
                              an important issue. Now you'll either stop top-posting or you won't.
                              You're decision will have an impact on how people percieve you in the
                              group and the quality and quantity of answers you recieve.

                              Comment

                              Working...