Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The question about postpostion CONST
Forum Update on Monday, May 27th 2025

The question about postpostion CONST

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 397 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • qazaq408Q Offline
    qazaq408Q Offline
    qazaq408
    wrote on last edited by
    #1

    A few years ago,i begin to study c++ by myself,i haven't teacher but a book that's named 《C++ Primer Plus 5th》.The book told me that use CONST as you can as posible int c++ class,and i also do it int accordance with book.

    And then,i begin study Qt,haven't teacher but a book named 《C++ Gui Programming With Qt4,Second Edition》 of course.But when i wirte code some example of book,i find a question,the code of example of book haven't postpostion CONST in many member function . At first,i think these member function has change member variable so that can't use postpostion CONST,for instance

    void ReadTxt::setLog(const String& str){
        log_PushButton->setText(str);
    }
    

    I always think it's right,because log_PushButton is a member varible of class ReadTxt,and this member function had changed the member varible of class,so i can't use postpostion CONST int the member function.

    But one day,an occasional opportunity ,i add postpostion CONST in this member function,but the code is OK when it complier and run.....

    void ReadTxt::setLog(const String& str)const{
        log_PushButton->setText(str);
    }
    

    And i ask the question in some form,someone tell me,the log_PushButton is a member varible of course,but it's just a pointer.this member function does change the object that's pointed by pointer ,but don't change this pointer,so when i add postpostion CONST,it work normal and it's right.....

    Qt5 has released a few years when i study Qt . So i think it's maybe different between Qt5 and Qt4 ,or mayby the book don't show the complete code. And i begin to find some example
    of Qt5.I open QtCreator(Qt5.12),search example "addressbook" , In this example ,there is a member function(of class mainwindow)

    void MainWindow::updateActions(const QItemSelection &selection)
    {
        QModelIndexList indexes = selection.indexes();
    
        if (!indexes.isEmpty()) {
            removeAct->setEnabled(true);
            editAct->setEnabled(true);
        } else {
            removeAct->setEnabled(false);
            editAct->setEnabled(false);
        }
    }
    

    These code is OK when it compile and run ,but i change the code of this example(Add postpostion CONST int this member function),it's also OK when is compile and run.

    so
    1.Take this member function for instance,i must use postpostion CONST in this member function , or it doesn't matter use postpostion CONST or not?

    2.《C++ Primer Plus》tell me use CONST as you can as posible , but many examples of 《C++ Gui Programming With Qt4》and Qt Assistant don't use postpostion CONST,even those member functions is OK when compile and run after add postpostion CONST . do they have contradiction?

    JonBJ 1 Reply Last reply
    0
    • qazaq408Q qazaq408

      A few years ago,i begin to study c++ by myself,i haven't teacher but a book that's named 《C++ Primer Plus 5th》.The book told me that use CONST as you can as posible int c++ class,and i also do it int accordance with book.

      And then,i begin study Qt,haven't teacher but a book named 《C++ Gui Programming With Qt4,Second Edition》 of course.But when i wirte code some example of book,i find a question,the code of example of book haven't postpostion CONST in many member function . At first,i think these member function has change member variable so that can't use postpostion CONST,for instance

      void ReadTxt::setLog(const String& str){
          log_PushButton->setText(str);
      }
      

      I always think it's right,because log_PushButton is a member varible of class ReadTxt,and this member function had changed the member varible of class,so i can't use postpostion CONST int the member function.

      But one day,an occasional opportunity ,i add postpostion CONST in this member function,but the code is OK when it complier and run.....

      void ReadTxt::setLog(const String& str)const{
          log_PushButton->setText(str);
      }
      

      And i ask the question in some form,someone tell me,the log_PushButton is a member varible of course,but it's just a pointer.this member function does change the object that's pointed by pointer ,but don't change this pointer,so when i add postpostion CONST,it work normal and it's right.....

      Qt5 has released a few years when i study Qt . So i think it's maybe different between Qt5 and Qt4 ,or mayby the book don't show the complete code. And i begin to find some example
      of Qt5.I open QtCreator(Qt5.12),search example "addressbook" , In this example ,there is a member function(of class mainwindow)

      void MainWindow::updateActions(const QItemSelection &selection)
      {
          QModelIndexList indexes = selection.indexes();
      
          if (!indexes.isEmpty()) {
              removeAct->setEnabled(true);
              editAct->setEnabled(true);
          } else {
              removeAct->setEnabled(false);
              editAct->setEnabled(false);
          }
      }
      

      These code is OK when it compile and run ,but i change the code of this example(Add postpostion CONST int this member function),it's also OK when is compile and run.

      so
      1.Take this member function for instance,i must use postpostion CONST in this member function , or it doesn't matter use postpostion CONST or not?

      2.《C++ Primer Plus》tell me use CONST as you can as posible , but many examples of 《C++ Gui Programming With Qt4》and Qt Assistant don't use postpostion CONST,even those member functions is OK when compile and run after add postpostion CONST . do they have contradiction?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @qazaq408
      You may make this method const if you wish, since it does not alter any member variables. It only sets the enablement of them.

      Read through all the replies to my recent topic https://forum.qt.io/topic/115079/how-rigorous-are-you-about-using-const for a discussion of when you might/might not bother to voluntarily use const. BTW, there is nothing at all "Qt" about this, it's just C++.

      IMO, yes, you should make any member function const if you can/it does not alter this, as that will allow you to call it off a const instance, which otherwise you would not be able to do.

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @qazaq408 said in The question about postpostion CONST:

        setLog(const String& str)const

        From a C++ point of view such a function should not be const since they alter the state of the class. I would even consider this a bad practice since a a const setter is somehat contrary. Just because the compiler does not complain does not mean the function must be const. But thats is no Qt specific stuff here and there are a lot of discussions out there for this.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        JonBJ 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          @qazaq408 said in The question about postpostion CONST:

          setLog(const String& str)const

          From a C++ point of view such a function should not be const since they alter the state of the class. I would even consider this a bad practice since a a const setter is somehat contrary. Just because the compiler does not complain does not mean the function must be const. But thats is no Qt specific stuff here and there are a lot of discussions out there for this.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Christian-Ehrlicher

          since they alter the state of the class

          You are right, and I tend to agree with you. (I only meant that the OP could mark it const, without compilation error.)

          The trouble is knowing where to stop in one's investigations of what is "truly" const? If the method has:

          member->somethingElse->someMethod(someArg)->final(otherArg);
          

          how much investigation of these methods & parameters are you prepared to do? And what will you interpret as constituting "alter the state of the class", that is open to interpretation?

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by
            #5

            My thought is that don't mark a function as const if you don't want it being called by a const pointer / instance.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved