C ++ - namespace and static functions

Possible duplicate:
Namespace + functions versus static methods in a class

I want to group similar togther functions. I can do this in one of two ways. For me, these are just syntactic differences ... in the end, it doesn't matter. How accurate is this view?

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

Grade:

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
+5
source share
2 answers

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

In addition, the code is incorrect, sizeof(array)it will not return what you think.

+5
source

, () , :

  • , Util Util foo; Util* foo = new Util();.
  • Util ( - , ). , , , vector<Util>.
  • Util sizeof(Util).

, , . , , , .

: sizeof(array) sizeof(int*) , , (.. , ).

+2

All Articles