P
Paul Brettschneider
Hello,
I have a global static array of structs and want to access a given
element using an identifier. I don't want to use the element subscript,
because it will change if I insert elements before the element I
want to access. In assembler I would simply add a label in front
of the element, but this doesn't work in C++.
The following programm snippet might explain what I want to do:
#include <iostream>
class A {
public:
const char *s;
};
static A array[] = {
{ "a" },
{ "b" },
// I wish I could just define a label to a given element like this,
// but this syntax is already used for designated inits (at least in gcc):
// element_c:
{ "c" },
{ "d" },
{ "e" },
{ "f" },
{ "g" },
};
int main()
{
// Access element "c"
std::cout << array[2].s << std::endl;
// Now I wish I could write it this:
// std::cout << element_c.s << std::endl;
// Or maybe:
// std::cout << array::element_c.s << std::endl;
return 0;
}
Is there a way to do this in C++ (maybe some preprocessor tricks)?
Thank you.
PS:
This is my first post to usenet, so bear with me.
I have a global static array of structs and want to access a given
element using an identifier. I don't want to use the element subscript,
because it will change if I insert elements before the element I
want to access. In assembler I would simply add a label in front
of the element, but this doesn't work in C++.
The following programm snippet might explain what I want to do:
#include <iostream>
class A {
public:
const char *s;
};
static A array[] = {
{ "a" },
{ "b" },
// I wish I could just define a label to a given element like this,
// but this syntax is already used for designated inits (at least in gcc):
// element_c:
{ "c" },
{ "d" },
{ "e" },
{ "f" },
{ "g" },
};
int main()
{
// Access element "c"
std::cout << array[2].s << std::endl;
// Now I wish I could write it this:
// std::cout << element_c.s << std::endl;
// Or maybe:
// std::cout << array::element_c.s << std::endl;
return 0;
}
Is there a way to do this in C++ (maybe some preprocessor tricks)?
Thank you.
PS:
This is my first post to usenet, so bear with me.