Yet another copy constructor question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Yet another copy constructor question

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Posts
    593

    Yet another copy constructor question

    Code:
    #pragma once
    
    #include "..\SkinnedMesh\skinnedMesh.h"
    
    class Objects
    {
    public:
    	Objects(SkinnedMesh *_mesh) { mesh = _mesh; }
    	~Objects(void) { }
    
    	void setPos(const D3DXVECTOR3& _pos) { pos = _pos; }
    	void setRot(const D3DXVECTOR3& _rot) { rot = _rot; }
    	void setScale(const D3DXVECTOR3& _scale) { scale = _scale; }
    
     
    
    protected:
    	SkinnedMesh *mesh;
    	D3DXVECTOR3 pos;
    	D3DXVECTOR3 rot;
    	D3DXVECTOR3 scale;
    	D3DXQUATERNION rotQuat;
    };
    
    
    class CCB : public Objects
    {
    	 CCB(const CCB& ccb) : mesh(ccb.mesh)
    
    };
    Here CCB(const CCB& ccb):mesh(ccb.mesh) says mesh is not a static member of CCB.
    Thanks
    Jack

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,358

    Re: Yet another copy constructor question

    Did you mean:
    Code:
    class CCB : public Objects
    {
    	 CCB(const CCB& ccb) : mesh(ccb.mesh) {}
    
    };
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2010
    Posts
    593

    Re: Yet another copy constructor question

    Hello Victor,
    That was a typo, sorry about that.
    But the result is still the same.

    [google translated]
    Error 1 error C2614: 'the CCB': member initialization illegal: 'mesh' is not the base or member class d: \ visual studio projects \ perfectsim \ perfectsim \ Objects \ Objects.h to 29 1 PerfectSim
    Thanks
    Jack

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,358

    Re: Yet another copy constructor question

    Victor Nijegorodov

  5. #5
    Join Date
    Dec 2010
    Posts
    593

    Re: Yet another copy constructor question

    Great thanks, have that fixed.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,426

    Re: Yet another copy constructor question

    Quote Originally Posted by lucky6969b View Post
    Great thanks, have that fixed.
    You may have fixed the compiler error, but I highly doubt that your copy constructor actually creates copies, meaning a runtime nightmare.

    I see many members of "Objects" that aren't being copied when the copy constructor is called. So now you have half or semi copies floating around, and that alone can cause all sorts of runtime issues and problems.

    The purpose of the copy constructor is not for your personal use. It is called whenever the compiler decides (and you mostly have very little say in this decision) to make a copy. That can occur at times you don't even expect it to happen. Therefore your copy constructor must create a real copy -- not a half or semi-copy. In other words, if I take that copy and use it instead of the original object, the program must externally behave exactly the same.

    Since copying (and assignment operator, which you failed to show) is something intrinsic, you can't write a copy-assignment operator that can have side effects, and the way your code looks, it has plenty of side-effects due to the non-copying of all members.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 14th, 2013 at 05:27 AM.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center