473,409 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

When to use a namespace

Consider

// file fft_data.h
class FFT_DATA
{
public:

static const unsigned int Data_Words = 10;
struct Data_Block_Type
{
unsigned short word[ Data_Words ];
unsigned short checksum;
bool valid;
};
// 4 more structs and thats it for fft_data
};

later

# include "fft_data.h"
class FP_SP_INTERFACE
{
public:

FFT_DATA::Data_Block_Type Do_A_Special_Thing();
};

Would a namespace be more appropriate here? If the answer depends,
then I'm confused on what.
It also appears 'odd' to me to have a class comprised SOLEY of
composite types and member data.
Thanks for your time.
Jul 22 '05 #1
8 1505
ma740988 wrote:
Consider

// file fft_data.h
class FFT_DATA
{
public:

static const unsigned int Data_Words = 10;
struct Data_Block_Type
{
unsigned short word[ Data_Words ];
unsigned short checksum;
bool valid;
};
// 4 more structs and thats it for fft_data
Doesn't seem to have any *data*, does it?
};

later

# include "fft_data.h"
class FP_SP_INTERFACE
{
public:

FFT_DATA::Data_Block_Type Do_A_Special_Thing();
Shouldn't that be 'static' or does it use some data members that you
decided to omit?
};

Would a namespace be more appropriate here? If the answer depends,
then I'm confused on what.
More appropriate for what? You can't create instances of namespaces.
You didn't explain at all whether you want to do that. You can't
inherit from a namespace either.
It also appears 'odd' to me to have a class comprised SOLEY of
composite types and member data.


What member data? Did you mean to say "static data"?

And, yes, if a struct has data and no member functions, it's OK.

Victor
Jul 22 '05 #2
"ma740988" <ma******@pegasus.cc.ucf.edu> wrote in message
news:a5*************************@posting.google.co m...
Consider

// file fft_data.h
class FFT_DATA
{
public:

static const unsigned int Data_Words = 10;
struct Data_Block_Type
{
unsigned short word[ Data_Words ];
unsigned short checksum;
bool valid;
};
// 4 more structs and thats it for fft_data
};

later

# include "fft_data.h"
class FP_SP_INTERFACE
{
public:

FFT_DATA::Data_Block_Type Do_A_Special_Thing();
};

Would a namespace be more appropriate here? If the answer depends,
then I'm confused on what.
It also appears 'odd' to me to have a class comprised SOLEY of
composite types and member data.


If you'd never create an instance of FFT_DATA and everything contained
inside of it is "open to the public", then it should probably be a namespace
instead of a class. However, in practice, there isn't much harm done in
leaving it as a struct. Sometimes you do need a struct instead of a
namespace. For instance, class templates sometimes delegate some work to
static members of a configurable type, and in that case, you can't use a
namespace.

--
David Hilsee
Jul 22 '05 #3
Victor Bazarov <v.********@comAcast.net> wrote in message news:<hb***************@newsread1.dllstx09.us.to.v erio.net>...
ma740988 wrote: [snip]
};

later

# include "fft_data.h"
class FP_SP_INTERFACE
{
public:

FFT_DATA::Data_Block_Type Do_A_Special_Thing();
Shouldn't that be 'static' or does it use some data members that you
decided to omit?

I didn't omit any member data and I'm not sure of what leads you to
believe that the returned type should be static.
};

Would a namespace be more appropriate here? If the answer depends,
then I'm confused on what.


More appropriate for what? You can't create instances of namespaces.
You didn't explain at all whether you want to do that. You can't
inherit from a namespace either.
It also appears 'odd' to me to have a class comprised SOLEY of
composite types and member data.


What member data? Did you mean to say "static data"?


I suspect I erred in referring to Data_Words as 'member data' since
you seem to ramble on, on a continuum about data.

I chose the wrong class to use as my example since it only contained '
static data'.
And, yes, if a struct has data and no member functions, it's OK.

I suspect the same can be said for the 'class' - per my initial
example - since struct/class are somewhat interchangable.
Jul 22 '05 #4
ma740988 wrote:
Victor Bazarov <v.********@comAcast.net> wrote in message news:<hb***************@newsread1.dllstx09.us.to.v erio.net>...
ma740988 wrote:
[snip]

};

later

# include "fft_data.h"
class FP_SP_INTERFACE
{
public:

FFT_DATA::Data_Block_Type Do_A_Special_Thing();


Shouldn't that be 'static' or does it use some data members that you
decided to omit?


I didn't omit any member data and I'm not sure of what leads you to
believe that the returned type should be static.


I am not sure what leads you to believe that specifying a member function
'static' has anything to do with its return type. Could it be you have
no idea what 'static' does to a member function?

};

Would a namespace be more appropriate here? If the answer depends,
then I'm confused on what.
More appropriate for what? You can't create instances of namespaces.
You didn't explain at all whether you want to do that. You can't
inherit from a namespace either.

