Connecting Tech Pros Worldwide Help | Site Map

Can I put this in a .h file?

 
LinkBack Thread Tools Search this Thread
  #1  
Old January 25th, 2006, 12:05 PM
jessica_boxer@yahoo.com
Guest
 
Posts: n/a
Default Can I put this in a .h file?

I want a standard list of strings, which I want to implement in a
header as:

// Approach 1 in .h
namespace StdNames
{
const char* name1 = "Name1";
const char* name2 = "Name2";
// etc
}

If I put this in the header will it allocate space for name1 and name2
everywhere I include the header (meaning I should have a corresponding
cpp file that actually does the assignments, and declars the above as
extern) or does it treat these as logical constants? Is Approach 1
above, or apporach 2 more appropriate?


// Approach 2 in .h
namespace StdNames
{
extern const char* name1;
extern const char* name2;
// etc
}

// And in the .cpp file
#include "StdNames.h"

namespace StdNames
{
const char* name1 = "Name1";
const char* name2 = "Name2";
// etc
}


  #2  
Old January 25th, 2006, 12:15 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: Can I put this in a .h file?

* jessica_boxer@yahoo.com:[color=blue]
> I want a standard list of strings, which I want to implement in a
> header as:
>
> // Approach 1 in .h
> namespace StdNames
> {
> const char* name1 = "Name1";
> const char* name2 = "Name2";
> // etc
> }
>
> If I put this in the header will it allocate space for name1 and name2
> everywhere I include the header[/color]

That's a quality of implementation issue.

There are workarounds where you want a guarantee, e.g., using
non-template code only,

namespace StdNames
{
inline std::vector<char const*> names()
{
static char const* values[] =
{
"Name1",
"Name2",
};
return std::vector<char const*>( values, values+2 )
}
}

where the literal constant 2 should of course be replaced by some
function or macro.

[color=blue]
> (meaning I should have a corresponding
> cpp file that actually does the assignments, and declars the above as
> extern) or does it treat these as logical constants? Is Approach 1
> above, or apporach 2 more appropriate?
>
>
> // Approach 2 in .h
> namespace StdNames
> {
> extern const char* name1;
> extern const char* name2;
> // etc
> }
>
> // And in the .cpp file
> #include "StdNames.h"
>
> namespace StdNames
> {
> const char* name1 = "Name1";
> const char* name2 = "Name2";
> // etc
> }[/color]

If you're happy to have a .cpp file then this is also perfectly OK.

One limitation is that you cannot then use such constants in compile
time expressions such as, for pointers, template instantiations.

However, the technique I sketched above does not solve that either, and
although a "simple" template solution exists the problem it solves seems
to not occur in practice... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  #3  
Old January 25th, 2006, 12:35 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: Can I put this in a .h file?

* Alf P. Steinbach:[color=blue]
> * jessica_boxer@yahoo.com:[color=green]
> > I want a standard list of strings, which I want to implement in a
> > header as:
> >
> > // Approach 1 in .h
> > namespace StdNames
> > {
> > const char* name1 = "Name1";
> > const char* name2 = "Name2";
> > // etc
> > }
> >
> > If I put this in the header will it allocate space for name1 and name2
> > everywhere I include the header[/color]
>
> That's a quality of implementation issue.[/color]

Sorry, forgot: IF (and I mean IF) what you meant was to have constants,
not variables, i.e.

char const *const name1 = "Name1";

which you can alternatively and better (preserving length information)
express as

char const name1[] = "Name1";

THEN it's a quality of implementation issue.

If, on the other hand, you really mean variables, then the answer is
that you're in violation of the "one definition rule" (ODR). That's
because while namespace scope constants have internal linkage as
default, namespace scope variables have external linkage as default.

In the rest of my posting, insert extra 'const''s as appropriate... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  #4  
Old January 25th, 2006, 01:45 PM
Gernot Frisch
Guest
 
Posts: n/a
Default Re: Can I put this in a .h file?

[color=blue]
> namespace StdNames
> {
> inline std::vector<char const*> names()
> {
> static char const* values[] =
> {
> "Name1",
> "Name2",
> };
> return std::vector<char const*>( values, values+2 )
> }
> }
>
> where the literal constant 2 should of course be replaced by some
> function or macro.[/color]

return std::vector<char const*>(values, sizeof(values)/sizeof(char
const*) );

e.g.


  #5  
Old January 25th, 2006, 04:25 PM
red floyd
Guest
 
Posts: n/a
Default Re: Can I put this in a .h file?

Gernot Frisch wrote:[color=blue][color=green]
>> namespace StdNames
>> {
>> inline std::vector<char const*> names()
>> {
>> static char const* values[] =
>> {
>> "Name1",
>> "Name2",
>> };
>> return std::vector<char const*>( values, values+2 )
>> }
>> }
>>
>> where the literal constant 2 should of course be replaced by some
>> function or macro.[/color]
>
> return std::vector<char const*>(values, sizeof(values)/sizeof(char
> const*) );[/color]

I'd write the whole thing as follows:

namespace StdNames
{
inline const std::vector<char const *>& names()
{
static char const* const values[] =
{
"Name1",
"Name2",
// ...
};
static const std::vector<char const*>
theVector(values, values+sizeof(values)/sizeof(values[0]));
return theVector;
}
}

You don't make multiple copies of the vector, you can't add values to
your standard names, and you can't change your names.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.