Tuesday, December 31, 2019

MFC quick tutorial and some critical concepts

googled keyword "MFC quick tutorial" and gleaned the useful links for my future references.

    good video tutorials

  1. VC++/C++ MFC tutorial 1: Creating a Dialog box for user input
  2. VC++ MFC Basics - 1 - Installing Visual Studio
  3. VC++ MFC Basics - 2 - Project File Structure and VS Environment
  4. VC++ MFC Basics - 3 - Drawing
  5. VC++ MFC Basics - 4 - Using Menus , Menu command
  6. VC++ MFC Basics - 5 - Dialog box with SDI
  7. VC++ MFC Basics - 6 - New, Open, Save, Save As in SDI
  8. VC++ MFC Basics - 7 - Button (CButton) in a view
  9. VC++/C++ MFC tutorial for beginners - A Draw app View/Document (no voice)
  10. C++ programming tutorial: Functions part 1
  11. C++ programming tutorial: Functions part 2
  12. C++ programming tutorial: Functions part 3
  13. C++ programming tutorial: Functions part 4
  14. C++ programming tutorial: Functions part 5/5
  15. C++ Programming tutorial: Reference variable
  16. What is the Difference Between a Pointer and a Reference C++
  17. C++ Programming tutorial: characters (char -type)
  18. C++ Programming tutorial: C-type strings (array or characters)
  19. C++ Tutorial for Beginners - Classes and object 1
  20. C++ Tutorial for Beginners - Classes and object 2
  21. other concepts

  22. How to write classes and create objects in C++ (Episode 4)
  23. C++ Tutorial 29 - Reading and Writing to Files - fstream
  24. C++ Programming Tutorial - 14 - string Class
  25. 16. Strings:: MIT license
  26. C++ Tutorial 27: Multiple constructors.
  27. C++ Tutorial 20-1 - Classes and Object-Oriented Programming (Part 1): another teacher
  28. C++ Tutorial 20-2 - Classes and Object-Oriented Programming (Part 2):another teacher
  29. C++ Tutorial 11 : Classes and Objects in C++
  30. C++ Tutorial: Classes and Objects
  31. C++ Programming Tutorial 21: Passing class object as function parameter
  32. C++ 03 - The C++ Object Model
  33. Classes and Objects (Lecture 19)

  34. C++ Tutorial: Pass by value, reference and pointer
  35. What is the Difference Between Pass By Value, Pass By Reference, and Pass By Pointer, C++
  36. What is the Difference Between Pass By Pointer and Pass By Pointer Reference (int * and int * &) C++
  37. good notes

    Paul gave very clear explanation on these two topics. very good.


  38. 6.11 — Reference variables
  39. References are the third basic type of variable that C++ supports. A reference is a type of C++ variable that acts as an alias to another object or value. other two types of variable are:

    • Normal variables, which hold values directly.
    • Pointers, which hold the address of another value (or null) and can be dereferenced to retrieve the value at the address they point to.

    Note:: const variables are considered non-modifiable l-values.

    l-values and r-values:

    In C++, variables are a type of l-value (pronounced ell-value). An l-value is a value that has an address (in memory). Since all variables have addresses, all variables are l-values. The name l-value came about because l-values are the only values that can be on the left side of an assignment statement. When we do an assignment, the left hand side of the assignment operator must be an l-value. Consequently, a statement like 5 = 6; will cause a compile error, because 5 is not an l-value. The value of 5 has no memory, and thus nothing can be assigned to it. 5 means 5, and its value can not be reassigned. When an l-value has a value assigned to it, the current value at that memory address is overwritten.

    The opposite of l-values are r-values (pronounced arr-values). An r-value refers to any value that can be assigned to an l-value. r-values are always evaluated to produce a single value. Examples of r-values are literals (such as 5, which evaluates to 5), variables (such as x, which evaluates to whatever value was last assigned to it), or expressions (such as 2 + x, which evaluates to the value of x plus 2).

    References must be initialized when created.Unlike pointers, which can hold a null value, there is no such thing as a null reference.

    Unlike pointers, which can hold a null value, there is no such thing as a null reference.

    References to non-const values can only be initialized with non-const l-values. They can not be initialized with const l-values or r-values.

    References can not be reassigned. Once initialized, a reference can not be changed to reference another variable. Consider the following snippet:

    References and pointers have an interesting relationship -- a reference acts like a pointer that is implicitly dereferenced when accessed (references are usually implemented internally by the compiler using pointers). Thus given the following:

    • int value{ 5 };
    • int *const ptr{ &value };
    • int &ref{ value };

    *ptr and ref evaluate identically. As a result, the following two statements produce the same effect:

    • *ptr = 5;
    • ref = 5;

    Because references must be initialized to valid objects (cannot be null) and can not be changed once set, references are generally much safer to use than pointers (since there’s no risk of dereferencing a null pointer). However, they are also a bit more limited in functionality accordingly.

    If a given task can be solved with either a reference or a pointer, the reference should generally be preferred. Pointers should only be used in situations where references are not sufficient (such as dynamically allocating memory).

  40. 6.11a — References and const
  41. A reference to a const value is often called a const reference for short, though this does make for some inconsistent nomenclature with pointers.

    Unlike references to non-const values, which can only be initialized with non-const l-values, references to const values can be initialized with non-const l-value, const l-values, and r-values.

    Much like a pointer to a const value, a reference to a const value can reference a non-const variable. When accessed through a reference to a const value, the value is considered const even if the original variable is not:

    References used as function parameters can also be const. This allows us to access the argument without making a copy of it, while guaranteeing that the function will not change the value being referenced.

    References to const values are particularly useful as function parameters because of their versatility. A const reference parameter allows you to pass in a non-const l-value argument, a const l-value argument, a literal, or the result of an expression:

    #include void printIt(const int &x) { std::cout << x; } int main() { int a = 1; printIt(a); // non-const l-value const int b = 2; printIt(b); // const l-value printIt(3); // literal r-value printIt(2+b); // expression r-value return 0; }

    To avoid making unnecessary, potentially expensive copies, variables that are not pointers or fundamental data types (int, double, etc…) should be generally passed by (const) reference. Fundamental data types should be passed by value, unless the function needs to change them.

    Rule: Pass non-pointer, non-fundamental data type variables (such as structs) by (const) reference.

  42. 6.10 — Pointers and const
  43. To summarize, you only need to remember 4 rules, and they are pretty logical:

    • A non-const pointer can be redirected to point to other addresses.
    • A const pointer always points to the same address, and this address can not be changed.
    • A pointer to a non-const value can change the value it is pointing to. These can not point to a const value.
    • A pointer to a const value treats the value as const (even if it is not), and thus can not change the value it is pointing to.
    • Keeping the declaration syntax straight can be challenging. Just remember that the type of value the pointer points to is always on the far left:

  44. 6.12 — Member selection with pointers and references
  45. Note that the pointer dereference must be enclosed in parentheses, because the member selection operator has a higher precedence than the dereference operator.

    • struct Person
    • {
    • int age;
    • double weight;
    • };
    • Person person;
    • // Member selection using pointer to struct
    • Person *ptr = &person;
    • (*ptr).age= 5;

    Because the syntax for access to structs and class members through a pointer is awkward, C++ offers a second member selection operator (->) for doing member selection from pointers. The following two lines are equivalent:

    • (*ptr).age = 5;
    • ptr->age = 5;

    This is not only easier to type, but is also much less prone to error because the dereference is implicitly done for you, so there are no precedence issues to worry about. Consequently, when doing member access through a pointer, always use the -> operator instead of the . operator.

    Rule: When using a pointer to access the value of a member, use operator-> instead of operator. (the . operator)

  46. References in C++
  47. Pointers and References in C++
  48. Pointers vs References in C++
  49. C++ References


  50. C++ & MFC Part 1: README FIRST: good and complete materials
  51. Event Programming Event from Washington University
  52. Chapter Tutorials for Essentials of Interactive Computer Graphics from Washington University
  53. Creating Dialog Based Applications with MFC 7
  54. A Beginners Guide to Dialog Based Applications - Part One
  55. Beginners Guide to Dialog Based Applications - Part Two
  56. Windows Message Handling - Part 1
  57. Windows Message Handling - Part 2
  58. Windows Message Handling - Part 3
  59. Windows Message Handling - Part 4
  60. Exception Handling Best Practices in .NET
  61. Creating Your First Windows Application
  62. Using the List Control

No comments:

Post a Comment