inheritance, copy costructors and assignment operators issue
Hello everybody, since last year i've been developing an SDL-based library for videogames,
but lately i'm stuck on a problem that concerns (i think) inheritance and copy costructors/assignment operators.
I've done lot of searching over internet for a solution but still i don't get it.
Here it is a sum of the code:
Code:
// base class (concrete)
class GFX
{
protected:
// some fields
// ...
public:
// base constructor
GFX()
{
// fields inizializations
//...
}
// default constructor
GFX( const char *file,
const SDL_Color Transparent = None,
const u16 InitNoFrames = 1 )
{
GFX();
// more fields inizialization
// ...
}
// methods
}
// 1st level derived class (abstract)
class ScreenObject : public GFX
{
protected:
// some fields and methods
public:
ScreenObject()
{
// fields inizializations
//...
}
// methods (some virtuals, some not)
virtual void Draw() = 0; // abstract method
}
// 2nd level derived class (concrete)
class Sprite : public ScreenObject
{
public:
// some fields
// Sprite constructor
Sprite()
{
// fields inizializations
//...
}
//! Alternative sprite constructor
Sprite( const GFX& InitGFX )
{
Sprite();
*this = InitGFX; // ???
// GFX::operator=( InitGFX );
}
// methods (some virtuals, some not)
}
As you can see i've declared no copy costructor or assignment operators,
meaning i would like to use the default ones that performs field-by-field copy.
The problem arise in the "Alternative sprite constructor".
Here i would like to do a field-by-field copy between an object of the
base class GFX and the derived class Sprite.
I don't know why, but it seems that operator= does not work as expected, and i get
lot of invocations of the Sprite constructor that don't returns.
Thanks in advance for answering!
Re: inheritance, copy costructors and assignment operators issue
Your GFX constructor that takes at least one argument looks wrong: it appears to be attempting to call the default constructor, but constructor chaining does not exist in C++ (yet). The same goes for the Sprite constructor that takes a GFX by const reference. It also seems weird that you are trying to construct the current object by copy assigning the GFX argument to it.
Also, note that initialisation of member variables actually only takes place in the initialisation list; once in the body of a constructor, you only perform assignment.
Re: inheritance, copy costructors and assignment operators issue
Quote:
Originally Posted by
laserlight
Your GFX constructor that takes at least one argument looks wrong: it appears to be attempting to call the default constructor, but constructor chaining does not exist in C++ (yet). The same goes for the Sprite constructor that takes a GFX by const reference.
Boy, you are right!
Maybe i've made confusion between Java and C++ having studied both in the same time.
If only the compiler had warned me about constructor chaining...
EDIT: now everything seems to work as expected, thank you for your precious help!
Code:
// base class (concrete)
class GFX
{
protected:
// some fields
// ...
private:
void Init()
{
// fields inizializations
//...
}
public:
// base constructor
GFX()
{
Init();
}
// default constructor
GFX( const char *file,
const SDL_Color Transparent = None,
const u16 InitNoFrames = 1 )
{
Init();
// more fields inizialization
// ...
}
// methods
}
// 1st level derived class (abstract)
class ScreenObject : public GFX
{
protected:
// some fields and methods
public:
ScreenObject()
{
// fields inizializations
//...
}
// methods (some virtuals, some not)
virtual void Draw() = 0; // abstract method
}
// 2nd level derived class (concrete)
class Sprite : public ScreenObject
{
private:
void Init()
{
// fields inizializations
//...
}
public:
// some fields
// Sprite constructor
Sprite()
{
Init();
}
//! Alternative sprite constructor
Sprite( const GFX& InitGFX )
{
Init();
//*this = InitGFX; // DOES NOT WORK!
GFX::operator=( InitGFX ); // works partially?
}
// methods (some virtuals, some not)
}
Re: inheritance, copy costructors and assignment operators issue
By the way, the constructor that you've called "base constructor" is actually the default constructor, and the one you've called "default constructor" isn't. A default constructor is one that can be "called" with no arguments.
The reason that the compiler didn't warn you about chaining constructors is that your use of GFX() in the other constructor was quite legitimate, if a bit pointless in this case - it simply created a temporary GFX object and then destroyed it. Sometimes creating and destroying an object in this way can make sense, depending on exactly what happens in the constructor and destructor of the object.