constructor trouble with private member from another class

The Partridge Family were neither partridges nor a family. Discuss.
Skyver
Posts: 12
Joined: September 26th, 2017, 12:35 pm
Location: Netherlands

Re: constructor trouble with private member from another cla

Post by Skyver » December 3rd, 2018, 9:48 am

thanks alot i also see how hitdetection gets his stuff passed in. the default constructor im having a little trouble to understand how to utilize it but it works for now thanks again il find it out

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: constructor trouble with private member from another cla

Post by albinopapa » December 3rd, 2018, 10:09 am

A default constructor just makes it so you can create an object without having to pass in parameters.

Goal goal; <- calls default constructor

goal = Goal(); <- calls default constructor and copies default constructed Goal to an already constructed Goal object.

If you changed the Goal() = default; to actually do something like randomly select it's own position, you could do something like:

Code: Select all

// Goal.h
class Goal
{
   Goal();
};

// Goal.cpp
#include <random>
Goal::Goal()
{
   std::mt19937 rng( std::random_device{} );
   std::uniform_int_distribution<int> xdist( 0, Graphics::ScreenWidth  - GoalWH );
   std::uniform_int_distribution<int> ydist( 0, Graphics::ScreenHeight - GoalWH );
   PointX = xdist( rng );
   PointY = ydist( rng );
}

// Game.pp
Game::UpdateModel()
{
   // If player reaches goal, reset to new location by assigning default constructed value to 
   // already constructed value, all values will be reset to defaults.
   if( goal.HitDetection( P0 ) )
   {
      goal = Goal();
   }
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply