Re: Auto cast an iterator into its base pointer type ?
Posted: March 19th, 2020, 4:13 pm
Didn't you try and *form* a C habit for a while?
Learn to program or die trying
https://forum.planetchili.net:443/
Yes. For example I use classes and methods, but at the end, I put everything in public because I want to easily access every member from anywhere... I know it's bad.albinopapa wrote: ↑March 19th, 2020, 7:08 amI've tried helping someone break the C habit, it wasn't easy for them.
One of the things that this person couldn't wrap their head around was that C++ automatically passes an instance of the class object to it's own methods. So when he created methods he would always put a reference to that classes object.binbinhfr wrote: ↑March 22nd, 2020, 1:24 pmYes. For example I use classes and methods, but at the end, I put everything in public because I want to easily access every member from anywhere... I know it's bad.albinopapa wrote: ↑March 19th, 2020, 7:08 amI've tried helping someone break the C habit, it wasn't easy for them.
Code: Select all
class Thing
{
public:
void Do( Thing& thing ); // <- This is what they would do
void Do(); // Instead of doing this
};
Code: Select all
class Dude
{
friend bool IsDudeCollidingWithPoo( Dude const&, Poo const& );
};
class Poo
{
friend bool IsDudeCollidingWithPoo( Dude const&, Poo const& );
};
bool IsDudeCollidingWithPoo( Dude const& dude, Poo const& poo )
{
return
( ( dude.x >= poo.x + poo.width ) && ( dude.x + dude.width < poo.x ) ) &&
( ( dude.y >= poo.y + poo.height ) && ( dude.y + dude.height < poo.y ) );
}
// Usage:
if( IsDudeCollidingWithPoo( dude, poo ) )
{
// Handle collision
}
Not sure exactly what you mean there. What checking is there in C++ that you wouldn't have in a C program?And all these C++ checking take some CPU times.
Code: Select all
template<typename Fn, typename...Args>
void ThreadSafeCall( std::mutex& mtx, Fn funcToCall, Args&&... funcArgs )
{
auto guard = std::lock_guard( mtx );
funcToCall( std::forward<Args>( funcArgs )... );
}
Code: Select all
void Print( const std::string& s )
{
// shared display routine
mutex_display.lock();
std::cout << s << std::endl;
mutex_display.unlock();
}
// Changes to just:
void Print( const std::string& s )
{
std::cout << s << std::endl;
}
// And called like:
ThreadSafeCall( mutex_display, &Print, "CREATING " + std::to_string( tot_threads ) + " THREADS" );