General message class in trouble with const function

Ingo Förster
Ingo Förster used Ask the Experts™
on
In my QT app I have a global class to show error messages.  This is working well in common functions.

int32 MdiChildHex::startHex(QString strBurnDrive)
{
    int32 ret = ::SetBurnDevice(strBurnDrive.at(0).toLatin1());
    if(showDiskbutlerMessage(ret, this)==false) {
        thisSuccessfullCreated = false;
        return;
    }
}

Open in new window


But if the function will hold a "const" in it, the function is not recognized-

int32 MdiChildHex::ReadSector(int32 sectorIndex) const
{
    char *buf = new char[2352];
    QByteArray mdata;

    int32 ret = ::ReadSectors(BS_CURRENT_DEVICE, sectorIndex, 1, BS_IMG_ISO, buf, bufferSize);
    showDiskbutlerMessage(ret, this);

    QByteArray byteArray = QByteArray::fromRawData(buf, bufferSize);

    hexView->clear();

    hexView->setData(new QHexView::DataStorageArray(byteArray));

    return ret;
}

Open in new window


The message I see in QTCreator is:
mdichild_hex.cpp:116:5: error: no matching function for call to 'showDiskbutlerMessage'
messanger.h:7:6: note: candidate function not viable: no known conversion from 'const MdiChildHex *' to 'QWidget *' for 2nd argument

message.h
#ifndef MESSANGER_H
#define MESSANGER_H

#include <QtWidgets>
#include "FoxSDKExport.h"

bool showDiskbutlerMessage(int32 nError, QWidget* parent);

#endif // MESSANGER_H

Open in new window


message.cpp
#include "messanger.h"
#include "utils_common.h"
#include <QMessageBox>


bool showDiskbutlerMessage(int32 nError, QWidget* parent){

    if (nError != BS_SDK_ERROR_NO){

        TCHAR chError[2048];
        int32 nLength = 2048;

        ::GetText(nError,chError, &nLength);

        QString errDesc;
#if defined (WIN32)
        errDesc= QString::fromUtf16(chError);
#else
        errDesc= QString::fromUtf8(chError);
#endif

        QMessageBox::information(getMainWindow(),  QWidget::tr("Diskbutler Error"),
                                  errDesc);
        return false;
    }

    return true;
}

Open in new window


What is the secret here with const?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Generally, the const function must ensure that its code cannot change the member variables. This way also all called functions in the body must have interface that proves that they cannot change them. This means that you have to add const also to them.

(Monday morning, being too dumb for more detailed answer. :)
Top Expert 2016

Commented:
non-member functions cannot be declared as const. the simple reason is that global functions don't have private or protected member variables where they have access to. by declaring a member function as const, you would state that the function doesn't change member data (of the class object) and doesn't use non-const member functions (which might change object data). for global functions all this could not apply.

Sara

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial