Class Without Data Members?

I

Immortal Nephi

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;
}
 
J

Jonathan Lee

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
 
S

Saeed Amrollahi

        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
 
D

DDD

Immortal said:
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() {...}
 
P

Puppet_Sock

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
 
K

Kai-Uwe Bux

Puppet_Sock said:
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
 
B

Brian

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top