argument error in copy constructor

I'm trying to create a constructor like this:

1
2
3
Cell::Cell(const Cell &dsfc) {
   Reg::clone(a, dsfc.a);
}



Question 1: if I leave out the const, the compiler gives me lots of errors; why?

As coded above, I still get one error:

error: cannot call member function 'void Reg::clone(Reg&, const Reg&)' without object


Clearly I'm not understanding something here; can anyone enlighten me?

Thanks.
is clone a static mem. funct. of Reg?
The function Reg::clone() is not a static function, it is a member function. You need to instantiate an object of type Reg and then use that member function. Alternatively and if Reg is a class of your own making you can declare it as static and that's it.
Here's the code for clone:

1
2
3
4
5
6
void	SocReg::clone (SocReg &destination, const SocReg &source)	// make a clone of the source.
{
	destination.rv = source.rv;
	destination.c = source.c;
	destination.n = source.n;
}


Perhaps I should re-write it so that the call is more like a.clone(dsfc.a);? Is that a good idea?
IMO, clone should return a copy of itself, so something like this:

1
2
3
SocReg SocReg::clone() const {
    //...
}
+1 firedraco's approach, but with a twist. For me, clone() is a good candidate to be static too.

1
2
3
4
5
6
7
8
9
static void SocReg::clone(SocReg &dest, const SocReg &src);

//And the member function uses the static version so we don't repeat code.
SocReg SocReg::clone(void) const
{
    SocReg newObj;
    SocReg::clone(newObj, *this);
    return newObj;
}


But now that I write that I see it is inefficient. I call for C++ experts here, as I am not a very expert one. I think clone should use the copy constructor so I can return SocReg(*this);. Suggestions?
Stick with firedraco's!

Actually, I think the prototype pattern usually returns a pointer and dynamically allocates the object...
Last edited on
I'm limited in my implementation choices here. I've overridden the assignment operator so that:

reg1 = reg2;

results in:

reg1.n = reg2.c;

What I want to do in my copy constructor is a full copy (a clone, if you will). I can't use the assignment operator, so I don't think I can use your suggestion, unless I'm misunderstanding something.
You are creating a copy constructor for a class named Cell. This class, as far as I can tell, has a member variable of type SocReg, which is another class of yours. Whenever the copy constructor of Cell is called you also want a copy of this other member variable. Right?

I am saying this: Instead of providing a clone() function in the SocReg class, provide a copy constructor. If you have no need to override the default copy constructor, then great!

Actually, if you provide an appropriate copy constructor for the class SocReg, and if Cell's copy constructor's sole purpose is to call clone(), then you can forego the copy constructor of Cell because the compiler-provided copy constructor will just use SocReg's copy constructor and everything will be just fine.
Ahhh...I got you. Excellent thinking. Thanks; I'm still trying to wrap my brain around nested constructors and such.

I appreciate everyone's help.
Topic archived. No new replies allowed.