Search found 4039 matches

by albinopapa
March 31st, 2020, 5:57 pm
Forum: Everything
Topic: Container : when do I need a move constructor ?
Replies: 5
Views: 29

Re: Container : when do I need a move constructor ?

If a destructor is declared, even if declared as = default it prevents the compiler from defining default special member functions like the copy/move constructors and assignment operators. This is why it's best to define them all or define none. It's even been suggested in some CppCon videos that yo...
by albinopapa
March 31st, 2020, 5:48 pm
Forum: Everything
Topic: overloading an operator, why?
Replies: 2
Views: 35

Re: overloading an operator, why?

Take your pick: struct Vec2 { float x, y; }; Vec2 operator+(Vec2 const& a, Vec2 const& b ) { return { a.x + b.x, a.y + b.y }; } Vec2 add(Vec2 const& a, Vec2 const& b ) { return { a.x + b.x, a.y + b.y }; } auto a = { 32.f, 3.14f }; auto b = { 37.f, 1.06f }; Choose: auto c = a + b; or auto c = add( a,...
by albinopapa
March 31st, 2020, 4:06 pm
Forum: Everything
Topic: Container : when do I need a move constructor ?
Replies: 5
Views: 29

Re: Container : when do I need a move constructor ?

The move constructor is only useful for classes that hold resources, like the new/delete raw pointers or file pointers. Usually, it's for transferring ownership from one object to another. When one object moves it's resource to another, the first object sets that resource to some invalid but reusabl...
by albinopapa
March 31st, 2020, 3:56 pm
Forum: Everything
Topic: Container : when do I need a move constructor ?
Replies: 5
Views: 29

Re: Container : when do I need a move constructor ?

Actually it's the declaring and defining of the destructor. Since you declared a destructor, the only implicit constructor is the copy and maybe the default constructor. Since you deleted the copy constructor, there was no way to assign one A to another. It's good that you are deleting the copy cons...
by albinopapa
March 31st, 2020, 3:46 pm
Forum: Everything
Topic: std::string::operator+(int) ?
Replies: 13
Views: 53

Re: std::string::operator+(int) ?

Well, while there is nothing technically wrong with using macros, your usage of them can have unexpected results. #define TOS( x ) std::to_string( x ) This should probably be: #define TOS( x ) std::to_string( ( x ) ) Probably not a big deal since you aren't using 'x' multiple times in this macro, bu...
by albinopapa
March 31st, 2020, 6:11 am
Forum: Everything
Topic: std::string::operator+(int) ?
Replies: 13
Views: 53

Re: std::string::operator+(int) ?

I guess you'd also have to overload the operator+ for double and float or whatever other types you want to concatenate strings with.
by albinopapa
March 31st, 2020, 6:10 am
Forum: Everything
Topic: std::string::operator+(int) ?
Replies: 13
Views: 53

Re: std::string::operator+(int) ?

Apparently not. The only way to overload the operators is by having the parameter list include a user-defined type, such as std::string. So operator+( char const*, int ) won't work. The closest thing you can do is use User Defined Literals. There is one for std::string already built into the standar...
by albinopapa
March 31st, 2020, 4:18 am
Forum: Everything
Topic: List of subclass pointers...
Replies: 12
Views: 69

Re: List of subclass pointers...

std::variant takes some getting use to, just a heads up. I spent the better part of 6 weeks to get to the point I could use it comfortably. Not saying you'd have to take that long, but it's not as straight forward as dynamic polymorphism. For instance, there are the global accessors: std::get<> and ...
by albinopapa
March 31st, 2020, 4:11 am
Forum: Everything
Topic: std::string::operator+(int) ?
Replies: 13
Views: 53

Re: std::string::operator+(int) ?

In regards to the lisibility, I finally looked it up and found that it does mean readability, I just couldn't figure it out based on context for some reason.
by albinopapa
March 31st, 2020, 4:09 am
Forum: Everything
Topic: std::string::operator+(int) ?
Replies: 13
Views: 53

Re: std::string::operator+(int) ?

First the operator+( char ), is adding a character to the end of a string, not a number. Make sure to define both: std::string operator+( int, std::string ); std::string operator+( std::string, int ); This way if the "int" comes before or after the std::string, it will call one of these functions. T...