How do namespace scope and class scope differ?

Discussion in 'C++' started by Steven T. Hatton, Jul 18, 2005.

  1. It was once suggested to me that I could accomplish much the same thing that
    modules would accomplish (if C++ had modules) by writing my entire program
    - except for main() - inside of a class. When it was suggested, I didn't
    take it very seriously, but I have recently begun wondering if it is an
    idea worth considering. I'm now trying to think of what fundamental
    differences might exist between namespace scope, and class scope.

    One that I can think of is that I would not be able to have a using
    declaration or directive at class scope. I'm not convinced that is a great
    loss. Can anybody think of other restrictions or differences that might
    exist if a class were used as something of an instantiated namespace?

    Is anybody aware of an example of this kind of design being used
    (effectively)?
    --
    If our hypothesis is about anything and not about some one or more
    particular things, then our deductions constitute mathematics. Thus
    mathematics may be defined as the subject in which we never know what we
    are talking about, nor whether what we are saying is true.-Bertrand Russell
    Steven T. Hatton, Jul 18, 2005
    #1
    1. Advertising

  2. Steven T. Hatton wrote:
    > It was once suggested to me that I could accomplish much the same thing that
    > modules would accomplish (if C++ had modules) by writing my entire program
    > - except for main() - inside of a class. When it was suggested, I didn't
    > take it very seriously, but I have recently begun wondering if it is an
    > idea worth considering. I'm now trying to think of what fundamental
    > differences might exist between namespace scope, and class scope.
    >
    > One that I can think of is that I would not be able to have a using
    > declaration or directive at class scope. I'm not convinced that is a great
    > loss. Can anybody think of other restrictions or differences that might
    > exist if a class were used as something of an instantiated namespace?
    >
    > Is anybody aware of an example of this kind of design being used
    > (effectively)?


    Something that classes offer over namespaces is better encapsulation.
    You control what other classes have access to, and what they don't.
    Namespaces simply group things together and don't really offer that
    kind of ability, so any other "module" can use your namespace without
    any real protection from it. Classes on the other hand, through
    encapsulation let you define what part of modules can interact.

    Personal I have used classes to simply generic programming that the
    programmer before me used. He had everything, including global
    variables, lumped together. I took the functions, variables, and
    grouped them together and then limited the scope to only what they
    needed to (and fixed about 50-60 latent bugs in the process).

    Sorta depends on what language you're coming from (for modules) but you
    can think of classes as a very similar device. Class scope can be as
    open (public) or as closed (private) as you want it to be, and that
    flexibility will help you from creating alot of errors and hassle.

    HTH,
    Josh McFarlane
    Josh Mcfarlane, Jul 19, 2005
    #2
    1. Advertising

  3. Steven T. Hatton

    Ian Guest

    Steven T. Hatton wrote:
    > It was once suggested to me that I could accomplish much the same thing that
    > modules would accomplish (if C++ had modules) by writing my entire program
    > - except for main() - inside of a class. When it was suggested, I didn't
    > take it very seriously, but I have recently begun wondering if it is an
    > idea worth considering. I'm now trying to think of what fundamental
    > differences might exist between namespace scope, and class scope.
    >

    Visibility? You'd have to use struct or make everything public.

    You'd have to put everything in one header.

    Following on from that, you couldn't extend the namespace.

    > One that I can think of is that I would not be able to have a using
    > declaration or directive at class scope. I'm not convinced that is a great
    > loss. Can anybody think of other restrictions or differences that might
    > exist if a class were used as something of an instantiated namespace?
    >
    > Is anybody aware of an example of this kind of design being used
    > (effectively)?


    Not for a long time, the only times I came across this was back in the
    days when namespaces were new and not supported by many compilers.

    I suppose nested classes are a form of this.

    Ian
    Ian, Jul 19, 2005
    #3
  4. Josh Mcfarlane wrote:

    > Something that classes offer over namespaces is better encapsulation.
    > You control what other classes have access to, and what they don't.
    > Namespaces simply group things together and don't really offer that
    > kind of ability, so any other "module" can use your namespace without
    > any real protection from it. Classes on the other hand, through
    > encapsulation let you define what part of modules can interact.
    >
    > Personal I have used classes to simply generic programming that the
    > programmer before me used. He had everything, including global
    > variables, lumped together. I took the functions, variables, and
    > grouped them together and then limited the scope to only what they
    > needed to (and fixed about 50-60 latent bugs in the process).
    >
    > Sorta depends on what language you're coming from (for modules) but you
    > can think of classes as a very similar device. Class scope can be as
    > open (public) or as closed (private) as you want it to be, and that
    > flexibility will help you from creating alot of errors and hassle.
    >
    > HTH,
    > Josh McFarlane

    Actually, in some ways what I'm contemplating is further from the concept of
    a module than is a C++ namespace. If my class were strictly singleton, then
    it would more readily qualify as a proper module. The closest thing to a
    module I've used is probably Mathematica's package system.

    I've got a pretty good idea of the basics of C++. I was really asking about
    the more subtle issues. The best example is the one I've already
    mentioned. That is, no using directives or using declarations at class
    scope. I guess I could typedef my way around that if it proved useful.

    What's going through my head is actually starting to scare me. It just
    might prove useful.
    --
    If our hypothesis is about anything and not about some one or more
    particular things, then our deductions constitute mathematics. Thus
    mathematics may be defined as the subject in which we never know what we
    are talking about, nor whether what we are saying is true.-Bertrand Russell
    Steven T. Hatton, Jul 19, 2005
    #4
  5. Ian wrote:

    > Steven T. Hatton wrote:
    >> It was once suggested to me that I could accomplish much the same thing
    >> that modules would accomplish (if C++ had modules) by writing my entire
    >> program
    >> - except for main() - inside of a class. When it was suggested, I didn't
    >> take it very seriously, but I have recently begun wondering if it is an
    >> idea worth considering. I'm now trying to think of what fundamental
    >> differences might exist between namespace scope, and class scope.
    >>

    > Visibility? You'd have to use struct or make everything public.
    >
    > You'd have to put everything in one header.


    Well...two qualifications on that. 1) I could spread the blinkin' class
    definition over a zillion files and the CPP would accept it. Not saying
    this is a good thing, mind you. 2) I could still put the implementation in
    a source file, even if that means lots of inner classes. But your point is
    valid in the sense that doing otherwise with the definition would simply be
    begging for trouble.

    > Following on from that, you couldn't extend the namespace.


    This is correct. I will note that some people have suggested opening
    classes in the same way namespaces are opened. I hope that doesn't survive
    the Committee.

    >> One that I can think of is that I would not be able to have a using
    >> declaration or directive at class scope. I'm not convinced that is a
    >> great loss. Can anybody think of other restrictions or differences that
    >> might exist if a class were used as something of an instantiated
    >> namespace?
    >>
    >> Is anybody aware of an example of this kind of design being used
    >> (effectively)?

    >
    > Not for a long time, the only times I came across this was back in the
    > days when namespaces were new and not supported by many compilers.
    >
    > I suppose nested classes are a form of this.


    Very much what I was thinking. Basically the entire program would consist
    of the one big outter class, and the rest would be inner classes, and other
    class members. If I were to persue this course, and find it useful, it may
    be worthwhile to create some mechanism for spreading the class definition
    over multiple files, say in a directory which only held those files, and
    with names reflecting the inner classes defined therein. It does, however,
    sound rather kludgey.
    --
    If our hypothesis is about anything and not about some one or more
    particular things, then our deductions constitute mathematics. Thus
    mathematics may be defined as the subject in which we never know what we
    are talking about, nor whether what we are saying is true.-Bertrand Russell
    Steven T. Hatton, Jul 19, 2005
    #5
  6. Steven T. Hatton

    Zorro Guest

    A deep and informative discussion.

    Class is a mechanism for CREATING new types so that their instances
    will represent abstractions of the entities in the problem domain. On
    the other hand, namespace is a packaging mechanism for libraries. One
    of the problems that I had to fix with C++ is the implementation of
    static methods. You can effectively turn a class into a namespace by
    making everything static. That is how enumeration is (or was)
    implemented in Java. Now, what does it mean for a type mechanism to
    turn into a packaging mechanism?

    I also extended namespaces so you can derive them from one another,
    like classes. Then, I added private public and protected sections. The
    public section is what a namespace exports. Finally, I separated the
    definition of a namespace from its implementation, just as is done for
    classes.

    Yes, C++ lacks the notion of module (component, etc). Some special
    cases are DLL. However, that was the very first thing I did in
    extending C++ to component-oriented development.

    Perhaps you will find these extensions easier to use than wasting time
    on bending C++.

    Regards,
    Dr. Z.
    Chief Scientist

    http://www.zhmicro.com
    http://distributed-software.blogspot.com
    Zorro, Jul 19, 2005
    #6
  7. Zorro wrote:

    > A deep and informative discussion.
    >
    > Class is a mechanism for CREATING new types so that their instances
    > will represent abstractions of the entities in the problem domain. On
    > the other hand, namespace is a packaging mechanism for libraries. One
    > of the problems that I had to fix with C++ is the implementation of
    > static methods. You can effectively turn a class into a namespace by
    > making everything static. That is how enumeration is (or was)
    > implemented in Java. Now, what does it mean for a type mechanism to
    > turn into a packaging mechanism?
    >
    > I also extended namespaces so you can derive them from one another,
    > like classes. Then, I added private public and protected sections. The
    > public section is what a namespace exports. Finally, I separated the
    > definition of a namespace from its implementation, just as is done for
    > classes.
    >
    > Yes, C++ lacks the notion of module (component, etc). Some special
    > cases are DLL. However, that was the very first thing I did in
    > extending C++ to component-oriented development.
    >
    > Perhaps you will find these extensions easier to use than wasting time
    > on bending C++.
    >
    > Regards,
    > Dr. Z.
    > Chief Scientist
    >
    > http://www.zhmicro.com
    > http://distributed-software.blogspot.com

    You MySQL is down.
    --
    If our hypothesis is about anything and not about some one or more
    particular things, then our deductions constitute mathematics. Thus
    mathematics may be defined as the subject in which we never know what we
    are talking about, nor whether what we are saying is true.-Bertrand Russell
    Steven T. Hatton, Jul 19, 2005
    #7
  8. Steven T. Hatton

    Zorro Guest

    The socket was gone. Sorry about that. It is up now, and thanks for the
    help.

    Thanks,
    Z.
    Zorro, Jul 19, 2005
    #8
  9. Steven T. Hatton

    Mercator Guest

    Steven T. Hatton wrote:
    > It was once suggested to me that I could accomplish much the same thing that
    > modules would accomplish (if C++ had modules) by writing my entire program
    > - except for main() - inside of a class. When it was suggested, I didn't
    > take it very seriously, but I have recently begun wondering if it is an
    > idea worth considering. I'm now trying to think of what fundamental
    > differences might exist between namespace scope, and class scope.


    This article series might interest you:
    http://www.cuj.com/documents/s=8064/cuj9806saks/
    Mercator, Jul 19, 2005
    #9
  10. Steven T. Hatton

    Kev Guest

    "Josh Mcfarlane" <> wrote in news:1121729159.955525.190150
    @o13g2000cwo.googlegroups.com:

    > Personal I have used classes to simply generic programming that the
    > programmer before me used. He had everything, including global
    > variables, lumped together. I took the functions, variables, and
    > grouped them together and then limited the scope to only what they
    > needed to (and fixed about 50-60 latent bugs in the process).


    Im pretty new to namespaces, certainly custom ones. At the moment the only
    one I have includes a few classes for somewhat specialized random functions
    that are used in many places. Ex: MyClass::Random::Truck(). The static fix
    was unexpected but wasnt really a problem after figuring it out... which
    admittedly wasnt a snap for me ;o)

    But they do seem to help me organize things more. Before, I simply included
    instances of the relevant classes when I needed them.... Random.Truck(). I
    know "Random" wasnt a good choice.. and will be fixed. If they were only
    needed in a few places, would using classes be the better choice?
    Kev, Jul 19, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alex9128
    Replies:
    2
    Views:
    670
  2. ad
    Replies:
    0
    Views:
    334
  3. Anonymous
    Replies:
    3
    Views:
    529
    Ron Natalie
    Aug 18, 2003
  4. GRenard
    Replies:
    2
    Views:
    418
    Salt_Peter
    Nov 3, 2006
  5. kunjaan
    Replies:
    8
    Views:
    111
    Charles Oliver Nutter
    May 28, 2009
Loading...

Share This Page