如何使用 C++ ->() 运算符获取基类私有成员?这样我就可以访问派生?
how to use c++ ->() operator to get a baseclasses private member? so I can access derived?
我试图在通过基础class 工厂方法
接收派生的 class TMAConnection
后访问它
template <>
class cpool::ConnectionPoolFactory<TMAConnection> {
public:
static std::unique_ptr<cpool::ConnectionPool> create( const std::uint16_t num_connections, const char* tma_gw_host, const int tma_gw_port) {
std::vector< std::unique_ptr<cpool::Connection> > connections;
for ( std::uint16_t k = 0; k < num_connections; ++k ) {
// cannot use std::make_unique, because constructor is hidden
connections.emplace_back( std::unique_ptr<TMAConnection>( new TMAConnection{tma_gw_host, tma_gw_port} ) );
}
return std::unique_ptr<cpool::ConnectionPool>( new cpool::ConnectionPool{std::move( connections )} );
}
};
我的麻烦在于尝试访问派生的 classes 函数和成员.. 例如 TestCPPClient
成员或 querySymbols
函数...
class TMAConnection final : public cpool::Connection {
public:
TMAConnection(const char* hostname, const int port){
this->gateway_host=hostname;
this->gateway_port=port;
}
bool heart_beat() override { return connected; }
bool is_healthy() override { return connected; }
bool connect() override {
connected = true;
return connected;
}
void disconnect() override {
connected = false;
}
void querySymbols(std::vector<tmaapi::CD> & empty_contract_vect, std::vector<std::string> & queries_list) {
this->tma_client.query_matching_tickers(empty_contract_vect, queries_list);
std::vector<std::string> empty_vec;
}
TestCppClient tma_client;
private:
TMAConnection(){}
friend cpool::ConnectionPoolFactory<TMAConnection>;
bool connected = false;
const char* gateway_host;
int gateway_port;
uint16_t tma_client_id;
};
当我查看 ConnectionProxy 代码时.. 似乎 我可以使用运算符 ->
获取成员或派生 class 但无济于事到目前为止
https://github.com/malikkirchner/connection-pool/blob/master/src/pool.cpp#L61-L63
Connection* ConnectionPool::ConnectionProxy::operator->() { return m_connection; }
Connection& ConnectionPool::ConnectionProxy::operator*() { return *m_connection; }
这里是我尝试调用 create
以建立连接的地方..但随后无法获得派生的 class 指针
cpool::ConnectionPool::ConnectionProxy proxy_conn = this->tma_conn_pool->get_connection();
proxy_conn->is_healthy(); // this works fine if i don't cast.. since it's in the base class
proxy_conn->querySymbols(empty_contract_vect, sub_batch_vector); // this fails
我尝试添加动态转换.. 似乎不起作用
cpool::ConnectionPool::ConnectionProxy proxy_conn = this->tma_conn_pool->get_connection();
TMAConnection* x = dynamic_cast<TMAConnection*>(&proxy_conn));
x->is_healthy();
x->querySymbols(empty_contract_vect, sub_batch_vector);
/home/server/src/server.cpp:48:64: error: cannot dynamic_cast ‘& proxy_conn’ (of type ‘class cpool::ConnectionPool::ConnectionProxy*’) to type ‘class TMAConnection*’ (source type is not polymorphic)
48 | TMAConnection* x = dynamic_cast<TMAConnection*>(&proxy_conn));
如何获得指向派生 class 的指针?我的工厂错了吗?谢谢
编辑:我试过@typewriters 建议使用 auto.. 还是没用
auto proxy_conn = this->tma_conn_pool->get_connection();
TMAConnection* x = dynamic_cast<TMAConnection*>(&proxy_conn));
error: cannot dynamic_cast ‘& proxy_conn’ (of type ‘class cpool::ConnectionPool::ConnectionProxy*’) to type ‘class TMAConnection*’ (source type is not polymorphic)
ConnectionPool::get_connection()
returns 一个 ConnectionProxy
其中 不是 一个 Connection
直接对象,而是包含一个:
ConnectionProxy
允许您访问底层连接对象。
// create a pool
auto pool = ...
// get a ConnectionProxy
auto proxy = pool->get_connection();
// cast to your type
if(auto conn = dynamic_cast<TMAConnection*>(proxy.operator->()))
{
// success!
}
我试图在通过基础class 工厂方法
接收派生的 classTMAConnection
后访问它
template <>
class cpool::ConnectionPoolFactory<TMAConnection> {
public:
static std::unique_ptr<cpool::ConnectionPool> create( const std::uint16_t num_connections, const char* tma_gw_host, const int tma_gw_port) {
std::vector< std::unique_ptr<cpool::Connection> > connections;
for ( std::uint16_t k = 0; k < num_connections; ++k ) {
// cannot use std::make_unique, because constructor is hidden
connections.emplace_back( std::unique_ptr<TMAConnection>( new TMAConnection{tma_gw_host, tma_gw_port} ) );
}
return std::unique_ptr<cpool::ConnectionPool>( new cpool::ConnectionPool{std::move( connections )} );
}
};
我的麻烦在于尝试访问派生的 classes 函数和成员.. 例如 TestCPPClient
成员或 querySymbols
函数...
class TMAConnection final : public cpool::Connection {
public:
TMAConnection(const char* hostname, const int port){
this->gateway_host=hostname;
this->gateway_port=port;
}
bool heart_beat() override { return connected; }
bool is_healthy() override { return connected; }
bool connect() override {
connected = true;
return connected;
}
void disconnect() override {
connected = false;
}
void querySymbols(std::vector<tmaapi::CD> & empty_contract_vect, std::vector<std::string> & queries_list) {
this->tma_client.query_matching_tickers(empty_contract_vect, queries_list);
std::vector<std::string> empty_vec;
}
TestCppClient tma_client;
private:
TMAConnection(){}
friend cpool::ConnectionPoolFactory<TMAConnection>;
bool connected = false;
const char* gateway_host;
int gateway_port;
uint16_t tma_client_id;
};
当我查看 ConnectionProxy 代码时.. 似乎 我可以使用运算符 ->
获取成员或派生 class 但无济于事到目前为止
https://github.com/malikkirchner/connection-pool/blob/master/src/pool.cpp#L61-L63
Connection* ConnectionPool::ConnectionProxy::operator->() { return m_connection; }
Connection& ConnectionPool::ConnectionProxy::operator*() { return *m_connection; }
这里是我尝试调用 create
以建立连接的地方..但随后无法获得派生的 class 指针
cpool::ConnectionPool::ConnectionProxy proxy_conn = this->tma_conn_pool->get_connection();
proxy_conn->is_healthy(); // this works fine if i don't cast.. since it's in the base class
proxy_conn->querySymbols(empty_contract_vect, sub_batch_vector); // this fails
我尝试添加动态转换.. 似乎不起作用
cpool::ConnectionPool::ConnectionProxy proxy_conn = this->tma_conn_pool->get_connection();
TMAConnection* x = dynamic_cast<TMAConnection*>(&proxy_conn));
x->is_healthy();
x->querySymbols(empty_contract_vect, sub_batch_vector);
/home/server/src/server.cpp:48:64: error: cannot dynamic_cast ‘& proxy_conn’ (of type ‘class cpool::ConnectionPool::ConnectionProxy*’) to type ‘class TMAConnection*’ (source type is not polymorphic)
48 | TMAConnection* x = dynamic_cast<TMAConnection*>(&proxy_conn));
如何获得指向派生 class 的指针?我的工厂错了吗?谢谢
编辑:我试过@typewriters 建议使用 auto.. 还是没用
auto proxy_conn = this->tma_conn_pool->get_connection();
TMAConnection* x = dynamic_cast<TMAConnection*>(&proxy_conn));
error: cannot dynamic_cast ‘& proxy_conn’ (of type ‘class cpool::ConnectionPool::ConnectionProxy*’) to type ‘class TMAConnection*’ (source type is not polymorphic)
ConnectionPool::get_connection()
returns 一个 ConnectionProxy
其中 不是 一个 Connection
直接对象,而是包含一个:
ConnectionProxy
允许您访问底层连接对象。
// create a pool
auto pool = ...
// get a ConnectionProxy
auto proxy = pool->get_connection();
// cast to your type
if(auto conn = dynamic_cast<TMAConnection*>(proxy.operator->()))
{
// success!
}