Copy constructors
Apr 2, 2011 at 4:39pm UTC
So i'm having problems deep copying one vector to another. I have these classes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
struct cloneable
{
virtual ~cloneable(){}
virtual cloneable* clone() const = 0;
};
class Digit: public cloneable
{
protected :
//x and y positons
int Xpos;
int Ypos;
//value in string format
std::stringstream svalue;
public :
Digit();
Digit(const Digit ©)
{copy.svalue.rdbuf();}
~Digit();
//copy fucntion
virtual Digit* clone() const
{ return new Digit( *this ); }
};
class Realnum: public Digit
{
protected :
float value;
public :
Realnum( int x, int y);
Realnum(const Realnum ©): Digit(copy)
{}
~Realnum();
//copy fucntion
Realnum* clone() const
{return new Realnum( *this );}
};
I have two vectors of type <Digit*>
my copying method now looks like this
1 2 3 4 5
for (int i=0; i< equation_buffer.size(); i++)
{
undo_vector[i] = equation_buffer[i]->clone();
}
Now my program compiles without error but I have a problem at runtime. The problem now is that the clone() returns something that does not initialize the values in undo_vector[i]
It returns this:
Realnum::clone returned 0x007d7660 {value=-4.3160208e+008 order=-842150451 } Realnum *
where is should return something more like
Realnum::clone returned 0x007d7660 {value=-2 order=0 } Realnum *
Apr 2, 2011 at 7:01pm UTC
plz help :)
Apr 2, 2011 at 8:42pm UTC
1 2 3
Digit::Digit(const Digit ©){
copy.svalue.rdbuf(); //statement has no effect
}
Maybe you want
this ->svalue.str( copy.svalue.str() );
Topic archived. No new replies allowed.