![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4
![]() |
namespace vs struct
Usually when I want global functions in my C++ applications I do like this:
namespace Tool
{
static inline int foo() {}
static inline int bar() {}
}
int main()
{
Tool::foo();
Tool::bar();
}Another more common way to achieve this result is by doing it this way: struct Tool
{
static inline int foo() {}
static inline int bar() {}
};
int main()
{
Tool::foo();
Tool::bar();
}So to my question: Is there a difference between them (have not checked the generated code yet)? Which one should I prefer and why? Thanks for your thoughts! /Klarre
__________________
http://www.klarre.se |
|
|
|
|
|
#2 |
|
hi: for(;;) goto hi;
|
Well I'm no pro but I'm pretty sure the second one doesn't work. More like
struct Tool
{
static inline int foo() {}
static inline int bar() {}
};
int main()
{
Tool hammer;
hammer::foo();
hammer::bar();
}Hope I helped >_>
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. |
|
|
|
|
|
#3 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4
![]() |
The functions are static and therefor you can call them without an object.
__________________
http://www.klarre.se |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
I think namespaces are there to resolve naming conflicts for linker in different parts of the program. For example, if you had a function with declaration "int foo() {}" in one file of your program, you shouldnot be able to have the same thing somewhere else unless its in different namespace. This is vary useful in program organization in large programs. Should be nothing more and nothing less.
__________________
Spread your wings and fly! Chicken! |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,221
Rep Power: 5
![]() |
Assuming all of your functions are static, there is no difference between the two approaches. A name of the struct (or class) is, in this context, a namespace. That is, among other things, why the syntax "Tool::foo();" works for calling Tool::foo() in both cases.
The example given by peaceofpi does not work, as the name of an object is not a namespace. |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
I would suggest going with the namespace, from a practical point it means you don't have to declare everything you want to put into 'Tool' at the one point.
Plus, stylistically, I feel the namespace version more clearly expresses your intentions. Could be just me though.
__________________
Visit my website BinaryNotions. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Dec 2006
Posts: 9
Rep Power: 0
![]() |
I would suggest going for the struct as you can do this
Tool a; a.foo(); or something of that effect. You can also go for classes as you can inherit them. (but now you can also inherit them in structs) which can make your expansion easier. But if you won't make variables out of struct, it defeats the purpose and so it's cooler to use Namespace. |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2
![]() |
I guess that namespaces and structs are way too different. Most of their differences are in the way you concieve your program and the reusability of the code. You create a namespace when you create your own library or anything like that if you want to prevent nameclashes. You create struct if you have to abstract an object in the real life( because c++ treats structs and unions the same as classes).
__________________
You never test the depth of a river with both feet. The believer is happy. The doubter is wise. Free speech carries with it some freedom to listen. The next generation will always surpass the previous one. It`s one of the never ending cycles of life. |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
A namespace defines a scope. All its members are public; forget access specifiers. Unlike a class or a struct, it is open. That is, you can extend it later with more definitions. Also unlike a class or struct, the closing brace does not have a following semicolon.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to create struct containing variable array struct | shoeyfighter | C | 14 | Nov 6th, 2006 3:26 AM |
| Casting a struct to s aimilar struct | shoeyfighter | C | 6 | Oct 27th, 2006 2:59 PM |
| Appreciate comments on some linked list code | Jessehk | C | 5 | Jul 20th, 2006 7:59 PM |
| struct program skipping inputs | codylee270 | C | 7 | Dec 5th, 2005 7:02 AM |
| c struct : problems whit structs containing each other | mevuorin | C | 10 | Jul 7th, 2005 1:05 PM |