简体   繁体   English

如何在不使用临时变量的情况下取消引用返回指针的函数的返回值?

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

C++ noob here. C ++菜鸟在这里。

So, this is a member function of my class DbHelper : 因此,这是我的class DbHelper的成员函数:

QSqlQueryModel* getCourses();

Now, whenever I do this in class MyModel where QSqlQueryModel courses is a member: 现在,每当我在QSqlQueryModel courses class MyModel其中QSqlQueryModel courses是成员)中执行此操作时:

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

Visual Studio says that Visual Studio说

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. 刚刚用VS验证过,事实证明我真的做不到。 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. QSqlQueryModel类的作者很可爱,并且巧妙地禁止复制该对象的实例。

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(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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