Jan 7, 2015 at 8:22pm UTC
Hi,
I have a question. I would like to get the this pointer by call by reference. Is this possible? I hoped to get it with this code, but it doesn't work:
[code="cpp"]class DemoClass
{
public:
DemoClass();
int x;
void setParam(const DemoClass ¶m){
param=this;
}
};
int main(int argc, char *argv[])
{
DemoClass *test1,*test2;
test1->x=1;
test2->x=2;
test1->setParam(*test2);
qDebug() << test1->x;
qDebug() << test2->x;
};
[/code]
I get alwayes the error code "C2678". But I don't understand how I should change my code to avoid this.
bg,
flambert
Jan 7, 2015 at 8:48pm UTC
this is a special kind of pointer type. Try dereferencing it ie:param=*this
Aceix.
Jan 7, 2015 at 8:53pm UTC
You're simply trying to assign to a 'const' declared parameter. Remove 'const' and everything should work well.
(But better assign some memory to your test1 and test2 variables first...)
EDIT: Added comment in parenthesis.
Last edited on Jan 7, 2015 at 8:54pm UTC
Jan 7, 2015 at 9:03pm UTC
test2 is now a copy of test1 . Both are different objects as before. test2 just got the attributes assigned from test1 .