Home > database >  Why can't I reassign the value of a pointer?
Why can't I reassign the value of a pointer?

Time:10-26

I am using a library for GUI (Qtitan for Qt).

There I have a class called NavigationViewHeader whose pointer I can access by navigationView->header().

Now I want to reassign the content of the pointer, but it tells me (translated)

The function "NavigationViewHeader::operator=(const NavigationViewHeader &)" (implicit declared)" cannot be referenced (it is a deleted function).

NavigationViewHeader newHeader(navigationView);
NavigationViewHeader* oldHeader = navigationView->header();

*oldHeader = newHeader;

Is it possible to reassign the value or does the framework maybe restrict this by themself? There Is no = delete function inside the framework-class.

CodePudding user response:

The error message is a bit confusingly worded. NavigationViewHeader doesn't declare a copy assignment operator, which is why the compiler implicitly declares one. However, one of its base classes has a deleted assignment operator, so this fails.

I assume NavigationViewHeader inherits from QObject since you mentioned it being a Qt GUI. Those cannot be copy-assigned. See here in the documentation.

QObject has neither a copy constructor nor an assignment operator. This is by design. Actually, they are declared, but in a private section with the macro Q_DISABLE_COPY(). In fact, all Qt classes derived from QObject (direct or indirect) use this macro to declare their copy constructor and assignment operator to be private. The reasoning is found in the discussion on Identity vs Value on the Qt Object Model page.

Side note: I think this part of the documentation is a bit outdated. Privating operators was a C 03 technique. They likely changed that macro to delete the method instead.

  • Related