CIS 29 - Notes for Tuesday, 1/12

Announcements and Reminders

  • Assignment 1 is due NOW
  • Attendance using Zoom waiting room
  • Office Hours:  Tuesday and Thursday, 12:45-1:15 pm on Zoom
  • Optional Online time tomorrow night 7:00-8:15 pm: review, SFML demo, introduction to SFML code and compilation
  • Project Group assignments will be announced next Tuesday

Recording

Comments on Assignment 1

Common mistakes
  • Word class destructor
  • Dictionary class destructor
  • Memory leak in resize function
Write good code
  • Accurate error messages (e.g. "file not found")
  • Formatting and eliminate distracting comments
  • Eliminate warnings
  • Stay away from C code
  • In CIS29, you are held to a higher standard
Other Issues
  • Submit only source code (not input files, output files, project files, ...)
  • What does "please resubmit" mean?
  • Ethics
Joe's mistake in the Word class constructor (from the online session)
Word::Word(const char* text)
: word(new char[strlen(text])
{
strcpy(word,text);
}

Loose Ends

Rvalue references

Rvalue references permits a reference to bind to a rvalue – a temporary or a literal.  This is useful for the move constructor or the move assignment operator, avoiding the expense of copying an object for this purpose.

Example 2-3 – Rvalue References
Move Semantics
With the use of rvalue references in C++11, the move constructor and the move assignment operator was added as a replacement for the copy constructor and the overloaded assignment operator.

Example 2-4 – Move Semantics

typedef and using

The keyword, typedef, originally from C, is used to define a type.

C++ 11 introduced the keyword, using to act like typedef.

typeid operator

The typeid operator returns an identifier of a type, a variable or an expression.  The return of the typeid is a class type, called type_info.  You can use the name() member function of the type_info class to display a literal description of the type.

Example 1-8 – typedef, using, typeid

The generic size function

The generic size function was introduced in C++17.  It returns the size of an array (number of elements) or the size of a C++ container;

Example 2-6 - The size function

C++ Cast Operators

Static Cast

A static_cast is used to convert one type to another.  Generally, static_casts should not be used and may be considered dangerous.  Often, conversions are automatic and the need to use a static_cast may imply that incorrect types are in use.  Static_cast are often used to convert one pointer type to another, or to convert an enumerated value to an integer value.

Example 4-1 – static_cast

Const Cast

A const_cast is used to add or remove constness to an expression.  Note, removing constness from a “pointed to” value may result in undefined behavior.

Example 4-2 – const_cast

Reinterpret Cast

A reinterpret_cast is used to cast one type to another.  It is most commonly used to treat one pointer type as another pointer type, or to treat a pointer type as an integer type and vice versa.  Note, this case type may be unsafe and to use it effectively, the sizes of the casted value and the casted type should match.

Example 4-3 – reinterpret_cast

Dynamic Cast

A dynamic_cast is used with inheritance to cast a base class pointer or reference to a derived class pointer or references.  This is called downcasting.  In order for this to succeed, the base class must be polymorphic (contains a virtual function).

Reference:  http://www.bogotobogo.com/cplusplus/upcasting_downcasting.php

Example 4-4 – dynamic_cast