Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 27th, 2006, 9:11 AM   #1
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 318
Rep Power: 4 Klarre is on a distinguished road
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
Klarre is offline   Reply With Quote
Old Dec 27th, 2006, 1:20 PM   #2
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 123
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
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.
peaceofpi is offline   Reply With Quote
Old Dec 27th, 2006, 3:13 PM   #3
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 318
Rep Power: 4 Klarre is on a distinguished road
Quote:
Originally Posted by peaceofpi View Post
Well I'm no pro but I'm pretty sure the second one doesn't work.
The functions are static and therefor you can call them without an object.
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old Dec 27th, 2006, 4:27 PM   #4
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
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!
rsnd is offline   Reply With Quote
Old Dec 27th, 2006, 6:30 PM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,318
Rep Power: 5 grumpy will become famous soon enough
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.
grumpy is offline   Reply With Quote
Old Dec 27th, 2006, 7:24 PM   #6
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
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.
Eoin is offline   Reply With Quote
Old Dec 29th, 2006, 3:28 AM   #7
prototype_angel
Newbie
 
Join Date: Dec 2006
Posts: 9
Rep Power: 0 prototype_angel is on a distinguished road
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.
prototype_angel is offline   Reply With Quote
Old Dec 29th, 2006, 11:43 AM   #8
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3 pegasus001 is on a distinguished road
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.
pegasus001 is offline   Reply With Quote
Old Dec 29th, 2006, 12:40 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create struct containing variable array struct shoeyfighter C 14 Nov 6th, 2006 4:26 AM
Casting a struct to s aimilar struct shoeyfighter C 6 Oct 27th, 2006 3:59 PM
Appreciate comments on some linked list code Jessehk C 5 Jul 20th, 2006 8:59 PM
struct program skipping inputs codylee270 C 7 Dec 5th, 2005 8:02 AM
c struct : problems whit structs containing each other mevuorin C 10 Jul 7th, 2005 2:05 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:37 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC