Sign in to follow this  

copy constructor

This topic is 3195 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok I was getting a bug in my reference counting system(C++ btw), and I believe it was because I was using the default copy constructor, so when an object was copied, the original object's destructor decrements the reference count, but the copy constructor wasn't incrementing it. The problem is, after I made the copy constructor, my code won't compile, here is the error 1>c:\program files\microsoft visual studio 9.0\vc\include\vector(1211) : error C2558: class 'PVertex' : no copy constructor available or copy constructor is declared 'explicit' 1> c:\program files\microsoft visual studio 9.0\vc\include\vector(1153) : while compiling class template member function 'void std::vector<_Ty>::_Insert_n(std::_Vector_const_iterator<_Ty,_Alloc>,unsigned int,const _Ty &)' 1> with 1> [ 1> _Ty=PVertex, 1> _Alloc=std::allocator<PVertex> 1> ] 1> c:\users\hugh\documents\programming\c++\games\3d\d3d\terrain\terrain.h(18) : see reference to class template instantiation 'std::vector<_Ty>' being compiled 1> with 1> [ 1> _Ty=PVertex 1> ] _Ty _Tmp = _Val; // in case _Val is in sequence 1>c:\program files\microsoft visual studio 9.0\vc\include\vector(1235) : error C2558: class 'PVertex' : no copy constructor available or copy constructor is declared 'explicit' I googled it, and everything says that i had to have made the copy constructor private, or explicit, but I didn't here is the code //public interface public: PVertex(); PVertex(LPDIRECT3DDEVICE9 device); PVertex(float x,float y,float z,LPDIRECT3DDEVICE9 device); PVertex(PVertex & ref); ~PVertex(void); //copy constructor PVertex::PVertex(PVertex & ref) { PVertex::referenceCounter++; this->x = ref.x; this->y = ref.y; this->z = ref.z; } I can't figure out what I'm doing wrong(I've never played with copy constructors before, since I've never had the need for one)

Share this post


Link to post
Share on other sites
Quote:
Original post by godsenddeath
ok I was getting a bug in my reference counting system(C++ btw)

Just curious, what are you using this ref counting system for?

As for copy constructors, you do need them as soon as you're storing objects inside std containers. For plain data objects, the default copy constructor et al will suffice, but for anything more complex (for example, an object that dynamically allocates resources), you'll want to give careful thought to what should happen with those resources when you make a copy of the object. Do you need a deep or a shallow copy? For a shallow copy, who will own those resources (e.g. who has to clean them up)?

Share this post


Link to post
Share on other sites

This topic is 3195 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this