Constructor Qustion.

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
LittleDude
Posts: 7
Joined: February 23rd, 2018, 7:03 pm

Constructor Qustion.

Post by LittleDude » March 14th, 2018, 7:41 am

Greetings!

I am quite confused with the default constructor in tutorial 19 (Vec2.h) we made the default constructor (and never call it as far as i noticed) even tho we already made another constructor.
Code:

class Vec2
{
public:
Vec2() = default; //
Vec2(float x_in, float y_in); //

public:
float x;
float y;
}

I assume we've implemented the default constructor in case we want to initialize a vector without giving it values.

Off topic:

Chilli, i bought multiple courses of c++ before watching your tuts and you guessed it, i regret it. Every one of them had 5 stars! And every one of them had a teacher with a closed mouth barely talking and when they did you had to turn the volume up. Waste of money!

Have you ever thought about making a mobile video game and sending it to a publisher? (that's my goal) You do have the skills without doubt. It's up to you if you think you can make a good enough game. And with this occasion you could make some unity or unreal engine tutorials (unity has way better deals on royalties).

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

Re: Constructor Qustion.

Post by albinopapa » March 14th, 2018, 8:24 am

To the default constructor, there are a couple of reasons to have one and maybe a couple to not have one.

Reasons for having a default constructor in your class would be:
  • You want to default initialize your object
  • You want an array or vector of those objects
  • You want to delay assigning values based on some conditions
Reasons for not having a default constructor in your class would be:
  • Your class should never be default initialized, all values must be initialized by outside sources through other constructors.
  • You have no constructors at all and your data is public, this allows for aggregate initialization
    • Ex: If Vec2 didn't have any constructors, you could do Vec2 pos = { 300,200 };
    • You can use the curly brace initialization with classes with private data as long as you have a matching constructor Vec2( float X, float Y ), otherwise aggregate initialization doesn't work.
  • Your class is a singleton and must be instantiated through an Instance() function for instance lol.
To the Unity/Unreal tuts, I believe he's made some patreon supported videos with him just messing around with the Unreal engine, but they aren't tutorials. There are plenty of those around anyway, and as you have discovered, not many GOOD C++ instructors.
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

nG Inverse
Posts: 115
Joined: April 27th, 2012, 11:49 pm

Re: Constructor Qustion.

Post by nG Inverse » March 14th, 2018, 5:00 pm

Nice explanation. You should add this to the sticky regarding constructors.

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

Re: Constructor Qustion.

Post by albinopapa » March 14th, 2018, 5:50 pm

nG Inverse wrote:Nice explanation. You should add this to the sticky regarding constructors.
done.
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