简体   繁体   中英

How do I dereference the return value of a function returning a pointer without using a temporary variable?

C++ noob here.

So, this is a member function of my class DbHelper :

QSqlQueryModel* getCourses();

Now, whenever I do this in class MyModel where QSqlQueryModel courses is a member:

this->courses = *(dbHelper->getCourses()); // problem here ... this is inside a member function of MyModel

Visual Studio says that

function ... operator= cannot be referenced ... it is a deleted function

Of course I can do this instead:

QSqlQueryModel* q = dbHelper->getCourses();
this->courses = *q;

But I'm thinking that declaring another variable just to dereference it is possibly redundant. So is there a shorter way?

EDIT:

Just verified it with VS and it turned out I really can't. My mind really just got messed up in studying pointers and references the entire afternoon. Thanks people.

The authors of the QSqlQueryModel class are being cute and are cleverly forbidding the copying of instances of that object.

The class member should be a pointer type:

QSqlQueryModel* courses;

as the documentation states that you don't own the memory associated with the pointer. Then you set trivially

this->courses = dbHelper->getCourses(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM