Why I am getting UMR(unInitialized Memory Read)Error in following code snippet. I am getting UMR error while calling copy constructor of derived class
class base{
public :
base(int);
int id;
int getid()
{
return id;
}
};
base::base(int a)
{
id = a;
}
class derived:public base{
public :
derived(derived &);
int inum;
int getnum()
{
return inum;
}
};
derived::derived(derived &d):base(d.getnum()) // UMR Error
{
inum = id;
}
Can anybody have any idea what is wrong with the implementation?
You derived class never initialize inum
variable. Error message said it to you. d.getnum()
returns uninitialized inum
variable.
I don't think you can compile your code, a few issues dressed below:
class derived:class base{
^^^-> public
should be:
class derived : public base
change int getnum();
to int getnum() const;
derived::derived(derived &d):base(d.getnum) // UMR Error
^^^ function call?
why initialize base::id with derived::inum?
should be:
derived::derived(const derived &d) : base(d) // initialize base with base
{
^^^ copy constructor has const
inum = d.inum; // initialize derived member
// inum = id; why you assign id back to inum again?
}
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.