user2366975
user2366975

Reputation: 4700

Const parameter passing: invalid conversion

I am refactoring a bit of my Qt-cpp code and want to ensure that some QWidgets, gotten from public functions, are const (not alterable).

// class Tabbar:
const Tab* activeTab(){
    return this->activeTab;  // do I need a const here?
}

// class Tabwidget (Tabbar with stacked widget):
void TabWidget::setTabWidget(const Tab2* t,
                             QWidget *w, bool switchToTab)
{
    QWidget* oldWidget = tabWidgets_.take(t);    // <-- error
    if (oldWidget){
        stackedWidget_->removeWidget(oldWidget);
        delete oldWidget;
    }

    tabWidgets_.insert(t,w);                     // <-- error
    stackedWidget_->addWidget(w);
    // ...
}

The error is:

invalid conversion from "const Tab*" to "Tab*" [-fpermissive]

The documentation says on .take-mouseover:

T QHash::take(const Key &key)

...and I am passing a const Tab* as key, so I do not understand the message. The header declaration is

void setTabWidget(const Tab *t, QWidget* w, bool switchToTab = false);

Also I am wondering if the Tab that I pass to setTabWidget needs to be const always.

Upvotes: 0

Views: 75

Answers (2)

djcouchycouch
djcouchycouch

Reputation: 12832

The method expects a reference and you're passing it a pointer. Just dereference the pointer.

QWidget* oldWidget = tabWidgets_.take(*t);

This is assuming of course that Tab is compatible with Key

Upvotes: 1

Nikola Butigan
Nikola Butigan

Reputation: 35

Once u delcare something as const you can't change it no metter what. Also if you define something in your class as const you have to use it as const whole program that means that you can't change it no matter what.Reading this will help you out to understand concept of const how to use it and when to use it.

Upvotes: 0

Related Questions