c++问题

majia041

新手上路
注册
2003-04-14
消息
106
荣誉分数
0
声望点数
0
#include <iostream.h>
class aa
{
public:
aa(int ii):i(ii){}
void setV(int ii){i = ii;}
int getV(){return i;}
private:
int i;
};

class bb
{
public:
bb(aa* app):ap(app){}
void output(){cout<<ap->getV()<<"\n";}
void setAV(int ii){ap->setV(ii);}
private:
aa * ap;
};

void main()
{
bb * bp = new bb(&aa(5)); //《=====哪位XD给分析下对错和解释下
bp->output();
bp->setAV(15);
bp->output();
delete bp;
}

谢谢。
 
顶上去,没人回答吗?
还有我不欢迎某些人的回帖。
 
From the line itself,there is a risk to take an address of temporay object. The behavior is uncertain when the assigned address is allocated for another usage.
In class bb, the constructor makes the object variable ap out of control.
 
When will compiler re-assign or delete the address for the temporay variable?
 
最初由 chhuili 发布
From the line itself,there is a risk to take an address of temporay object. The behavior is uncertain when the assigned address is allocated for another usage.
In class bb, the constructor makes the object variable ap out of control.

知道了,谢谢。
 
这两天太忙了,等过节的时候闲下来,再仔细看看.坛里的C++电扇好象不多啊.
 
guess need to create aa on heap instead of on stack.
 
后退
顶部