I think it is possible to do this by calculating memory offsets and such but it will be a bit hackish. Maybe you can try to avoid it. You could have last and first names in an array. That way you can use an index to decide if it should be first name or last name.
"I think it is possible to do this by calculating memory offsets and such but it will be a bit hackish."
Not to mention that alignment is compiler-specific. Have you tried using a pointer-to-a-member? It's probably one of the safest ways to do what you want:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
struct STRUCT
{
std::string LastName;
std::string FirstName;
};
typedef std::string(STRUCT:: *PtrToLastName);
int main()
{
STRUCT Struct[3];
PtrToLastName LastName(&STRUCT::LastName);
for (int i = 0; i <= 2; i++)
std::cout << Struct[i].*LastName << std::endl;
return 0;
}
...or am I missing something?
Edit:
Note that a pointer-to-a-member isn't bound to 1 particular instance of STRUCT: