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 8774323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:32:11+00:00 2026-06-13T18:32:11+00:00

I am trying to implement limited reflection in C++, so that I could be

  • 0

I am trying to implement limited reflection in C++, so that I could be able to call getter and setter on the objects stored. Here is what I have done so far

Main Code

#ifndef REFLECTION_MANAGER_HPP_
#define REFLECTION_MANAGER_HPP_

#include <iostream>
#include <string>


template <class Owner, class IOType> 
class SingleProperty {
public:
  typedef IOType    (Owner::*get_func_t)(); 
  typedef void    (Owner::*set_func_t)( IOType Value );

  inline SingleProperty(get_func_t Getter, set_func_t Setter ): m_Getter(Getter), m_Setter(Setter) {
  }
  get_func_t        m_Getter;
  set_func_t        m_Setter;
};

class ReflectionManager {
public:
  static ReflectionManager& Instance() {
    static ReflectionManager instance;
    return instance;
  }

  template <class Owner, class IOType> 
  void RegisterProperty(  std::string class_name, 
                          std::string property_name, 
                          typename SingleProperty<Owner, IOType>::get_func_t GetFn, 
                          typename SingleProperty<Owner, IOType>::set_func_t SetFn)  {
    SingleProperty<Owner, IOType>* pProperty = new SingleProperty<Owner, IOType>(GetFn, SetFn );
    m_class_memeber_map[class_name][property_name] = pProperty;
  }

  template <class Owner> void put(std::string key, void *value, std::string class_name = NULL ) {
    Owner *ptr = reinterpret_cast<Owner*>(value);
    std::map<std::string, std::map< std::string, void*> >::iterator pos = m_class_memeber_map.find(class_name);
    if ( pos == m_class_memeber_map.end()) {
      return; // handle the error
    }
    for ( std::map< std::string, void*>::iterator itr = pos->second.begin(); itr != pos->second.end(); ++itr ) {
      SingleProperty<Owner,std::string> *ptr = (SingleProperty<Owner,std::string> *)itr->second;
      (ptr->*m_Getter)();
    }
  }
private:
  ReflectionManager() {
  }
  ReflectionManager(ReflectionManager const&); 
  void operator=(ReflectionManager const&);

  std::map<std::string, std::map< std::string, void*> > m_class_memeber_map;

};

#endif

Calling Code

This will be called as follows

ReflectionManager::Instance().RegisterProperty<Person, std::string>("Person", "m_name", &Person::GetName, &Person::SetName);
  ReflectionManager::Instance().RegisterProperty<Person, std::string>("Person", "m_dept", &Person::GetDept, &Person::SetDept);
  ReflectionManager::Instance().RegisterProperty<Person, int>("Person", "m_age", &Person::GetAge, &Person::SetAge);

  Person p1;
  p1.SetName("Avinash");
  p1.SetDept("Gemfire Native Client");
  p1.SetAge(34);

  ReflectionManager::Instance().put<Person>( "key1", &p1, "Person");

I have stored the getter and setter in the map, but in put function I am not able to call it since I donot know the type.

Problem

Basically How do i implement now the for loop for ( std::map< std::string, void*>::iterator itr = pos->second.begin(); itr != pos->second.end(); ++itr ) in which I want to iterate over each element and call the respective getter and setter methods.

I tried this but does not work.

(ptr->*((itr->second).m_Getter))();

Another Approach I found Would appreciate Comments

  template <class Owner, class IOType> 
  void RegisterProperty(  std::string class_name, 
    std::string property_name, 
    typename SingleProperty<Owner, IOType>::get_func_t GetFn, 
    typename SingleProperty<Owner, IOType>::set_func_t SetFn)  {

      SingleProperty<Owner, IOType>* pProperty = new SingleProperty<Owner, IOType>(GetFn, SetFn );
      m_class_memeber_map[class_name][property_name] = pProperty;
      m_property_type_map[class_name].push_back(std::make_pair(property_name, TypeName<IOType>::get()));

  }

  template <class Owner> void put(std::string key, void *value, std::string class_name = NULL ) {
    Owner *pOwner  = reinterpret_cast<Owner*>(value);
    std::map<std::string, std::map< std::string, void*> >::iterator pos = m_class_memeber_map.find(class_name);

    std::vector<std::pair<std::string, std::string> > vector_property_map = m_property_type_map.find(class_name)->second;
    for ( std::vector<std::pair<std::string, std::string> >::iterator itr = vector_property_map.begin();itr != vector_property_map.end(); ++itr ) {
        std::map< std::string, void*>::iterator pos_getter_setter_fn = pos->second.find(itr->first);
        if ( itr->second == "int" ) {
          SingleProperty<Owner,int> *ptr = (SingleProperty<Owner, int> *)pos_getter_setter_fn->second;
          (pOwner->*(ptr->m_Getter))();
        } else if ( itr->second == "string" ) {
          SingleProperty<Owner,std::string> *ptr = (SingleProperty<Owner, std::string> *)pos_getter_setter_fn->second;
          (pOwner->*(ptr->m_Getter))();
        } 
    }
  }
  • 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-06-13T18:32:12+00:00Added an answer on June 13, 2026 at 6:32 pm

    These pointers are pointers to function members of Owner type:

    get_func_t m_Getter;
    set_func_t m_Setter;
    

    Why do you try to call them giving SingleProperty<> pointer as object?

    Owner *ptr = reinterpret_cast<Owner*>(value);
    for ( std::map< std::string, void*>::iterator itr = pos->second.begin();
       itr != pos->second.end(); ++itr )
    {
      SingleProperty<Owner,std::string> *ptr = 
          (SingleProperty<Owner,std::string> *)itr->second;
      // Here is the flaw, you are trying to call m_Getter from SingleProperty object
      // pointer, but it must be Owner pointer!
      (ptr->*m_Getter)();
    }
    

    Also you have to properly pass type of property as template parameter IOType.
    I’m not sure what do you try to do with this function and what is the effect of calling getters with discarded return type, but from abstract syntax point of view it shall be like this:

    template <class Owner, class IOType> void put(const std::string& key, Owner* value,
        const std::string& class_name = "")
    {
        std::map<std::string, std::map<std::string, void*> >::iterator pos =
            m_class_memeber_map.find(class_name);
    
        if(pos == m_class_memeber_map.end())
        {
           return; // handle the error
        }
    
        for(std::map<std::string, void*>::iterator itr = pos->second.begin();
           itr != pos->second.end(); ++itr)
        {
    
            SingleProperty<Owner,IOType> *ptr = 
              static_cast<SingleProperty<Owner, IOType>*>(itr->second);
            (value->*(ptr->m_Getter))(); // return IOType discarded?!?!
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to implement a search similar to here .This searches properties based on city,locality,property
Im trying to implement a destructor for the objects of linked-list Iv created. I
I have been trying with limited success to code a JSF application. In one
SetFocus I'm trying implement the above Se Focus code in a Class Library that
I'm trying implement Data Annotation to my Linq to SQL objects. The .dbml file
I'm trying to implement the strategy pattern using reflection, i.e. instantiate a new Concrete
I'm trying to implement a requirement in our system that all strings be trimmed
I'm trying to implement HTTPS on my Node.js server (Expressjs framework). I have my
I'm trying to implement a program which runs a function limited by a fixed
I'm trying to create a collection class in Flex that is limited to housing

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.