difference between a namespace and class with just default constructor

Discussion in 'C++' started by Youssef Mesri, Jan 17, 2006.

  1. What is the difference between these two situations:

    1- hold a namespace which contains justs some functions:

    namespace MyNamespace
    {
    void foo1();
    void foo2();
    void foo3();
    void foo4();
    ....

    }


    and
    2- hold a class within just a default constructor:

    class Myclass
    {
    public:
    Myclass() {};
    void foo1();
    void foo2();
    void foo3();
    void foo4();
    ...

    }

    Thanks in advance for your answers

    Yous!
    Youssef Mesri, Jan 17, 2006
    #1
    1. Advertising

  2. Youssef Mesri wrote:
    > What is the difference between these two situations:
    >
    > 1- hold a namespace which contains justs some functions:
    >
    > namespace MyNamespace
    > {
    > void foo1();
    > void foo2();
    > void foo3();
    > void foo4();
    > ...
    >
    > }
    >
    >
    > and
    > 2- hold a class within just a default constructor:
    >
    > class Myclass
    > {
    > public:
    > Myclass() {};
    > void foo1();
    > void foo2();
    > void foo3();
    > void foo4();
    > ...
    > ~Myclass() {};
    > }
    >
    > Thanks in advance for your answers
    >
    > Yous!
    Youssef Mesri, Jan 17, 2006
    #2
    1. Advertising

  3. Youssef Mesri wrote:
    > What is the difference between these two situations:
    >
    > 1- hold a namespace which contains justs some functions:
    >
    > namespace MyNamespace
    > {
    > void foo1();
    > void foo2();
    > void foo3();
    > void foo4();
    > ...
    >
    > }
    >
    >
    > and
    > 2- hold a class within just a default constructor:
    >
    > class Myclass
    > {
    > public:
    > Myclass() {};
    > void foo1();
    > void foo2();
    > void foo3();
    > void foo4();
    > ...
    >
    > }
    >
    > Thanks in advance for your answers


    Generally speaking, the difference is that to access thos member functions
    of a class, you need an instance of that class. And inside those member
    functions the instance is identifiable through the 'this' pointer. The
    namespace-based functions do not have the 'this' pointer. They are more
    like static members of the class. You could write

    class Myclass
    {
    public:
    static void foo1();
    static void foo2();
    ...
    };

    which would be _very_much_ like those functions in your namespace.

    V
    Victor Bazarov, Jan 17, 2006
    #3
  4. Youssef Mesri

    Jerry Coffin Guest

    Re: difference between a namespace and class with just default constructor

    Youssef Mesri wrote:
    > What is the difference between these two situations:
    >
    > 1- hold a namespace which contains justs some functions:


    [ ... ]

    > 2- hold a class within just a default constructor:


    You still have to create an instance of the class to call its member
    functions, whereas there's no such thing as an instance of a namespace.

    To get something closer to a namespace, your class would have a private
    ctor to prevent constructing objects, and the other functions would all
    be static so you could call them without referring to an particular
    object (since no such object will ever exist).

    --
    Later,
    Jerry.
    Jerry Coffin, Jan 17, 2006
    #4
  5. Youssef Mesri

    red floyd Guest

    Re: difference between a namespace and class with just default constructor

    Jerry Coffin wrote:
    > Youssef Mesri wrote:
    >> What is the difference between these two situations:
    >>
    >> 1- hold a namespace which contains justs some functions:

    >
    > [ ... ]
    >
    >> 2- hold a class within just a default constructor:

    >
    > You still have to create an instance of the class to call its member
    > functions, whereas there's no such thing as an instance of a namespace.
    >
    > To get something closer to a namespace, your class would have a private
    > ctor to prevent constructing objects, and the other functions would all
    > be static so you could call them without referring to an particular
    > object (since no such object will ever exist).
    >


    As far as I can tell (I don't have my copy of the Standard present), the
    differences between a namespace and a class with static only members
    would be:

    1. You can use a using directive to bring all or part of a namespace
    into the current scope.
    2. You can extend a namespace (add other members/etc...) without
    modifying the existing source.
    3. You can mark certain members of the class private or protected.
    red floyd, Jan 18, 2006
    #5
  6. Youssef Mesri

    Earl Purple Guest

    Re: difference between a namespace and class with just default constructor

    Victor Bazorov wrote:
    Earl Purple, Jan 18, 2006
    #6
  7. Youssef Mesri

    BigBrian Guest

    Re: difference between a namespace and class with just default constructor

    Youssef Mesri wrote:
    > What is the difference between these two situations:
    >
    > 1- hold a namespace which contains justs some functions:
    >
    > namespace MyNamespace
    > {
    > void foo1();
    > void foo2();
    > void foo3();
    > void foo4();
    > ...
    >
    > }
    >
    >
    > and
    > 2- hold a class within just a default constructor:
    >
    > class Myclass
    > {
    > public:
    > Myclass() {};
    > void foo1();
    > void foo2();
    > void foo3();
    > void foo4();
    > ...
    >
    > }
    >
    > Thanks in advance for your answers
    >
    > Yous!


    In addition to what others have said, namespaces are also open, they
    can be spread across files. For example you can do this

    //file1
    namespace Foo
    {
    void f1() { /* do something */ }
    }

    // file2
    namesapce Foo
    {
    void f2() { /* do something */ }
    }

    You can't do this with a class.
    BigBrian, Jan 18, 2006
    #7
  8. Youssef Mesri

    BigBrian Guest

    Re: difference between a namespace and class with just default constructor

    > In addition to what others have said, namespaces are also open, they
    > can be spread across files. For example you can do this
    >
    > //file1
    > namespace Foo
    > {
    > void f1() { /* do something */ }
    > }
    >
    > // file2
    > namesapce Foo
    > {
    > void f2() { /* do something */ }
    > }
    >
    > You can't do this with a class.


    Actually, the separate files aren't the point. You can't do this in
    the same file either with a class. But namespaces are open, that was
    my point.
    BigBrian, Jan 18, 2006
    #8
  9. Youssef Mesri

    Earl Purple Guest

    Re: difference between a namespace and class with just default constructor

    red floyd wrote:
    >
    > As far as I can tell (I don't have my copy of the Standard present), the
    > differences between a namespace and a class with static only members
    > would be:
    >
    > 1. You can use a using directive to bring all or part of a namespace
    > into the current scope.
    > 2. You can extend a namespace (add other members/etc...) without
    > modifying the existing source.
    > 3. You can mark certain members of the class private or protected.


    4. You can use the class as a template parameter.
    Earl Purple, Jan 18, 2006
    #9
  10. Youssef Mesri

    Earl Purple Guest

    Re: difference between a namespace and class with just default constructor

    BigBrian wrote:

    > Actually, the separate files aren't the point. You can't do this in
    > the same file either with a class. But namespaces are open, that was
    > my point.


    Although classes aren't open, there is a workaround which is to define
    free functions that take class members as parameters.

    In this case it can also be done with a function that takes multiple
    overloads (and may or may not have a template version that works by
    default).

    Of course the free functions only get public access (unless you modify
    the header of the class which we've assumed you're not allowed to do)
    and must themselves be public (though not necessarily unlimited in
    scope as they can be written in an anonymous namespace).
    Earl Purple, Jan 18, 2006
    #10
  11. Youssef Mesri

    BigBrian Guest

    Re: difference between a namespace and class with just default constructor

    Youssef Mesri wrote
    > What is the difference between these two situations
    >
    > 1- hold a namespace which contains justs some functions
    >
    > namespace MyNamespac
    >
    > void foo1()
    > void foo2()
    > void foo3()
    > void foo4()
    > ..
    >
    >
    >
    >
    > an
    > 2- hold a class within just a default constructor
    >
    > class Myclas
    >
    > public
    > Myclass() {}
    > void foo1()
    > void foo2()
    > void foo3()
    > void foo4()
    > ..
    >
    >
    >
    > Thanks in advance for your answer
    >
    > Yous
    >

    In addition to what others have said, namespaces are also open, the
    can be spread across files. For example you can do thi

    //file
    namespace Fo

    void f1() { /* do something */


    // file
    namesapce Fo

    void f2() { /* do something */


    You can't do this with a class
    BigBrian, Jan 18, 2006
    #11
  12. Youssef Mesri

    Default User Guest

    Re: difference between a namespace and class with just default constructor

    Earl Purple wrote:

    > Victor Bazorov wrote:
    >
    Default User, Jan 18, 2006
    #12
  13. Youssef Mesri

    Earl Purple Guest

    Re: difference between a namespace and class with just default constructor

    Default User wrote:
    > Earl Purple wrote:
    >
    > > Victor Bazorov wrote:
    > >
    Earl Purple, Jan 18, 2006
    #13
  14. Youssef Mesri

    JustBoo Guest

    Re: difference between a namespace and class with just default constructor

    On 18 Jan 2006 11:59:35 -0800, "Earl Purple" <>
    wrote:
    >> Please quote enough of the previous message for context. To do so from
    >> Google, click "show options" and use the Reply shown in the expanded
    >> header.

    >I know what to do was waiting a lifetime for the form to come up. I
    >blame the programmers - obviously it wasn't written in standard C++.


    You don't have to use Google.

    http://www.forteinc.com/main/homepage.php

    They have a free version that is very good.

    --
    Solitary trees, if they grow at all, grow strong.
    Winston Churchill
    JustBoo, Jan 18, 2006
    #14
  15. Youssef Mesri

    Default User Guest

    Re: difference between a namespace and class with just default constructor

    Earl Purple wrote:

    >
    > Default User wrote:


    > > Please quote enough of the previous message for context. To do so
    > > from Google, click "show options" and use the Reply shown in the
    > > expanded header.

    >
    > I know what to do was waiting a lifetime for the form to come up. I
    > blame the programmers - obviously it wasn't written in standard C++.


    Yeah, I saw in other messages that you were using the usual quote
    mechanism there. Google's a tough newsreader, I had to use it for about
    four months in the early part of 2005. I feel your pain.




    Brian
    Default User, Jan 18, 2006
    #15
  16. Youssef Mesri

    Earl Purple Guest

    Re: difference between a namespace and class with just default constructor

    JustBoo wrote:
    >
    > You don't have to use Google.
    >
    > http://www.forteinc.com/main/homepage.php
    >
    > They have a free version that is very good.


    This is getting O/T but from work google is the easiest (and possibly
    only) option.
    Earl Purple, Jan 19, 2006
    #16
    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. jakk
    Replies:
    4
    Views:
    12,118
  2. Jason Heyes
    Replies:
    1
    Views:
    448
    Woebegone
    Nov 19, 2004
  3. Max
    Replies:
    2
    Views:
    476
    Joe Kesselman
    Aug 1, 2006
  4. Replies:
    9
    Views:
    959
    Alf P. Steinbach
    Mar 6, 2006
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,218
Loading...

Share This Page