Tuesday, 5 June 2018

Chapter 9 // Exercise 4 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


Chapter 9 // Exercise 4


Look at the headache-inducing last example of section 8.4. Indent it properly and explain the meaning of each construct. Note that the example does do anything meaningful; it is pure obfuscation.

#include "stdafx.h"
#include "std_lib_facilities.h"

//just ew
struct X
{
 //strangely returns 1 no matter what
 void f(int x)
 {
  struct Y
  {
   int f() { return 1; }
   int m;
  };

  int m;
  m = x; 
  Y m2;
  return f(m2.f());
 }

 int m;
 //if m, return 3 if not return whatever is in m + 2
 void g(int m)
 {
  if (m)
   f(m + 2);
  else
  {
   g(m + 2);
  }
 }

 X() {} //default constructor for X struct
 void m3() {} //define and declare m3, basically does nothing
 void main()
 {
  X a;
  a.f(2);  //return 1
 }

};

This code is so confusing that even though it doesn't do anything it still made me question myself. 

No comments:

Post a Comment