0

I have the following use case,

struct Trans {
         double price;
};
struct Quote {
    double bid_price_1;
    double ask_price_1;
};
template<typename DataT, double DataT::*ptr>
class Return {
public:
    void operator()(DataT& data) {
        // print out the right field of the right class
        //std::cout << &DataT::ptr << std::endl;
    }
};
int main() {
    Quote quote {1, 2};
    Return<Quote, &Quote::bid_price_1> ret;
    ret(quote);
}

In the operator of Return class, I would like to be able to reference the right member variable of the right class, I have read the following post (Pointer to class member as template parameter), but it does not seem to mention how to retrieve the member variable part. I would like to know what is the right syntax to retrieve the member variable specified at compile time.

tesla1060
  • 2,621
  • 6
  • 31
  • 43

1 Answers1

1

The syntax to address a member uses the operators .* (for a reference) and ->* (for a pointer). In your case

void operator()(DataT& data) {
    std::cout << data.*ptr << std::endl;
}
arayq2
  • 2,502
  • 17
  • 21