Unsolved The question about postpostion CONST
-
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?
-
@qazaq408
You may make this methodconst
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 alterthis
, as that will allow you to call it off aconst
instance, which otherwise you would not be able to do. -
@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.
-
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?
-
My thought is that don't mark a function as const if you don't want it being called by a const pointer / instance.