I'm trying to understand classes in C++ and this example is stumping me. I don't understand where the class definition ends at. I thought it was at the first }; but I'm finding one more after the OneMoreCheeseGone function.
I'm not sure if the int variable CheeseCount is included in the class. I'm not sure if the OneMoreCheeseGone function is part of the class, or if it is a standard function that can be called from anywhere.
I'm really stumped on exactly how the "this" pointer variable works and I'm getting a little hung up on the confusion in the class definition too.
This code comes from C++ for Dummies. The code does run, and I copied the code from the examples provided with the book, not something I typed myself, so it's not my typos.
I'm going to go look at some other examples on the internet to try to understand "this".
Thanks for any help!
TheMemberFormerlyKnownAsAWOL:
this is an implicit parameter to every class member function, and points to the current instance of the class.
that answer is just as hard to understand as all the other definitions I have been looking at! I'm starting to understand "this", just a little bit.
But, again, I am puzzled by "void OneMoreCheeseGone(Cheese *Block)" function. Does it belong to the class, or is it a standard function? It closes the function with the extra semicolon:
void OneMoreCheeseGone(Cheese *Block)
{
CheeseCount--;
Block->status = "Gone";
};//this puzzles me, why is this semicolon here?
Thanks, I removed it and code still worked. I thought that I had tried that before. I guess I did not.
That part is making a lot more sense now. I'm still getting hung up on "this", but I will continue to dig into it. I'm just getting started, and at this point many of the terms mean so little to me, it takes time for "objects" and "classes", and other terms to have meaning. I'll get there.
"this" is a pointer to the current instantiation of the class. Such a pointer is needed since classes can have several, independent instances while sharing the same logic. If you're familiar with Python, "this" == "self".
I'll take all the explanations I can get! Each time I get a little more out of it. I understand the instances of the class. I'm not understanding how the variable in the class gets targeted. I don't have time to explain further, I'm out of time. Right now I am reading a different tutorial covering classes. A different instructor explains things differently which can help. Thanks!
amdkt7:
I'm not understanding how the variable in the class gets targeted.
Although it isn't obvious, the compiler determines the "targeting" of the "this" pointer automatically.
Unless you want to learn about what's under the hood of a C++ compiler, it's easiest to just imagine your compiler figures out how to use "this" with magic
(The latter example is more like how you'd do it in C which doesn't have classes.)
The compiler passes a pointer to the object as a "hidden" argument to the function, and the function accesses that hidden argument by the name this. With button.press(), this (inside the press() function) is a pointer to button.
Is it just something I am overlooking, or is this part of the code also wrong?
int main()
{
Cheese *asiago = new Cheese();
Cheese *limburger = new Cheese();
This is a snippet from the first example I posted at the beginning.
The weird thing to me is the opening and closing () at the end of the statements creating new instances of Cheese. I tried removing the () pair and the code still works fine.
Is this a typo or is this a programming technique that the author is not explaining? I'm going with typo.
I have been looking at that page, I found it myself. It is not answering the question. The closest thing I see on that page is the section about Parameterized Constructor, but this example code is not using a parameterized constructor. Here is the code example again:
#include <iostream>
using namespace std;
class Cheese
{
public:
string status;
void eat();
void rot();
};
int CheeseCount;
void OneMoreCheeseGone(Cheese *Block)
{
CheeseCount--;
Block->status = "Gone";
}
void Cheese::eat()
{
cout <<"Eaten up!" << endl;
OneMoreCheeseGone(this);
}
void Cheese::rot()
{
cout <<"Rotted away!" << endl;
OneMoreCheeseGone(this);
}
int main()
{
Cheese *cheddar = new Cheese();//this is the lines that are confusing me
Cheese *swiss = new Cheese();//why is the () on the end? Code works the same with or without them.
CheeseCount = 2;
cheddar->eat();
swiss->rot();
cout << endl;
cout <<"Cheese count: " << CheeseCount << endl;
cout <<"cheddar: " << cheddar->status << endl;
cout <<"swiss: " << swiss->status << endl;
return 0;
}
I'm trying to understand this. I am using many resources including the forum. I am new at this and would like better hints, or better yet an answer to the question "Is this a typo, or does it make sense?"
You don't have a constructor explicitly defined, therefore the class uses the (hidden) default constructor with no parameters. Things might make more sense if you look at how a servo is instantiated via Servo.h. That's a good starting point.
Power_Broker:
You don't have a constructor explicitly defined, therefore the class uses the (hidden) default constructor with no parameters. Things might make more sense if you look at how a servo is instantiated via Servo.h. That's a good starting point.
Thanks. It was interesting to look at the servo.h file. It's not answering my question though. It's probably a good example to study and I will continue to look at it.
[quote author=dougp link=msg=4474157 date=1581472585]
[color=blue]this[/color] pointer demo
// this pointer compare
// https://www.tutorialspoint.com/cplusplus/cpp_this_pointer.htm
Thanks dougp,
I bookmarked this site, and I think I will spend sometime reviewing the Class and Objects section to help round out my understanding.