cannot access derived class from base class when using factory

I've got the following code, trying to access functions that go beyond my base class..

When I do the compiler states it doesn't see my derived class functions such as my public member `TestCPPClient` nor `querySymbols` function.. leading me to believe it can only see the Super class.. is my factory not setup correctly?

1
2
3
4
5
6
7
    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); 

    this->tma_conn_pool->release_connection(std::move(proxy_conn));


1
2
3
 error: cannot dynamic_cast ‘& proxy_conn’ (of type ‘class cpool::ConnectionPool::ConnectionProxy*’) to type ‘class TMAConnection&’ (source is not of class type)
   48 |     TMAConnection* x = dynamic_cast<TMAConnection&>(&proxy_conn);
      |                                                                ^


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once

#ifndef TMAConnection_H
#define TMAConnection_H

#include "TMAConnection.h"
#include <connection-pool/pool.h>
#include "tma-client/TestCppClient.h"

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; 
    }

    // cannot call this function
    void querySymbols(std::vector<tmaapithrift::ContractDescription> & 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; // cannot see this

private:
    TMAConnection(){ }
    friend cpool::ConnectionPoolFactory<TMAConnection>;
    bool connected = false;
    
    const char* gateway_host;
    int gateway_port;
    uint16_t tma_client_id;
};


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 )} );
    }
};

#endif 



**Main.cpp**

 
auto tma_conn_pool = cpool::ConnectionPoolFactory<TMAConnection>::create(32, target_gateway_host, gateway_port);


I store the reference to this `tma_conn_pool` throughout my server code as

 
cpool::ConnectionPool * const tma_conn_pool;


When I tried adding Templates to the ConnectionPool pointer i get more errors that this is not a template..

All other things with `override` work as expected with my customized code from the derived class `TMAConnection` .. just nothing beyond `override` functions... any hints appreciated.. I am guessing I need to modify the Factory ?

The connection pool is a library I added, source can be found at https://github.com/malikkirchner/connection-pool
Last edited on
Change line 2:

TMAConnection* x = dynamic_cast<TMAConnection*>(&proxy_conn); // Noie: & -> *

since the result is pointer. You need to decide whether you want a pointer or a reference. Both is not possible.
Hi Coder777,

I believe I need a pointer, as the design of the connection pool keeps ownership of the objects to gives me via create()

I am going to see if I need to simply modify the factory... or in fact modify the entire library to fit my needs.. but I think I am just needing to understand how to get the pointer of the derived class instead of cpool::ConnectionPool::ConnectionProxy

EDIT:

here's my attempts to modify to cast to a pointer ..

1
2
3
4
5
6
7
    cpool::ConnectionPool::ConnectionProxy proxy_conn = this->tma_conn_pool->get_connection();
    TMAConnection* x = dynamic_cast<TMAConnection*>(&proxy_conn);


/home/server.cpp: In member function ‘std::vector<std::__cxx11::basic_string<char> > DataFetcher::process_data(std::vector<std::__cxx11::basic_string<char> >, int, int)’:
/home/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);



when I look at the ConnectionProxy descriptor

https://github.com/malikkirchner/connection-pool/blob/master/src/pool.cpp#L28-L72

it looks as if I am supposed to call itself with
1
2
3
Connection* ConnectionPool::ConnectionProxy::operator->() { return m_connection; }

Connection& ConnectionPool::ConnectionProxy::operator*() { return *m_connection; }


but these seem to fail.. or I am not understanding this enough yet
Last edited on
According to the documentation I think it's supposed to be this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    cpool::ConnectionPool::ConnectionProxy proxy_conn = this->tma_conn_pool->get_connection();
    TMAConnection* x = dynamic_cast<TMAConnection&>(&proxy_conn);

if(proxy_conn.valid()) // Check validity
{
    if(proxy_conn->is_healthy()) // Check connection
    {
      // In order to access your specific connection you can use this:
      TMAConnection& x = dynamic_cast<TMAConnection&>(*proxy_conn); // Note that this may throw
      x.querySymbols(empty_contract_vect, sub_batch_vector); 
    }
    else
      std::cout << "Problem with connection\n"; // Or proxy_conn->connect()
}
else
  std::cout << "Invalid connection\n";

    this->tma_conn_pool->release_connection(std::move(proxy_conn));
An interface that requires a [dynamic_]cast seems to be rather flawed...
Topic archived. No new replies allowed.