Presentation is loading. Please wait.

Presentation is loading. Please wait.

This.

Similar presentations


Presentation on theme: "This."— Presentation transcript:

1 this

2 Outline In this lesson, we will:
Describe issues with the primitive data types Introduce the 3-body problem and an attempt to solve it Introduce the struct keyword and member variables Create a 3-dimensional vector data structure Describe assigning to and using the member variables Describe passing instances of data structures as arguments We will create a library of functions for vectors Initialize instances of data structures Revisit our 3-body problem Determine if we have a solution to arrays

3 Member variables To this point, we have called member functions:
double Vector_3d::norm() const { return std::sqrt( x*x + y*y + z*z ); } Vector_3d Vector_3d::operator*( double s ) { return Vector_3d{ x*s, y*s, z*s };

4 Address Suppose we wrote a function: We can access the vector itself:
double norm( Vector_3d const &v ) const { return std::sqrt( v.x*v.x + v.y*v.y + v.z*v.z ); } We can access the vector itself: std::cout << "The address of 'v' is " << &v << std::endl; Vector_3d *p_v = &v; return std::sqrt( p_v->x*p_v->x + p_v->y*p_v->y + p_v->z*p_v->z ); Pass by reference: v is an alias for the argument The address of v

5 this as an implied parameter
Each member function has an implied parameter The name of the implied parameter is this and it is assigned the address of the object on which the member function is called double Vector_3d::norm() const { std::cout << "The address of 'this' is " << this << std::endl; return std::sqrt( x*x + y*y + z*z ); }

6 this as an implied parameter
double Vector_3d::norm() const { std::cout << "The address of 'this' is " << this << std::endl; return std::sqrt( x*x + y*y + z*z ); } You can now see that this works: int main() { Vector_3d v{3.2, 2.7, 5.9}; std::cout << "The address of 'v' is " << &v << std::endl; std::cout << v.norm() << std::endl; return 0; } Output: The address of 'v' is 0xff387a98cb8 The address of 'this' is 0xff387a98cb8 52.34

7 this as an implied parameter
Thus, if this is a pointer to this object, *this is the object itself Vector_3d &Vector_3d::operator*=( double s ) { x *= s; y *= s; z *= s; return *this; } Vector_3d &Vector_3d::operator/=( double s ) { x /= s; y /= s; z /= s;

8 this as an identifier Remember that your uWaterloo Student Number is unique: It is a number that uniquely identifies you In another sense, the address of an object is a unique identifier unsigned long Vector_3d::address() const { // Reinterpret the address as if it was an 'unsigned long' return reinterpret_cast<unsigned long>( this ); } unsigned long Vector_3d::hash() const { unsigned long hash_value{reinterpret_cast<unsigned long>( this )}; return *hash_value; A very large prime number

9 this for communication
Instances of this class can store an address of another instance: class Messenger { public: Messenger(); Messenger( Messenger &partner ); bool send( int msg ); // Send a 'message' to the partner int receive(); // Receive a 'message' from the partner private: Messenger *p_partner; int message; bool received; };

10 } this for communcation
A messenger created without an argument has no partner Messenger::Messenger(): p_partner{ nullptr }, // No partner message{ 0 }, received{ false } { // empty constructor } int main() { Messenger first{}; 0xffe328a0 0x p_partner } first message false received

11 this for communcation A messenger created with a another messenger links each to each other Messenger::Messenger( Messenger &partner ): p_partner{ &partner }, // Store the address of my partner message{ 0 }, received{ false } { // If the partner already has a partner, 'unfriend' that // partner first--otherwise, we'll be in trouble... if ( partner.p_partner != nullptr ) { partner.p_partner->p_partner = nullptr; } partner.p_partner = this; // Let the partner know about me

12 } this for communcation Let’s work this one out: 0xffe32880 0xffe328a0
Messenger::Messenger( Messenger &partner ): p_partner{ &partner }, // Store the address of my partner message{ 0 }, received{ false } { // If the partner already has a partner, 'unfriend' that // partner first--otherwise, we'll be in trouble... if ( partner.p_partner != nullptr ) { partner.p_partner->p_partner = nullptr; } partner.p_partner = this; // Let the partner know about me int main() { Messenger first{}; Messenger second{ first }; 0xffe32880 0xffe328a0 p_partner } second false 0x first message received

13 } this for communcation Let’s work this one out: 0xffe32880 0xffe328a0
Messenger::Messenger( Messenger &partner ): p_partner{ &partner }, // Store the address of my partner message{ 0 }, received{ false } { // If the partner already has a partner, 'unfriend' that // partner first--otherwise, we'll be in trouble... if ( partner.p_partner != nullptr ) { partner.p_partner->p_partner = nullptr; } partner.p_partner = this; // Let the partner know about me int main() { Messenger first{}; Messenger second{ first }; 0xffe32880 0xffe328a0 p_partner } second false first message received

14 } this for communcation After both constructors are called:
first has the address of second and vice versa 0xffe32880 0xffe328a0 p_partner } second false first message received

