Answer the question
In order to leave comments, you need to log in
What's the difference between passing by pointer and by reference?
There is a base class A.
There is a function bool foo(A &qw, A *qw2)
What difference does it make what to pass to the function, a reference to a class object, or a pointer to a class object.
In the first case, the inversion is qw.xx, and in the second case qw2->xx.
Please explain what is the difference. And how to be more correct.
Answer the question
In order to leave comments, you need to log in
About passing an object to a function - usually a reference , BUT!
1. If the library lives on pointers (Qt) - a pointer.
void fillCombo(QComboBox* x) {} // хорошо
fillCombo(ui->comboHistory);
void fillCombo(QComboBox& x) {} // плохо
fillCombo(*ui->comboHistory);
// Хуже, но возможно
void import(AsyncRunner* asy) {
if (asy) {
asy->run([](){
doImport(x);
});
} else {
doImport(x);
}
}
import(nullptr);
// Лучше
void import(AsyncRunner& asy) {
asy.run([](){
doImport(x);
});
}
import(NoAsync::INST);
void write(size_t size, const char* data) {}
void save(std::string_view fileName) {}
void transform(Matrix4 x) {}
Obj::Obj(std::string aName) : name(std::move(aName)) {}
The pointer can be nullptr (do not point to anything). The reference cannot be uninitialized.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question