c++pointerssubclassc++builder-xe7appmethod

How to create pointers for subclasses in C++?


I'm with a little trouble when pointing to a subclass of an API.

Here is the original pascal code of what am I trying to do:

  Adapter:=TJBluetoothAdapter.JavaClass.getDefaultAdapter;
  remoteDevice:=Adapter.getRemoteDevice(stringtojstring(targetMACAddress));
  sock:=remoteDevice.createRfcommSocketToServiceRecord(UID);

Reduced version of it:

sock:=TJBluetoothAdapter.JavaClass.getDefaultAdapter.getRemoteDevice(stringtojstring(targetMACAddress)).createRfcommSocketToServiceRecord(UID);

Here I have what I've already tried to do:

  _di_JBluetoothSocket* sock = new _di_JBluetoothSocket;
  sock = &TJBluetoothAdapter::JavaClass->getDefaultAdapter()->getRemoteDevice(StringToJString(targetMACAddress))->createRfcommSocketToServiceRecord(uid);

and:

  TJBluetoothAdapter::JavaClass->getDefaultAdapter()->getRemoteDevice(StringToJString(targetMACAddress))->createRfcommSocketToServiceRecord(uid)* sock;

and:

  TJBluetoothAdapter* sock;
  sock = TJBluetoothAdapter::JavaClass->getDefaultAdapter()->getRemoteDevice(StringToJString(targetMACAddress))->createRfcommSocketToServiceRecord(uid);

However I always get error when compilling, what should I do? Since now, thanks for your advice.


Solution

  • You are declaring your sock variable incorrectly. Do not declare a pointer to a _di_... class type. It is already a pointer (more specifically, it is a typedef for the System::DelphiInterface class, which is a C++ smart pointer wrapper around a Delphi interface pointer).

    _di_JBluetoothAdapter Adapter = TJBluetoothAdapter::JavaClass->getDefaultAdapter();
    _di_JBluetoothDevice remoteDevice = Adapter->getRemoteDevice(StringToJString(targetMACAddress));
    _di_JBluetoothSocket sock = remoteDevice->createRfcommSocketToServiceRecord(UID);
    

    Which is roughly equivalent to this:

    JBluetoothAdapter *Adapter = TJBluetoothAdapter::JavaClass->getDefaultAdapter();
    Adapter->AddRef();
    
    JBluetoothDevice *remoteDevice = Adapter->getRemoteDevice(StringToJString(targetMACAddress));
    remoteDevice->AddRef();
    
    JBluetoothSocket *sock = remoteDevice->createRfcommSocketToServiceRecord(UID);
    sock->AddRef();
    
    ...
    
    sock->_Release();
    remoteDevice->_Release();
    Adapter->_Release();