15 Sending a message The member function send(…):
Checks if there is a partner If there is a partner, it checks if the partner a pending message If the partner has no pending message, save the message and flag that a message has been received bool Messenger::send( int msg ) { if ( (p_partner == nullptr) || (p_partner->received) ) { // Message not sent return false; } else { p_partner->message = msg; p_partner->received = true; // Message sent return true; } Recall: short-circuit evaluation! Important: p_partner is the address of an instance of this class, so this member function can modify its member variables

16 Sending a message To see what happens, let second send a message to first: int main() { Messenger first{}; Messenger second{ first }; second.send( 42 ); second.send( 616 ); 0xffe32880 0xffe328a0 p_partner } second false first message received

17 Sending a message First, we check if the partner exists and can receive a message int main() { Messenger first{}; Messenger second{ first }; second.send( 42 ); second.send( 616 ); 0xffe32880 0xffe328a0 p_partner } second false first message received bool Messenger::send( int msg ) { if ( (p_partner == nullptr) || (p_partner->received) ) { // Message not sent return false; } else { p_partner->message = msg; p_partner->received = true; // Message sent return true; }

18 Sending a message Next, we copy the message over and set received to true int main() { Messenger first{}; Messenger second{ first }; second.send( 42 ); second.send( 616 ); 0xffe32880 0xffe328a0 p_partner } second false first 42 message true received bool Messenger::send( int msg ) { if ( (p_partner == nullptr) || (p_partner->received) ) { // Message not sent return false; } else { p_partner->message = msg; p_partner->received = true; // Message sent return true; }

19 Sending a message If we try to send a second message, the conditional fails: int main() { Messenger first{}; Messenger second{ first }; second.send( 42 ); second.send( 616 ); 0xffe32880 0xffe328a0 p_partner } second false first 42 message true received bool Messenger::send( int msg ) { if ( (p_partner == nullptr) || (p_partner->received) ) { // Message not sent return false; } else { p_partner->message = msg; p_partner->received = true; // Message sent return true; }

20 Receiving a message The member function receive():
If a message was received, received is reset and the message is returned Otherwise, we return 0 indicating no message int Messenger::receive() { if ( received ) { received = false; return message; } else { return 0; }

21 } Receiving a message Let us get first to retrieve the message
int main() { Messenger first{}; Messenger second{ first }; second.send( 42 ); second.send( 616 ); std::cout << first.receive() << std::endl; return 0; } 0xffe32880 0xffe328a0 p_partner } second false first 42 message true received

22 } Receiving a message A message is received… int main() {
Messenger first{}; Messenger second{ first }; second.send( 42 ); second.send( 616 ); std::cout << first.receive() << std::endl; return 0; } 0xffe32880 0xffe328a0 p_partner } second false first 42 message true received int Messenger::receive() { if ( received ) { received = false; return message; } else { return 0; }

23 Receiving a message received is reset and the message is returned and printed int main() { Messenger first{}; Messenger second{ first }; second.send( 42 ); second.send( 616 ); std::cout << first.receive() << std::endl; return 0; } 0xffe32880 0xffe328a0 p_partner } second false first 42 message received int Messenger::receive() { if ( received ) { received = false; return message; } else { return 0; } Output: 42

24 Receiving a message If we try again, received is false so 0 is returned int main() { Messenger first{}; Messenger second{ first }; second.send( 42 ); second.send( 616 ); std::cout << first.receive() << std::endl; return 0; } 0xffe32880 0xffe328a0 p_partner } second false first 42 message received int Messenger::receive() { if ( received ) { received = false; return message; } else { return 0; } Output: 42

25 Communication Let’s try a game: Given a pair of partners,
For twenty-five times: Randomly choose one of the two partners to either send or receive Display the results

26 Communication int main() { Messenger A{}; // 'A' has no partner at this point Messenger B{A}; // 'A' and 'B' are now partnered up for ( int k{1}; k <= 25; ++k ) { int msg{}; int choice{rand() % 4}; if ( choice == 0 ) { std::cout << "A sending" << k << std::endl; if ( !A.send( k ) ) { std::cout << " - message not sent..." << std::endl; } } else if ( choice == 1 ) { std::cout << "B sending" << k << std::endl; if ( !B.send( k ) ) {

27 Communication } else if ( choice == 2 ) { msg = A.receive(); if ( msg == 0 ) { std::cout << " - not received..." << std::endl; } else { std::cout << " - received " << msg << std::endl; } assert( choice == 3 ); std::cout << "B receiving:" << std::endl; msg = B.receive(); return 0;

28 Communication B receiving: - not received... A receiving: B sending 3 B sending 5 - message not sent... - received 3 A sending 8 B sending 9 B sending 10 - received 9 - received 8 B receiving: - not received... A receiving: A sending 17 A sending 19 - message not sent... A sending 20 - received 17 A sending 22 - received 22 B sending 24 - received 24

29 Question Does this class need a destructor? Why?
Suppose one partner goes out of scope or is deleted If the partner is deleted, the address assigned to partner is now a dangling pointer Thus, when a messenger is deleted, its partnership should be ended Messenger::~Messenger() { if ( p_partner != nullptr ) { // Your partner's pointer to a partner is now // set to 'nullptr' p_partner->p_partner = nullptr; }

30 Summary Following this lesson, you now Understand the this keyword
Know that it stores the address of the object being called Understand that it can be used to: Identify the object Allow another object to know this object’s address

31 References [1] Wikipedia

32 Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see for more information.

33 Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.


Download ppt "This."

Similar presentations


Ads by Google