By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,811 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,811 IT Pros & Developers. It's quick & easy.

difference between a namespace and class with just default constructor

P: n/a
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!
Jan 17 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Youssef Mesri wrote:
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!

Jan 17 '06 #2

P: n/a
Youssef Mesri wrote:
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
Jan 17 '06 #3

P: n/a
Youssef Mesri wrote:
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).

--
Later,
Jerry.

Jan 17 '06 #4

P: n/a
Jerry Coffin wrote:
Youssef Mesri wrote:
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.
Jan 18 '06 #5

P: n/a
Victor Bazorov wrote:
[quote]
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.

Jan 18 '06 #6

P: n/a

Youssef Mesri wrote:
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.

Jan 18 '06 #7

P: n/a
> 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.

Jan 18 '06 #8

P: n/a

red floyd wrote:

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.

Jan 18 '06 #9

P: n/a

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

Jan 18 '06 #10

P: n/a
Youssef Mesri wrote:
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.

Jan 18 '06 #11

P: n/a
Earl Purple wrote:
Victor Bazorov wrote:
[quote]
[end of quote]

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

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Jan 18 '06 #12

P: n/a

Default User wrote:
Earl Purple wrote:
Victor Bazorov wrote:
[quote]


[end of quote]

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

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.


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

Jan 18 '06 #13

P: n/a
On 18 Jan 2006 11:59:35 -0800, "Earl Purple" <ea********@gmail.com>
wrote:
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.

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


You don't have to use Google.

http://www.forteinc.com/main/homepage.php

They have a free version that is very good.

--
Solitary trees, if they grow at all, grow strong.
Winston Churchill
Jan 18 '06 #14

P: n/a
Earl Purple wrote:

Default User wrote:

Please quote enough of the previous message for context. To do so
from Google, click "show options" and use the Reply shown in the
expanded header.


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
Jan 18 '06 #15

P: n/a

JustBoo wrote:

You don't have to use Google.

http://www.forteinc.com/main/homepage.php

They have a free version that is very good.


This is getting O/T but from work google is the easiest (and possibly
only) option.

Jan 19 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.