difference between a namespace and class with just default constructor

Y

Youssef Mesri

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!
 
Y

Youssef Mesri

Youssef said:
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!
 
V

Victor Bazarov

Youssef said:
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
 
J

Jerry Coffin

Youssef said:
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).
 
R

red floyd

Jerry said:
Youssef said:
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.
 
E

Earl Purple

Victor Bazorov wrote:
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

[end of quote]

Indeed it is similar but having them as static members of a class can
actually have an advantage in certain situations.
You can use the class as a template parameter, something you cannot do
with a namespace.
 
B

BigBrian

Youssef said:
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.
 
B

BigBrian

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.
 
E

Earl Purple

red said:
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.
 
E

Earl Purple

BigBrian said:
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).
 
B

BigBrian

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
 
E

Earl Purple

Default said:
Earl said:
Victor Bazorov wrote:
[end of quote]


This would be more readable if you used the actual Google quoting
mechanism. See .sig below.



Brian

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++.
 
D

Default User

Earl said:
Default User wrote:

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
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top