CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2008
    Posts
    22

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Sep 2008
    Posts
    22

    Re: inheritance, copy costructors and assignment operators issue

    Quote Originally Posted by laserlight View Post
    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)
    }
    Last edited by hi1; November 22nd, 2008 at 05:39 AM.

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured