Class Without Data Members?

Discussion in 'C++' started by Immortal Nephi, May 10, 2010.

  1. There are many global functions with numbers of parameters and return
    type. I want to group them into a class. The class does not have
    data members because functions do not need them. I include that class
    into another class. Is practice normal?

    For example:

    class Foo
    {
    public:
    int foo( int a, int b ) { /*…*/ return iReturn; }
    int bar( int a, int b, int c ) { /*…*/ return iReturn; }
    /*…*/
    int doit( int a ) { /*…*/ return iReturn; }

    /* Do not need to include data members here */
    };

    class Object
    {
    public:
    Foo foo;
    void doit() { foo.bar( 1, 2, 3 ); }

    private:
    /* Here are data members */
    };

    int main()
    {
    Object obj;
    obj.doit();

    return 0;
    }
    Immortal Nephi, May 10, 2010
    #1
    1. Advertising

  2. Immortal Nephi

    Jonathan Lee Guest

    On May 9, 9:45 pm, Immortal Nephi <> wrote:
    > There are many global functions with numbers of parameters and return
    > type.  I want to group them into a class.  The class does not have
    > data members because functions do not need them.  I include that class
    > into another class.


    Use a namespace.

    --Jonathan
    Jonathan Lee, May 10, 2010
    #2
    1. Advertising

  3. On May 10, 5:45 am, Immortal Nephi <> wrote:
    >         There are many global functions with numbers of parameters and return
    > type.  I want to group them into a class.  The class does not have
    > data members because functions do not need them.  I include that class
    > into another class.  Is practice normal?
    >
    > For example:
    >
    > class Foo
    > {
    > public:
    >         int foo( int a, int b ) { /*…*/ return iReturn; }
    >         int bar( int a, int b, int c ) { /*…*/ return iReturn; }
    >         /*…*/
    >         int doit( int a ) { /*…*/ return iReturn; }
    >
    >         /* Do not need to include data members here */
    >
    > };
    >
    > class Object
    > {
    > public:
    >         Foo foo;
    >         void doit() { foo.bar( 1, 2, 3 ); }
    >
    > private:
    >         /* Here are data members */
    >
    > };
    >
    > int main()
    > {
    >         Object obj;
    >         obj.doit();
    >
    >         return 0;
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -


    Hi

    Besides namespace, I use a class with a lot of -related-
    static member functions.
    For example, in a GUI application, I likely to fill the
    gender combo box or employee list box, several times in
    different forms. So I write a static function like this:
    struct GUIInterface {
    void Fill(ListBox&, const std::vector<std::string>& emp);
    // ...
    };
    Booch, in his book: Object-Oriented Analysis and Design with
    applications, called such abstraction: Virtual Machine abstraction and
    Coincidental abstraction.

    Regards,
    -- Saeed Amrollahi
    Saeed Amrollahi, May 10, 2010
    #3
  4. Immortal Nephi

    DDD Guest

    Immortal Nephi wrote:
    > There are many global functions with numbers of parameters and return
    > type. I want to group them into a class. The class does not have
    > data members because functions do not need them. I include that class
    > into another class. Is practice normal?
    >
    > For example:
    >
    > class Foo
    > {
    > public:
    > int foo( int a, int b ) { /*…*/ return iReturn; }
    > int bar( int a, int b, int c ) { /*…*/ return iReturn; }
    > /*…*/
    > int doit( int a ) { /*…*/ return iReturn; }
    >
    > /* Do not need to include data members here */
    > };
    >
    > class Object
    > {
    > public:
    > Foo foo;
    > void doit() { foo.bar( 1, 2, 3 ); }
    >
    > private:
    > /* Here are data members */
    > };
    >


    Why not putting Foo foo in private scope.

    private:
    Foo foo;
    public:
    void doit() {...}

    > int main()
    > {
    > Object obj;
    > obj.doit();
    >
    > return 0;
    > }
    DDD, May 10, 2010
    #4
  5. Immortal Nephi

    Puppet_Sock Guest

    On May 9, 10:02 pm, Jonathan Lee <> wrote:
    > On May 9, 9:45 pm, Immortal Nephi <> wrote:
    >
    > > There are many global functions with numbers of parameters and return
    > > type.  I want to group them into a class.  The class does not have
    > > data members because functions do not need them.  I include that class
    > > into another class.

    >
    > Use a namespace.


    A namespace gives just about as much protection
    and convenience.

    It does not require creating an instance of a
    class to hold the functions. You just need to
    remember to decorate references to the functions
    with namespace:: etc.

    The only reason I can think of to use a class
    instead of a namespace is, if you think that it
    is likely that at some point you will include
    some data members in the class that holds the
    functions. And that you would then possibly
    need more than one copy of those data members.
    Socks
    Puppet_Sock, May 10, 2010
    #5
  6. Immortal Nephi

    Kai-Uwe Bux Guest

    Puppet_Sock wrote:

    > On May 9, 10:02 pm, Jonathan Lee <> wrote:
    >> On May 9, 9:45 pm, Immortal Nephi <> wrote:
    >>
    >> > There are many global functions with numbers of parameters and return
    >> > type. I want to group them into a class. The class does not have
    >> > data members because functions do not need them. I include that class
    >> > into another class.

    >>
    >> Use a namespace.

    >
    > A namespace gives just about as much protection
    > and convenience.
    >
    > It does not require creating an instance of a
    > class to hold the functions.


    Neither does a class. Just make the function you want to export static.

    > You just need to
    > remember to decorate references to the functions
    > with namespace:: etc.
    >
    > The only reason I can think of to use a class
    > instead of a namespace is, if you think that it
    > is likely that at some point you will include
    > some data members in the class that holds the
    > functions. And that you would then possibly
    > need more than one copy of those data members.
    > Socks


    Other reasons are more syntactical. A namespace is open: clients can later
    add members to it; a class is closed. Also, using declarations won't work on
    classes; hence you can force client code to use the scope::member notation.
    Finally, you can have private members invisible to the outside.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, May 10, 2010
    #6
  7. Immortal Nephi

    Brian Guest

    On May 10, 2:53 pm, Kai-Uwe Bux <> wrote:
    > Puppet_Sock wrote:
    > > On May 9, 10:02 pm, Jonathan Lee <> wrote:
    > >> On May 9, 9:45 pm, Immortal Nephi <> wrote:

    >
    > >> > There are many global functions with numbers of parameters and return
    > >> > type.  I want to group them into a class.  The class does not have
    > >> > data members because functions do not need them.  I include that class
    > >> > into another class.

    >
    > >> Use a namespace.

    >
    > > A namespace gives just about as much protection
    > > and convenience.

    >
    > > It does not require creating an instance of a
    > > class to hold the functions.

    >
    > Neither does a class. Just make the function you want to export static.


    I just create an instance and use that to make the calls.
    I'm not sure if doing things with static functions would
    be an improvement. The only thing I can think of about
    using static functions that might be better is it is
    clearer what's going on. But with my high-level generated
    classes, there aren't any data members so I don't think
    there's much possibility for ambiguity.


    Brian Wood
    http://webEbenezer.net
    (651) 251-9384


    >
    > > You just need to
    > > remember to decorate references to the functions
    > > with namespace:: etc.

    >
    > > The only reason I can think of to use a class
    > > instead of a namespace is, if you think that it
    > > is likely that at some point you will include
    > > some data members in the class that holds the
    > > functions. And that you would then possibly
    > > need more than one copy of those data members.
    > > Socks

    >
    > Other reasons are more syntactical. A namespace is open: clients can later
    > add members to it; a class is closed. Also, using declarations won't work on
    > classes; hence you can force client code to use the scope::member notation.
    > Finally, you can have private members invisible to the outside.
    >
    > Best
    >
    > Kai-Uwe Bux
    Brian, May 11, 2010
    #7
    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. Brahmam
    Replies:
    3
    Views:
    549
    Francois Beaussier
    Jan 11, 2006
  2. CoolPint
    Replies:
    8
    Views:
    958
    Jeff Schwab
    Dec 14, 2003
  3. Xiangliang Meng
    Replies:
    2
    Views:
    3,464
    Ioannis Vranos
    Jun 21, 2004
  4. hdixon
    Replies:
    3
    Views:
    630
    hdixon
    Jul 9, 2006
  5. Jason
    Replies:
    2
    Views:
    495
    Jonathan Mcdougall
    May 13, 2006
Loading...

Share This Page