Intermediate Homework4 questions

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
willkillson
Posts: 18
Joined: September 3rd, 2017, 7:38 am

Intermediate Homework4 questions

Post by willkillson » December 31st, 2017, 6:52 am

I was going over your implementation of the Homework 4, and I have never seen the syntax used here with the

Code: Select all

Entry(const char* name, int value)
                                  :
                                  value( value)

Are you doing this to involve the constructors of the int data member value?

And why would you do this instead of just doing this->value = value; once inside the entry Constructor, and what is a Entry() = default?

Sorry ive taken two classes in C/C++ and im trying to patch in information that I am lacking.

Below is a screen shot of the video just in case I was too vague.

https://i.imgur.com/QkGdUeK.png


Here is my solution to this project (I didn't realize we were suppose to go low level on this bad boi...)

https://github.com/willkillson/ASCIIBarChart.git
https://www.twitch.tv/willkillson

Come help me program!

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

Re: Intermediate Homework4 questions

Post by albinopapa » December 31st, 2017, 9:24 am

1) Are you doing this to involve the constructors of the int data member value?

2) Why would you do this instead of just doing this->value = value; once inside the entry Constructor,

3) What is a Entry() = default?
Answers:
1) Intrinsic types like int, float, char don't have constructors, so the short answer is no.

2) The reason to use this method of initialization is optimization really. When your object "Entity" is created, there are two things that happen. First memory is allocated to the size of all data members listed in the class. If your class has two ints, the your class is allocated 8 bytes ( int is 4 bytes 2 * 4 = 8 ). Second, the constructor is called to initialize those members. The reason to use the class initializer list is to have the data members initialized at the same time as the object allocation. If you use the constructor body, you are assigning values after the members of the object have been initialized, which means that the members first get allocated, then initialized then assigned new values. If you use the initializer list as chili did in the video, your members get allocated and initialized and that's it. So you save a step.

3) Basically it means to use a compiler generated default for the constructor. Other functions you can meaningfully use = default on is the copy and move constructors, the copy and move assignment operators and the destructor.

Entity() = default; // Default constructor
Entity( const Entity& ) = default; // Copy constructor
Entity( Entity&& ) = default; // Move constructor
Entity& operator=( const Entity& ) = default; // Copy assignment operator
Entity& operator=( Entity&& ) = default; // Move assignment operator
~Entity() = default; // Destructor

You'll learn about inheritance later, but for completeness sake:
virtual ~Entity() = default; // Base class virtual destructor

Most of the time, you don't have to even bother with declaring the copy/move constructors or assignment operators nor the destructor. Again, chili covers this stuff in his vids. If you have a unmovable object in your class, then the compiler won't create one for you, so you have to create one. If you have a pointer in your object and you plan on copying from A to B then you probably want to copy the data that the pointer points to so you need to create a copy constructor and copy assignment operator so the data on the heap the pointer points to gets copied, which you must do manually.

You can prevent things from being copied by changing = default to = delete next to the copy constructor and copy assignment operator.

Chili introduces constructors: Beginner tutorial 12.
Chili covers the rest of what I put here in Intermediate tutorial 6.

Sorry, didn't realize that shit was discussed after intermediate tutorial 4.
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

willkillson
Posts: 18
Joined: September 3rd, 2017, 7:38 am

Re: Intermediate Homework4 questions

Post by willkillson » December 31st, 2017, 9:34 am

This is great information, I appreciate it a lot.

I thought I knew enough about operator overloading.

This information is compounding so it is hard to find out where to start (currently intermediate 4).
https://www.twitch.tv/willkillson

Come help me program!

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

Re: Intermediate Homework4 questions

Post by albinopapa » December 31st, 2017, 9:45 am

That's probably why I initially thought it had already been covered, everything kind of runs together. Had to look through the wiki to figure out which videos to reference. Then watch through the vids a bit to make sure it was correct. Don't know if the wiki has really served it's purpose yet, but hopefully it will be utilized more and more.
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