Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4268166
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:59:54+00:00 2026-05-21T06:59:54+00:00

Please consider this class: class A { public: //public in this example string a1;

  • 0

Please consider this class:

class A
{
public:       //public in this example
  string a1;
  string a2;
  string a3;
  string a4;
  int a5;
  double a6;
  (...) plus other 50 member names

  multiset<string> getAttrib(const vector(A)& va, string attr)
  {
    for (vector<string>::const_iterator ci = va.begin; ci != va.end(); ++ci) {
      cout << ci->attr << endl;     //ERROR
  }
};

The member function could be called like:

A a
a.getAttrib(vector_a, "a1");

This results in an error. Const class A has no member named 'attr'.

I didn’t want to write 50 different case statements to achieve the above wanted flexibility.
Is it possible somehow?
And if member names are private can a public member function exist to perform this?

Besides class A, I have six other similar classes and wanted to have a parent class that would take a vector of any of the seven classes and could return the value (or in the real case a multiset) of the chosen membername.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:59:54+00:00Added an answer on May 21, 2026 at 6:59 am

    There’s not a direct way to do what you want in C++, although there are some reasonable attempts at reflection frameworks out there. You might also look here: How can I add reflection to a C++ application? for additional commentary about reflection. I would suggest, however, that instead of having 50 member variables, you simply use one private std::hashmap and use std::string keys. You can then easily pass string parameters along as keys for direct lookups.

    This, of course, assumes all your 50 members are of the same type, and that may not be suitable for your application. However, take my point, which is: do it a little differently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Please consider this -probably poorly written- example : class Command; class Command : public
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
Please pardon me for a n00bish question. Please consider the following code: public class
Please consider the following code: type TFoo1 = class public procedure DoSomething1; end; TFoo2
Please consider the following: class base{ base(); ~base(); }: class derived : public base{
Please clarify my doubt in Hashset. Consider the following code, class Person { String
This is a complex question, please consider carefully before answering. Consider this situation. Two
This is my test.cpp : #include <iostream.h> class C { public: C(); ~C(); };

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.