Ivan Ivan - 1 year ago 85
C++ Question

Passing by value and by pointer

This is usual question, but I can't make it work.

This is prototype of function:

void QSerialPort::setPort(const QSerialPortInfo &serialPortInfo)

and I am trying to use it, like this:

QSerialPortInfo *serialPortInfo = new QSerialPortInfo("H");
QSerialPort *SerialPort=new QSerialPort;
SerialPort->setPort( &serialPortInfo);

As I understand it in prototype we have reference to object, but I have a pointer to QSerialPortInfo, so I anyway us &. It all doesn't work.

What am I doing wrong?

Answer Source

QSerialPortInfo should not be dynamically allocated (this is not really QT-way, I know).

In your case it should be SerialPort->setPort(*serialPortInfo) since you need to dereference your pointer to pass variable by reference.

But this code should be written like here:

QSerialPortInfo serialPortInfo("COM1");
QSerialPort* SerialPort = new QSerialPort;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download