It also appears 'odd' to me to have a class comprised SOLEY of
composite types and member data.


What member data? Did you mean to say "static data"?

I suspect I erred in referring to Data_Words as 'member data' since
you seem to ramble on, on a continuum about data.


I am not sure I understand the sentence above. Do you by any chance
understand the importance of "member data"? Why do we have member data
in classes at all? Do you know the difference between static and non-
static data members? If you answered "no" to any of these questions,
take a course on C++, or at least get back to your favourite C++ book.
[..]

Jul 22 '05 #5
Victor Bazarov <v.********@comAcast.net> wrote in message news:<yx***************@newsread1.dllstx09.us.to.v erio.net>...
ma740988 wrote:
Victor Bazarov <v.********@comAcast.net> wrote in message news:<hb***************@newsread1.dllstx09.us.to.v erio.net>...
[...]
I am not sure what leads you to believe that specifying a member function
'static' has anything to do with its return type. Could it be you have
no idea what 'static' does to a member function?
This has absolutely nothing with my understanding of static member
function but more importantly what YOU initially wrote. i.e...

[V Bazarov]Shouldn't that be 'static' or does it use some data members that you
decided to omit? [/V Bazarov]

Perhaps I should have asked for clarification. i.e. Shouldn't 'that'?
'that' what?

My interpretation per your question was as follows:

class FFT_DATA
{
public:
static Data_Block_Type // Note: I added 'static' to Data_Block_Type
{
};
};

// later
class FP_SP_INTERFACE
{
public:
FFT_DATA::Data_Block Do_A_Special_Thing();
};

Alternatively, your questions could have been interpreted as:
static FFT_DATA::Data_Block_Type Do_A_Special_Thing();
Do_A_Special_Thing should be a static member function. IOW add static
in front of the return type. I chose the former interpretation.

[...]
I am not sure I understand the sentence above. Do you by any chance
understand the importance of "member data"? Why do we have member data
in classes at all? Do you know the difference between static and non-
static data members? If you answered "no" to any of these questions,
take a course on C++, or at least get back to your favourite C++ book.


Let me re-iterate again. This has absolutely NOTHING to do with my
understanding of the importance of member data among other things.

You rambled on about 'data' in your initial post. Of interest, lets
re-visit two of your initial questions.

1) Doesn't seem to have any *data*, does it?
2) What member data? Did you mean to say "static data"?
In my initial post I used the term 'member data' - on a 'broader
scale' - in an attempt to understand the impetus behind (more of a
design question) having a CLASS with just member data (static and non
static) and composite types? IOW. Why not a namespace.
Unfortunately FFT_DATA contained no 'non static member data' and I
erred in referring to Data_Words as member data as opposed to static
member data.

Perhaps FFT_DATA would have been more appropriate for you if I had.

class FFT_DATA
{
public:
int Idx; // member data
static int Jdx; // static member data
};
In the end I don't need your recommendations with regard to a course
or resorting to my favorite book on C++. At the graduate level, my
program of study may not entail C++, but after an undergraduate course
and reading texts, I'm comfortable enough with various aspects of the
language. I might not be able to quote the standard and am clueless
when it comes to libraries such as 'boost' (rumor has it, boost might
be included with the next 'revision') but I feel - linguistics aside
- it's important for me - at this point to know/use the right 'tools'
(especially those algorithms) for the job.

Indeed, finding and using the right tools for the job is oft easier
said that done, especially when working in 'teams': Just ask 'John
Doe' :
1) Why he used an array as opposed to std::vect
2) Why muck with char* when you have std::string.

Occassionally this turns into an arm wrestling match with plenty of
verbiage about 'what's better' etc. etc.

By the way. David Hilsee provided a detailed explanation which
answered my question. That said, we could move on.

Thanks for you time.
[..]

Jul 22 '05 #6
David Hilsee wrote:
"ma740988" <ma******@pegasus.cc.ucf.edu> wrote in message
news:a5*************************@posting.google.co m...
Consider

// file fft_data.h
class FFT_DATA
{
public:

static const unsigned int Data_Words = 10;
struct Data_Block_Type
{
unsigned short word[ Data_Words ];
unsigned short checksum;
bool valid;
};
// 4 more structs and thats it for fft_data
};

later

# include "fft_data.h"
class FP_SP_INTERFACE
{
public:

FFT_DATA::Data_Block_Type Do_A_Special_Thing();
};

Would a namespace be more appropriate here? If the answer depends,
then I'm confused on what.
It also appears 'odd' to me to have a class comprised SOLEY of
composite types and member data.

If you'd never create an instance of FFT_DATA and everything contained
inside of it is "open to the public", then it should probably be a namespace
instead of a class. However, in practice, there isn't much harm done in
leaving it as a struct. Sometimes you do need a struct instead of a
namespace. For instance, class templates sometimes delegate some work to
static members of a configurable type, and in that case, you can't use a
namespace.


