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.
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.