This question already has answers here:
Why doesn't a const method override a non-const method in C++?
(6 answers)
virtual function const vs virtual function non-const
(3 answers)
Top-level const doesn't influence a function signature
(7 answers)
const qualifier disappears from pure virtual function [duplicate]
(1 answer)
Closed 2 years ago.
Suppose I have a method1 in the parent class and I want to override it in a subclass. Does adding const affect the overriding? For example,
int Subclass::method1(const int a)const {...}
Does this override
int Parentclass::method1(int a) {...}
correctly?
Related
This question already has answers here:
How do I remove code duplication between similar const and non-const member functions?
(21 answers)
Closed 2 years ago.
Consider the following code:
http://coliru.stacked-crooked.com/a/d89377889a8ff749
IStorage has const and non-const get methods.
The concrete implementation defines them, but the definition is just copy-paste.
I could not call one get method from another because of the const mechanics of C++.
Is there any way to avoid this copy-paste?
This is one of the legitmate uses of const_cast
const std::string* get(ID id) const override
{
return const_cast<ConcreteStorage*>(this)->get(id);
}
This question already has answers here:
when does a function have to return a reference in c++ [duplicate]
(5 answers)
C++: what is the advantage of references in this case?
(3 answers)
C++ return by reference what are the effects?
(2 answers)
Closed 2 years ago.
I came across the code, where in a class one of the member function returns by reference. What are the advantages of doing it like that? E.g.:
class SomeClass {
std:vector<int> some_vals;
// some functions that modify some_vals
public:
const std::vector<int>& get_some_vals() const { return some_vals; }
};
And then when the function is used:
SomeClass my_obj;
const vector<int>& some_vals = my_obj.get_some_vals();
What is the advantage of doing it like this instead of just returning std::vector?
This question already has answers here:
C++ How to pass member function pointer to another class?
(2 answers)
How can I pass a member function where a free function is expected?
(9 answers)
Calling C++ member functions via a function pointer
(10 answers)
Closed 2 years ago.
Next answers not responding my question:
C++ How to pass member function pointer to another class? (2 answers)
How can I pass a member function where a free function is expected? (9 answers)
Calling C++ class methods via a function pointer (10 answers)
I have two classes:
typedef void (*pPrintContent)(QPainter& painter, bool friendly);
class KonsolePrintManager : QWidget
{
Q_OBJECT
public:
explicit KonsolePrintManager(QWidget *parent = nullptr) : QWidget(parent){}
void printRequest(pPrintContent pContent, QWidget *parent);
And:
class TerminalDisplay : public QWidget
{
Q_OBJECT
public:
void printContent(QPainter &painter, bool friendly);
void printScreen();
In printScreen () I intend to do something like:
void TerminalDisplay::printScreen()
{
KonsolePrintManager pManager(this);
pManager.printRequest(printContent, this); #Here the error occurs
}
And the error occurs when passing the printContent () function as a parameter of printRequest.
How could I resolve this issue?
This question already has answers here:
Why is a pure virtual function initialized by 0?
(11 answers)
Virtual/pure virtual explained
(12 answers)
Closed 3 years ago.
I was reading through the source code of a program written in C++ when I came across a few function declarations like this:
virtual bool _Open(LPCTSTR aFileSpec, DWORD &aFlags) = 0;
Why is this declared function being set equal to zero?
Additional Question: (Edit)
What would a statement like this mean?
virtual __int64 _Length() const = 0;
What other keywords could replace const, and what would they mean?
This is a pure virtual function: https://en.cppreference.com/w/cpp/language/abstract_class
Deriving non-abstract classes are expected to implement it.
This question already has answers here:
Is there any reason to use this->
(16 answers)
Use of "this" keyword in C++ [duplicate]
(6 answers)
Closed 8 years ago.
Class::Class() {
this->func();
}
and
Class::Class() {
func();
}
Are there any differences between these two ways of calling a function?
Yes, this->var ensures you're working with instance variable - which may be useful when your instance variables may be shadowed inside a block.