To me, a namespace should be used if the visibility of some entities
need to be confined to a group of classes, or a group of functions. In
the example above, the structs could have just as well been defined in
class FP_SP_INTERFACE, couldn't they?

If there were several classes that would like to share the definition of
the structs, as well as the static variable, then yes, they should all
go into a namespace.
Jul 22 '05 #7
"Dave Rahardja" <as*@me.com> wrote in message
news:rS*******************@twister.rdc-kc.rr.com...
David Hilsee wrote:
"ma740988" <ma******@pegasus.cc.ucf.edu> wrote in message
news:a5*************************@posting.google.co m...
Consider

// file fft_data.h
class FFT_DATA
{
public:

static const unsigned int Data_Words = 10;
struct Data_Block_Type
{
unsigned short word[ Data_Words ];
unsigned short checksum;
bool valid;
};
// 4 more structs and thats it for fft_data
};

later

# include "fft_data.h"
class FP_SP_INTERFACE
{
public:

FFT_DATA::Data_Block_Type Do_A_Special_Thing();
};

Would a namespace be more appropriate here? If the answer depends,
then I'm confused on what.
It also appears 'odd' to me to have a class comprised SOLEY of
composite types and member data.

If you'd never create an instance of FFT_DATA and everything contained
inside of it is "open to the public", then it should probably be a namespace instead of a class. However, in practice, there isn't much harm done in
leaving it as a struct. Sometimes you do need a struct instead of a
namespace. For instance, class templates sometimes delegate some work to static members of a configurable type, and in that case, you can't use a
namespace.


To me, a namespace should be used if the visibility of some entities
need to be confined to a group of classes, or a group of functions. In
the example above, the structs could have just as well been defined in
class FP_SP_INTERFACE, couldn't they?

If there were several classes that would like to share the definition of
the structs, as well as the static variable, then yes, they should all
go into a namespace.


I don't think I understand your comment about visibility. To me, a
namespace is usually not used to confine visibility. Namespaces are
generally used as a logical grouping of functionality. Namespaces are "wide
open" when it comes to visibility. That is why I added that bit about
everything being "open to the public."

I agree that the structs could have been declared inside FP_SP_INTERFACE,
but I thought that ma740988 was asking if FFT_DATA should be a namespace
instead of a class. From the little bit of code that was provided, it
seemed to me that FFT_DATA was not being used to define a type of which
instances would be used. Instead, it was being used to group some common
functionality. For that usage, it sounded like a namespace was more
appropriate. Did I misunderstand you?

--
David Hilsee
Jul 22 '05 #8
"David Hilsee" <da*************@yahoo.com> writes:

[...]
I don't think I understand your comment about visibility. To me, a
namespace is usually not used to confine visibility. Namespaces are
generally used as a logical grouping of functionality. Namespaces are "wide
open" when it comes to visibility. That is why I added that bit about
everything being "open to the public."


The general prupose of namespaces is to avoid nameclashes. The
properties of structuring code is a kind "side effect" :-)

Kind regards,
Nicolas

--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joseph A Romeo | last post by:
I have written an XSLT transformation on an ASP.NET page. The resulting HTML is primarily a table of links. I have found that when the resulting HTML is less than or equal to 16040 bytes, the...
0
by: Joseph A Romeo | last post by:
I have written an XSLT transformation on an ASP.NET page. The resulting HTML is primarily a table of links. I have found that when the resulting HTML is less than or equal to 16040 bytes, the...
2
by: ree32 | last post by:
When I import an xml document in Visual studio and Genereate as schema from it, and create a dataset from it, it adds this line into to the root element of my xml file -...
4
by: Sebastien Tardif | last post by:
Subject: XmlSerializer.Deserialize complain when root declare the namespace If I do XmlSerializer.Deserialize( myString ) and myString is: String myString = "<?xml version=\"1.0\"...
4
by: Flare | last post by:
OK. I'll try explain my problem so simple as possible. I have to send a complex data type to a WebService from a Asp.net webapplication. My Data type look like this. (A class with a porperty)...
19
by: Tiraman | last post by:
Hi , I have an assembly that hold few class and few imports which are not used in all of the class's . so my question is when to use the "Imports XXX" And when to use this kind of statement...
2
by: Jeff Brown | last post by:
Hi, I suspect that this isn't possible, but I figured I'd ask. My project has a root namespace, let's say it's "Root", that applies to almost every source file (which is why I've set it as the...
3
by: Jerome Cohen | last post by:
AI am trying to call a third-party web service. this service expects an XML fragment that contains the request plus other parameter. adding the web reference created the syntax below(reference.vb)....
0
by: selvialagar | last post by:
i did a program in vc++.net for ICMP communication using sockets.But when i execute it i received a very slow response...is there any configuration to be made on the server..or any other thing?.. i...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.