6

Possible Duplicate:
Namespace + functions versus static methods on a class

I want to group similar functions togther. I can do it one of two ways. To me they are just syntactical differences...in the end it does not matter. Is this view accurate?

Namespace:

namespace util
  {
  void print_array(int array[])
    {
    int count = sizeof( array ) / sizeof( array[0] );
    for (int i = 0; i <= count; i++) cout << array[i];
    }
  }

Class:

class Util
  {
  public:
    static void print_array(int array[])
      {
      int count = sizeof(array);
      for (int i = 0; i <= count; i++) cout << array[i];
      }
  };

Call with

Util::print_array()  // Class

or

util::print_array()  // Namespace
Community
  • 1
  • 1
  • 1
    We had this discussion just recently. Hopefully someone will post a link. Namespaces are subject to ADL, while classes are not; classes can be templated, while namespaces cannot. – Kerrek SB Oct 13 '11 at 14:44

2 Answers2

6

The second way is silly and a complete nonsense. Classes are not namespaces, and should never be used as such.

Also, the code is wrong, sizeof(array) won't return what you think.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • Classes can be templated, though. – Kerrek SB Oct 13 '11 at 14:46
  • 2
    @KerrekSB: Functions, too. The benefit is partial specialisation, but if you know about that, you're not asking silly questions like this one. – Cat Plus Plus Oct 13 '11 at 14:47
  • 2
    The "complete nonsense" bit seems overblown: if a compiler will compile it and it has the desired effect, it's sensical by definition. "Silly" is more subjective, but you should still back it up. So, please explain *why* you feel that classes should never be used in the proposed manner. – Caleb Oct 13 '11 at 15:05
  • 1
    @JohnDibling: Functors with all static members? The question is about treating classes as namespaces, and the answer to that is "no, use namespaces". I thought we'd agreed by now that C++ is not Java. (I am tired, though, and could be missing something, but I don't think any concepts advanced than that apply to this question.) – Cat Plus Plus Oct 13 '11 at 15:17
  • @Cat: I agree in principle with you if you are saying "prefer to use namespaces." But that's not what it *looks* like you're saying. It looks like you're saying "there is never an instance in which you should use a class as a namespace construct." – John Dibling Oct 13 '11 at 15:29
  • I can think of one reason to use such a `class`, or at least one case where I did because I didn't have another idea. Specifically, I once had several `class`es entirely defined in a header file. I wanted them to be able to share some functions, but I didn't want anyone including that header file to gain access to those functions. My solution was to make the functions `static` and `private` in an otherwise empty class and then make the other classes `friend`s of it. – Joshua Green Jul 18 '15 at 01:45
3

There are some differences in that the second way allows you to do a couple of (nonsensical) things that the first way does not:

  • Using the second way you can create instances of Util using either Util foo; or Util* foo = new Util();.
  • You can also use Util as a type parameter for templates (or anywhere else where typenames are allowed). So you could e.g. create a vector<Util>.
  • You can also calculate the size of Util instances using sizeof(Util).

Since there's no reason you'd want to be able to do either of those things, you should use the first way. Generally you should almost always rethink using a class when there are no circumstances under which you'd want to instantiate it.

On an unrelated note: In your code sizeof(array) is equivalent to sizeof(int*) and almost certainly doesn't do what you think it does (i.e. it does not tell you how many elements are in the array).

sepp2k
  • 363,768
  • 54
  • 674
  • 675