Pointer Usage in Multi-Paradigm C++ Programming

e81 cse 428s l.w
1 / 5
Embed
Share

Learn how to effectively utilize pointers in C++ for accessing different data members and member functions of a class, enabling deferred invocation and controlled encapsulation violation. Explore the use of pointers to data members and member functions for enhanced programming flexibility.

  • C++
  • Pointers
  • Programming
  • OOP
  • Encapsulation

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. E81 CSE 428S: Multi-Paradigm Programming in C++ Pointer to Class Member Department of Computer Science & Engineering Washington University, St. Louis MO Chris Gill cdgill@wustl.edu 1

  2. Pointers to Data Members When to use pointers to data members To access different members of a class, which have the same type (e.g., strings for last name, first name, student ID, etc.) and then apply that access to different objects (e.g., printing out a tabular roster of all the students in a course) How to use pointers to data members Do not initialize them to point to any data (yet), but rather initialize each one to point to a particular member of a class They then can be dereferenced to fetch a particular member of a particular object (e.g., theobject.*pmember) Note that derived class member pointers can point to members that were inherited from base classes! Public functions returning pointers to data members Lets you smuggle out pointers to private/protected members Provide controlled violations of encapsulation of private or protected member variables without (yet) accessing an object These serve as a deferred form of accessor, when applied CSE 428S Multi-Paradigm Programming in C++ 2

  3. Pointers to Member Functions When to use pointers to member functionss For deferred invocation of member functions (on objects that are selected at run-time) How to use pointers to member functions As with pointers to data members, first declare a pointer to the appropriate member function of a class type Then, combine a dereference of the pointer with the member selection operator on a particular object: (obj.*ptr)() Type aliases for member pointers Used as syntactic sugar to capture complex expressions Tables of pointers to member functions Many C++ compilers use v-tables to implement polymorphism Compiler may generate specific code to resolve some virtual functions at run-time (see LLM pp. 604-605) CSE 428S Multi-Paradigm Programming in C++ 3

  4. Member Functions as Callable Objects We ve looked at how to use the standard function template to wrap a non-member function (in Studio 4) For member functions, take a (const if the function is const or non-const otherwise) reference to a class object in signature Pass an object into a call to it to invoke the member function The mem_fn template does some of the work for us It lets us use auto to generate the appropriate type of callable object based on information the compiler already has The bind template also can be used to generate a callable object from a member function It lets us chain callable objects, member functions, etc. as for plain-old-C functions in the functional programming studio CSE 428S Multi-Paradigm Programming in C++ 4

  5. Studio 9 Use pointers to data members to access public data members of derived and base class objects Use public static functions that return pointers to data members, for access to private members of derived and base class objects Use pointers to member functions to invoke them on objects of derived and base class types Explore different ways to produce callable objects from pointers to member functions (via the standard function and mem_fn templates) Studios 0 through 11 are due 11:59pm Monday October 16th (the night before Exam 0) Submit as soon as each is done so you get feedback and can resubmit any that may be marked incomplete CSE 428S Multi-Paradigm Programming in C++ 5

Related


More Related Content