Connecting Tech Pros Worldwide Help | Site Map

Can I put this in a .h file?

  #1  
Old January 25th, 2006, 01:05 PM
jessica_boxer@yahoo.com
Guest
 
Posts: n/a
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, 01:15 PM
Alf P. Steinbach
Guest
 
Posts: n/a

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, 01:35 PM
Alf P. Steinbach
Guest
 
Posts: n/a

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, 02:45 PM
Gernot Frisch
Guest
 
Posts: n/a

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, 05:25 PM
red floyd
Guest
 
Posts: n/a

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.


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can somebody put this VB code into c#? Sunfire answers 5 November 14th, 2007 05:05 PM
EXTRACT AND LOAD UTILITY IN FILE AID FOR DB2 mraghu83@gmail.com answers 0 March 1st, 2007 05:25 PM
In file parsing, taking the first few characters of a text file after a readfile or streamreader file read... .Net Sports answers 11 January 17th, 2006 12:55 AM
store image in DB or in file clark answers 4 November 17th, 2005 07:44 PM