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

  • SEARCH
  • Home
  • 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 6735673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:02:25+00:00 2026-05-26T11:02:25+00:00

I have a class that holds several vectors of objects: struct ComponentA { public:

  • 0

I have a class that holds several vectors of objects:

struct ComponentA
{
 public:
   methodA1();
   float data1;
   ...
};
struct ComponentB
{
   ...
};
struct ComponentC
{
   ...
};
struct ComponentD
{
   ...
};

class Assembly
{
    vector<ComponentA> As;
    vector<ComponentB> Bs;
    vector<ComponentC> Cs;
    vector<ComponentD> Ds;
};

I would like to use traits and define a function that can return reference to members like this:

template< int T >
struct ComponentTraits;

template<>
ComponentTraits<TYPEA>
{
    typedef vector<ComponentA> data_type;
}
....
template< int T >
ComponentTraits<T>::data_type getComp(const Assembly & myassy)
{
    ...
}

such that a call

getComp<TYPEA>(thisassy)

would return a reference to As so I can manipulate at the vector level and access each component object method and data:

getComp<TYPEA>(thisassy).push_back(newcomponentA);
getComp<TYPEA>(thisassy).back().methodA1();
getComp<TYPEA>(thisassy).front().data1 = 5.0;

Thanks,

Wada

  • 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-26T11:02:26+00:00Added an answer on May 26, 2026 at 11:02 am

    Traits aren’t going to do what you want here. C++ lacks the introspection support needed to say, “Get me the first member of class X that is of type Y.” Also, traits are intended for “tell me more about type X” kinds of operations.

    Template specialization can be used for this, but they’ll be a lot of work:

    template<class T>
    T& getComp<T>(Assembly& assy);
    
    template<>
    ComponentA& getComp<ComponentA>(Assembly& assy)
    { return assy.As;
    }
    
    template<>
    ComponentB& getComp<ComponentB>(Assembly& assy)
    { return assy.Bs;
    }
    
    template<>
    ComponentC& getComp<ComponentC>(Assembly& assy)
    { return assy.Cs;
    }
    
    template<>
    ComponentD& getComp<ComponentD>(Assembly& assy)
    { return assy.Ds;
    }
    

    Or shorter:

    template<class T>
    T& getComp<T>(Assembly& assy);
    
    #define SpecializeGetComp(T, field) template<> \
    T& getComp<T>(Assembly& assy) { return assy.field; }
    
    SpecializeGetComp(ComponentA, As)
    SpecializeGetComp(ComponentB, Bs)
    SpecializeGetComp(ComponentC, Cs)
    SpecializeGetComp(ComponentD, Ds)
    

    You may also want to read up on Typelists (Chapter 3) in Alexandrescu’s book Modern C++ Design. You might be able to use them in a Factory-like pattern, but that’s way more than an SO answer really fits.

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

Sidebar

Related Questions

I have a specialized list that holds items of type IThing : public class
I have written a class (AbcdBase) which holds several static objects, these include maps
If I have a class that holds one or several lists, is it better
I have a class Team that holds a generic list: [DataContract(Name = TeamDTO, IsReference
I have a private class variable that holds a collection of order items: Private
I have a class includes.vb that holds some variables (sharing them with other pages)
I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have a class in C++ that I can't modify. However, that class holds
I have a simple class that essentially just holds some values. I have overridden
I have this small class called City that simply holds some information about a

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.