- #36
Filip Larsen
Gold Member
- 1,884
- 785
Jarvis323 said:And you can use std::string as a helper, to make life easier if you want,
C:Vec operator+ (const Vec& rhs) const { return Vec(( std::string( name ) + " + " + std::string( rhs.name ) ).c_str() ,x + rhs.x,y + rhs.y ); }
Note it is returning a Vec by value, which is crucial. You wouldn't want to return a temporary object by address/reference for reasons already explained.
I know you guys are trying to help yungman as he goes in strange directions, but advice him to mix std::string and C-strings is in my opinion a good example of showing him how C++ happily let's people shoot themself in the foot if they really want to. The c_str() from the above expression is going to be a dangling pointer no matter what context you use it in.
I will repeat my recommendation to yungman: If you want to learn modern C++ then use the modern "safe" API's, like the standard library, instead of trying to visit every anti-pattern and trap that the C-style API's has to offer. Shooting yourself in the foot over and over is of course one kind of lesson, but since a workable and clean approach for C++ programs do exists, why not take that instead.