mirror of https://github.com/CppCon/CppCon2014.git
Add lightning talks.
parent
76cb5c94d4
commit
98f6272690
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
|
||||
int main() // Part I: Legal, or compile error?
|
||||
{ // LEGAL!
|
||||
using namespace std;
|
||||
|
||||
cout << "hello, cruel world!" << endl; // usual "cout"
|
||||
|
||||
int cout; // this cout HIDES the one at namespace scope
|
||||
cout = 10; // uses the int
|
||||
std::cout << "cout = " << cout << endl; // 2nd cout is the int
|
||||
std::cout << "cout << 5 = " << (cout << 5) << endl; // 2nd cout is int
|
||||
}
|
||||
|
||||
|
||||
// Part II: What if we replaced
|
||||
// using namespace std;
|
||||
// with:
|
||||
// using std::cout;
|
||||
// using std::endl;
|
||||
//
|
||||
// ?
|
||||
|
||||
// Answer: ILLEGAL! A using DECLARATION brings the name into THIS scope,
|
||||
// so you can't have a redeclaration in the same scope.
|
||||
@ -0,0 +1,24 @@
|
||||
// NOTE: There are TWO PARTS to this puzzler!
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main() // Part I: Legal, or compile error?
|
||||
{
|
||||
using namespace std;
|
||||
|
||||
cout << "Hello, puzzling world!" << endl;
|
||||
|
||||
int cout;
|
||||
cout = 10;
|
||||
std::cout << "cout = " << cout << endl;
|
||||
std::cout << "cout << 5 = " << (cout << 5) << endl;
|
||||
}
|
||||
|
||||
|
||||
// Part II: What if we replaced
|
||||
// using namespace std;
|
||||
// with:
|
||||
// using std::cout;
|
||||
// using std::endl;
|
||||
//
|
||||
// ?
|
||||
@ -0,0 +1,33 @@
|
||||
//
|
||||
// Morning puzzler: see last comment (please don't reveal answer
|
||||
// until everyone has had a chance to think about it...)
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Base
|
||||
{
|
||||
public:
|
||||
Base() {}
|
||||
virtual void func()
|
||||
{
|
||||
cout << "Base's func() now running\n";
|
||||
}
|
||||
};
|
||||
|
||||
class Derived : public Base
|
||||
{
|
||||
public:
|
||||
Derived() {}
|
||||
void func()
|
||||
{
|
||||
Base:func(); // this is a LABEL!
|
||||
cout << "Derived's func() now running\n";
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Base *bp = new Derived;
|
||||
bp->func(); // What's the run-time result?
|
||||
} // Result is infinite recursion and crash.
|
||||
@ -0,0 +1,30 @@
|
||||
// Puzzler: see comment at bottom for the question
|
||||
// (PLEASE don't say anything about the REASON, just answer the question!)
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Base
|
||||
{
|
||||
public:
|
||||
virtual void func()
|
||||
{
|
||||
cout << "Base's func() now running\n";
|
||||
}
|
||||
};
|
||||
|
||||
class Derived : public Base
|
||||
{
|
||||
public:
|
||||
void func()
|
||||
{
|
||||
Base:func();
|
||||
cout << "Derived's func() now running\n";
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Base *bp = new Derived;
|
||||
bp->func(); // Question: What's the runtime behavior?
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
// T.h:
|
||||
// Which do think is best?
|
||||
//
|
||||
// ANSWER: I personally prefer option #3, because the INTERFACE
|
||||
// should not traffic in IMPLEMENTATION DETAILS...and whether
|
||||
// any particular function is inline or not is NOT part of
|
||||
// the interface a client has any need to know or care about.
|
||||
|
||||
#include <iostream>
|
||||
using std::cout;
|
||||
|
||||
class T
|
||||
{
|
||||
public:
|
||||
void foo()
|
||||
{
|
||||
cout << "foo()\n"; // option #1: "in-situ"
|
||||
}
|
||||
|
||||
inline void bar(); // option #2
|
||||
|
||||
void zot(); // option #3
|
||||
};
|
||||
|
||||
void T::bar() { cout << "bar()\n"; } // option #2
|
||||
|
||||
inline void T::zot() {
|
||||
cout << "zot()\n"; } // option #3
|
||||
@ -0,0 +1,29 @@
|
||||
// Not a "puzzler" in the usual sense, but worth thinking about:
|
||||
// Inline member functions should always be defined in
|
||||
// header files...but there are 3 totally equivalent (and legal)
|
||||
// ways to do this, syntactically.
|
||||
//
|
||||
// Are there any reasons to prefer one style above another?
|
||||
// (THERE IS NO DIFFERENCE in semantics or performance!)
|
||||
//
|
||||
|
||||
#include <iostream> // Note: This is ALL in a header file
|
||||
using std::cout;
|
||||
|
||||
class T
|
||||
{
|
||||
public:
|
||||
void foo()
|
||||
{
|
||||
cout << "foo()\n"; // option #1: "in-situ" (complete)
|
||||
}
|
||||
|
||||
inline void bar(); // option #2 (declaration)
|
||||
|
||||
void zot(); // option #3 (declaration)
|
||||
};
|
||||
|
||||
void T::bar() { cout << "bar()\n"; } // option #2 (definition)
|
||||
|
||||
inline void T::zot() {
|
||||
cout << "zot()\n"; } // option #3 (definition)
|
||||
@ -0,0 +1,18 @@
|
||||
// Display "hello, world" without using any semi-colons
|
||||
// in your program
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
if (std::cout << "Hello, World") {}
|
||||
}
|
||||
|
||||
|
||||
// In C:
|
||||
|
||||
int main()
|
||||
{
|
||||
if (printf("Hello, world")) {}
|
||||
}
|
||||
|
||||
@ -0,0 +1,12 @@
|
||||
Puzzler:
|
||||
|
||||
Write a portable C++ (and/or a C) program to display
|
||||
|
||||
Hello, World
|
||||
|
||||
on the standard output--
|
||||
|
||||
WITHOUT USING *ANY* SEMICOLONS IN YOUR CODE.
|
||||
|
||||
(Do not worry about what's in Standard header files.)
|
||||
|
||||
@ -0,0 +1,41 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class A
|
||||
{
|
||||
public:
|
||||
A (int i) {}
|
||||
};
|
||||
|
||||
class B {
|
||||
public:
|
||||
B (A a) {}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
int i = 1;
|
||||
B b(A(i)); // ******: declaration of function: B b(A);
|
||||
|
||||
b(A(5)); // Call the function
|
||||
}
|
||||
|
||||
B b(A a) // And here's an implementation...
|
||||
{
|
||||
cout << "b() called!\n";
|
||||
return B(a);
|
||||
}
|
||||
|
||||
// This analysis explains why the compiler sees that as a func. decl.:
|
||||
|
||||
int f(int x); // a function taking an int
|
||||
int f(int (x)); // redundant parentheses around parameter name OK; still
|
||||
// a declaration of a function f, not an int initialized by
|
||||
// static_cast<int>(x)
|
||||
|
||||
|
||||
B b(A i); // a function taking an A
|
||||
B b(A (i)); // redundant parentheses around parameter name OK; still
|
||||
// a declaration of a function b, not a B object initialized by
|
||||
// an A constructed from i.
|
||||
|
||||
@ -0,0 +1,24 @@
|
||||
//
|
||||
// Puzzler
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class A
|
||||
{
|
||||
public:
|
||||
A (int i) {}
|
||||
};
|
||||
|
||||
class B {
|
||||
public:
|
||||
B (A a) {}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
int i = 1;
|
||||
B b(A(i)); // what is b's type?
|
||||
}
|
||||
|
||||
@ -0,0 +1,16 @@
|
||||
To all attendees of my 2014 15-minute "C++ Puzzlers" Lightgning Talk:
|
||||
|
||||
Thanks so much for attending the Lightning Talk!
|
||||
|
||||
I thought you might appreciate having the Puzzlers with my
|
||||
little soltions attached, so I'm putting this together.
|
||||
|
||||
I had up to 5 to present if time permitted, but it didn't (as
|
||||
I pretty much expected). I'm adding them to the archive just
|
||||
for the heck of it.
|
||||
|
||||
Enjoy,
|
||||
-leor
|
||||
www.bdsoft.com
|
||||
(On-Site C++ Training, really just in C++ these days)
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue