Simple C++ question

Started by
26 comments, last by xropi 21 years, 6 months ago
quote:Implementing assignment in term of the copy constructor.
Will be expensive in case of self-assigment (goes through unneeded copy).

Since vector::swap needs a non-const argument, I would have thought you''d want to remove the ''const''.

Why not:

  class Cxxx{private:	auto_ptr<vector<int> > vec;	// other members herepublic:	Cxxx() : vec(new vector<int>)	{}	Cxxx(const Cxxx& rhs) : vec(new vector<int>(*(rhs.vec))) // etc.	{}	void swap(const Cxxx& rhs) throws()	{		swap(vec, rhs.vec);		// and any other members	}	Cxxx& operator=(const Cxxx& rhs)	{		if(this == &rhs) return *this;		Cxxx temp(rhs);		temp.swap(*this);		return *this;	}};  

It doesn''t copy unnecessarily, and won''t fuck up the left-hand object unnecessarily.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
Advertisement
Bah, you removed the const whilst I was posting.

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
quote:Original post by DrPizza
Bah, you removed the const whilst I was posting.


I hope you won''t holdit against me

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
may I ask why in the hell you are dynamically allocating that vector? It seems completely redundant and useless.

You could achieve the same effect like this:


  class Cxxx{protected:  std::vector<int> m_vecx;};  




The default copy ctor and assignment operators are fine. They''ll call the appropriate ctors and operators for the vector, which will copy everything over from vector to vector.
daerid@gmail.com
quote:
may I ask why in the hell you are dynamically allocating that vector? It seems completely redundant and useless.

You could achieve the same effect like this:

The default copy ctor and assignment operators are fine. They''ll call the appropriate ctors and operators for the vector, which will copy everything over from vector to vector.



Of course, but if you read my comment carefully, you will see that it might have been anything else. (a pointer to a base class and then while instantiating, assign a derived object''s address for exmpl.)

The intent of the example was a "real" situation, in which the default compiler generated code doesn''t work correctly so we need to write them by hand.


Others: Thank you for your patience. I will implement my things using adjunct member functions like MakeEmpty() and Assign().


I don''t understand all that you wrote. You told me "you musn''t do that" but I still cannot figure out the reason. (it''s not a real reason that "because the C++ standard doesn''t allow this"; a real reason is the existence of a situation in which we were in trouble and that''s why the C++ standard excludes it)

I read in the MSDN the related topics but there was nothing about callinng destructors from member functions but I belive you. Although I, personally, don''t see any exact case when this would be a problem.
I'm interested in full featured 3D system development.
Refer to GotW #23 to see what''s wrong with the this->~Cxxx(); stuff.
quote:Original post by Beer Hunter
Refer to GotW #23 to see what''s wrong with the this->~Cxxx(); stuff.



Thanks! This article showed me the truth.
My original method is really evil and avoidable.
I'm interested in full featured 3D system development.
quote:Original post by xropi
My original method is really evil and avoidable.

It wouldn''t arise if you''d follow the advice "do the simplest thing that can possibly work" rather than looking for strange and convoluted solutions to a very simple problem and worrying about efficiency.

This topic is closed to new replies.

Advertisement