#include <iostream>
#include <string>
using namespace std;
struct record {
	string	C0;
	string	C1;
	string	C2;
};
int main()
{
	record  param;
	param.C0 = "heading1";
	param.C1 = "heading2";
	param.C2 = "heading3";
	string key = "CO";
	string data = param.C0;
	cout << "DATA: " << data << endl; 
	return 1;
}

Is it possible to replace 'C0' with the variable 'key'?
ie:
string data = param.??key??; // where key is interpolated to 'C0'.

Dani AI

Generated

The short answer: C++ does not support turning a runtime string like "C0" into a direct member access expression (so 's one-word "No." is correct). Member names are part of the type and fixed at compile time. Common solutions are to change the data model so the desired field can be selected at runtime.

As mentioned, two standard patterns are (1) store values in an indexed container inside the struct (std::array or std::vector) and convert the key to an index, or (2) store values in an associative container keyed by string (std::map or std::unordered_map). The indexed approach is compact and fast but needs a numeric index; the associative approach accepts string keys directly but adds runtime overhead and loses compile-time field checks.

A third pattern keeps named members but exposes a runtime name->member mapping via pointer-to-member. It preserves the original struct layout and type-safety while allowing string lookups through a small dispatch table:

struct Record { std::string C0, C1, C2; };

Record obj;
obj.C0 = "heading1"; obj.C1 = "heading2"; obj.C2 = "heading3";

static const std::pair<const char*, std::string Record::*> table[] = {
    { "C0", &Record::C0 },
    { "C1", &Record::C1 },
    { "C2", &Record::C2 },
};

std::string key = "C1";
std::string data;
for (auto &entry : table) {
    if (key == entry.first) { data = obj.*(entry.second); break; }
}

Notes: the pointer-to-member table must be kept in sync with the struct; for larger or truly dynamic sets prefer an associative container (std::unordered_map for best average lookup speed in modern C++). For small, fixed field sets the pointer-to-member approach is a tidy, type-safe compromise.

Recommended Answers

All 3 Replies

No.

Like

#include <iostream>
#include <string>
using namespace std;
struct record {
	string	C[3];
};
int main()
{
	record  param;
	param.C[0] = "heading1";
	param.C[1] = "heading2";
	param.C[2] = "heading3";
	int key = 0;
	string data = param.C[key];
	cout << "DATA: " << data << endl; 
	return 0;  // zero is success
}

Or maybe

#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
  map<string,string> param;
  param["C0"] = "heading1";
  param["C1"] = "heading2";
  param["C2"] = "heading3";
  string key = "C0";
  string data = param[key];
  cout << "DATA: " << data << endl;
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.