Connecting Tech Pros Worldwide Forums | Help | Site Map

initialization of static data member in header file

mathieu
Guest
 
Posts: n/a
#1: Feb 11 '08
Hi there

I am trying to provide a lookup from two 'int's into a char array,
something like this:

template <int g, int estruct Lookup;
template <struct Lookup<0,0{
// some typedef + enums definitions
static const char A;
};
const char Lookup<0,0>::A = "BLA";
// continue with Lookup<0,1; Lookup<0,2...

Since the initialization is done in the .h file, it gets included
multiple times, and at link time I am getting duplicate symbols.

Is there another way to initialize static data members ? Or could
someone let me know of any work around ?

Thanks for suggestion,
-Mathieu
Ps: Full source code is here:
http://gdcm.svn.sourceforge.net/view....h?view=markup

Victor Bazarov
Guest
 
Posts: n/a
#2: Feb 11 '08

re: initialization of static data member in header file


mathieu wrote:
Quote:
I am trying to provide a lookup from two 'int's into a char array,
something like this:
>
template <int g, int estruct Lookup;
template <struct Lookup<0,0{
// some typedef + enums definitions
static const char A;
};
const char Lookup<0,0>::A = "BLA";
You're trying to initialise an object of type 'char' with a character
array. That's impossible. A char array is not convertible to 'char'.
Quote:
// continue with Lookup<0,1; Lookup<0,2...
>
Since the initialization is done in the .h file, it gets included
multiple times, and at link time I am getting duplicate symbols.
>
Is there another way to initialize static data members ?
Another? Not sure what you mean. Whatever you posted is _not_
a way to initialise it.
Quote:
Or could
someone let me know of any work around ?
Define and initialise static data in a separate translation unit.
It's not a work-around. It's the only way to do it.
Quote:
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


mathieu
Guest
 
Posts: n/a
#3: Feb 11 '08

re: initialization of static data member in header file


On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Quote:
mathieu wrote:
Quote:
I am trying to provide a lookup from two 'int's into a char array,
something like this:
>
Quote:
template <int g, int estruct Lookup;
template <struct Lookup<0,0{
// some typedef + enums definitions
static const char A;
};
const char Lookup<0,0>::A = "BLA";
>
You're trying to initialise an object of type 'char' with a character
array. That's impossible. A char array is not convertible to 'char'.
Sorry I meant:

....
static const char A[];
....
const char Lookup<0,0>::A[] = "BLA";

Quote:
Define and initialise static data in a separate translation unit.
It's not a work-around. It's the only way to do it.
Ok. This might be naive, but I was able to do it with typdef and enum,
I tought I could do the same with a char array...
After all I might have to require user of my lib to link to it,
instead of just including it.

-Mathieu
Victor Bazarov
Guest
 
Posts: n/a
#4: Feb 11 '08

re: initialization of static data member in header file


mathieu wrote:
Quote:
On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Quote:
>mathieu wrote:
Quote:
>> I am trying to provide a lookup from two 'int's into a char array,
>>something like this:
>>
Quote:
>>template <int g, int estruct Lookup;
>>template <struct Lookup<0,0{
>> // some typedef + enums definitions
>> static const char A;
>>};
>>const char Lookup<0,0>::A = "BLA";
>>
>You're trying to initialise an object of type 'char' with a character
>array. That's impossible. A char array is not convertible to
>'char'.
>
Sorry I meant:
>
...
static const char A[];
...
const char Lookup<0,0>::A[] = "BLA";
>
>
Quote:
>Define and initialise static data in a separate translation unit.
>It's not a work-around. It's the only way to do it.
>
Ok. This might be naive, but I was able to do it with typdef and enum,
Not sure what you mean here. Any code sample?
Quote:
I tought I could do the same with a char array...
After all I might have to require user of my lib to link to it,
instead of just including it.
Static data are special beasts. You can definitely forgo linking if
all you have is code and it's all in templates.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


mathieu
Guest
 
Posts: n/a
#5: Feb 11 '08

re: initialization of static data member in header file


On Feb 11, 4:08 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Quote:
mathieu wrote:
Quote:
On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Quote:
mathieu wrote:
> I am trying to provide a lookup from two 'int's into a char array,
>something like this:
>
Quote:
Quote:
>template <int g, int estruct Lookup;
>template <struct Lookup<0,0{
> // some typedef + enums definitions
> static const char A;
>};
>const char Lookup<0,0>::A = "BLA";
>
Quote:
Quote:
You're trying to initialise an object of type 'char' with a character
array. That's impossible. A char array is not convertible to
'char'.
>
Quote:
Sorry I meant:
>
Quote:
...
static const char A[];
...
const char Lookup<0,0>::A[] = "BLA";
>
Quote:
Quote:
Define and initialise static data in a separate translation unit.
It's not a work-around. It's the only way to do it.
>
Quote:
Ok. This might be naive, but I was able to do it with typdef and enum,
>
Not sure what you mean here. Any code sample?
Thanks Victor for taking the time to try to help me out :)
The code is exactly:

http://gdcm.svn.sourceforge.net/view....h?view=markup

Again I'll use the notion of lookup. From a key (a pair of two
uint16_t) I need to provide extra information. Think of it as a yellow
page, from an integer I need to provide a char array (the name).

template <int Numberstruct YellowPage;
template <struct YellowPage<1234567890{
enum { Age = 33 };
static const char Name[];
void Print(std::ostream &os) { os << Name << " is " << Age << " old
\n"; }
};
const char YellowPage<1234567890>::Name[] = "John Doe";

int main()
{
YellowPage<1234567890jd;
jd.Print( std::cout );
return 0;
}

Actually I do even need to expose the 'Name', all I need is really the
'Print' function (to an ostream).
Quote:
Quote:
I tought I could do the same with a char array...
After all I might have to require user of my lib to link to it,
instead of just including it.
>
Static data are special beasts. You can definitely forgo linking if
all you have is code and it's all in templates.
From your last post, I understood that there is no way to initialize
data, enum were just a special case. Maybe with some ifdef blockers
magic, I should be able to include only once the initilization part...

Thanks
-Mathieu
mathieu
Guest
 
Posts: n/a
#6: Feb 11 '08

re: initialization of static data member in header file


On Feb 11, 4:29 pm, mathieu <mathieu.malate...@gmail.comwrote:
Quote:
On Feb 11, 4:08 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>
>
>
Quote:
mathieu wrote:
Quote:
On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>mathieu wrote:
>> I am trying to provide a lookup from two 'int's into a char array,
>>something like this:
>
Quote:
Quote:
>>template <int g, int estruct Lookup;
>>template <struct Lookup<0,0{
>> // some typedef + enums definitions
>> static const char A;
>>};
>>const char Lookup<0,0>::A = "BLA";
>
Quote:
Quote:
>You're trying to initialise an object of type 'char' with a character
>array. That's impossible. A char array is not convertible to
>'char'.
>
Quote:
Quote:
Sorry I meant:
>
Quote:
Quote:
...
static const char A[];
...
const char Lookup<0,0>::A[] = "BLA";
>
Quote:
Quote:
>Define and initialise static data in a separate translation unit.
>It's not a work-around. It's the only way to do it.
>
Quote:
Quote:
Ok. This might be naive, but I was able to do it with typdef and enum,
>
Quote:
Not sure what you mean here. Any code sample?
>
Thanks Victor for taking the time to try to help me out :)
The code is exactly:
>
http://gdcm.svn.sourceforge.net/view...e/DataDictiona...
>
Again I'll use the notion of lookup. From a key (a pair of two
uint16_t) I need to provide extra information. Think of it as a yellow
page, from an integer I need to provide a char array (the name).
>
template <int Numberstruct YellowPage;
template <struct YellowPage<1234567890{
enum { Age = 33 };
static const char Name[];
void Print(std::ostream &os) { os << Name << " is " << Age << " old
\n"; }};
>
const char YellowPage<1234567890>::Name[] = "John Doe";
>
int main()
{
YellowPage<1234567890jd;
jd.Print( std::cout );
return 0;
>
}
How about simply:

template <int Numberstruct YellowPage;
template <struct YellowPage<1234567890{
enum { Age = 33 };
const char *GetName() const {
static const char Name[] = "John Doe";
return Name;
}
void Print(std::ostream &os) { os << GetName() << " is " << Age << "
years old\n"; }
};

that should do it.

Thanks all, and sorry for the noise
-Mathieu
mathieu
Guest
 
Posts: n/a
#7: Feb 11 '08

re: initialization of static data member in header file


On Feb 11, 4:51 pm, mathieu <mathieu.malate...@gmail.comwrote:
Quote:
On Feb 11, 4:29 pm, mathieu <mathieu.malate...@gmail.comwrote:
>
>
>
Quote:
On Feb 11, 4:08 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>
Quote:
Quote:
mathieu wrote:
On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
> I am trying to provide a lookup from two 'int's into a char array,
>something like this:
>
Quote:
Quote:
>template <int g, int estruct Lookup;
>template <struct Lookup<0,0{
> // some typedef + enums definitions
> static const char A;
>};
>const char Lookup<0,0>::A = "BLA";
>
Quote:
Quote:
You're trying to initialise an object of type 'char' with a character
array. That's impossible. A char array is not convertible to
'char'.
>
Quote:
Quote:
Sorry I meant:
>
Quote:
Quote:
...
static const char A[];
...
const char Lookup<0,0>::A[] = "BLA";
>
Quote:
Quote:
Define and initialise static data in a separate translation unit.
It's not a work-around. It's the only way to do it.
>
Quote:
Quote:
Ok. This might be naive, but I was able to do it with typdef and enum,
>
Quote:
Quote:
Not sure what you mean here. Any code sample?
>
Quote:
Thanks Victor for taking the time to try to help me out :)
The code is exactly:
>>
Quote:
Again I'll use the notion of lookup. From a key (a pair of two
uint16_t) I need to provide extra information. Think of it as a yellow
page, from an integer I need to provide a char array (the name).
>
Quote:
template <int Numberstruct YellowPage;
template <struct YellowPage<1234567890{
enum { Age = 33 };
static const char Name[];
void Print(std::ostream &os) { os << Name << " is " << Age << " old
\n"; }};
>
Quote:
const char YellowPage<1234567890>::Name[] = "John Doe";
>
Quote:
int main()
{
YellowPage<1234567890jd;
jd.Print( std::cout );
return 0;
>
Quote:
}
>
How about simply:
>
template <int Numberstruct YellowPage;
template <struct YellowPage<1234567890{
enum { Age = 33 };
const char *GetName() const {
static const char Name[] = "John Doe";
return Name;
}
void Print(std::ostream &os) { os << GetName() << " is " << Age << "
years old\n"; }
>
};
>
that should do it.
>
Thanks all, and sorry for the noise
-Mathieu
Afterall I needed this one:

static const char *GetName() { return "John Doe"; }

-M
Closed Thread