forked from ksinkar/ticpp2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The sourcecode for Thinking in C++ (Volume 2)
- Loading branch information
0 parents
commit 2b4f16c
Showing
518 changed files
with
27,264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CPP = bcc32 | ||
CPPFLAGS = -w-inl | ||
OFLAGS = -c | ||
EXEFLAG = -e | ||
OBJEXT = obj | ||
LIBLINK = -l | ||
.cpp.$(OBJEXT) : | ||
$(CPP) $(CPPFLAGS) $(OFLAGS) $< -o $@ | ||
.$(OBJEXT).exe : | ||
$(CPP) $(CPPFLAGS) $< $(EXEFLAG)$@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//: C01:Auto_ptr.cpp | ||
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. | ||
// (c) 1995-2004 MindView, Inc. All Rights Reserved. | ||
// See source code use permissions stated in the file 'License.txt', | ||
// distributed with the code package available at www.MindView.net. | ||
// Illustrates the RAII nature of auto_ptr. | ||
#include <memory> | ||
#include <iostream> | ||
#include <cstddef> | ||
using namespace std; | ||
|
||
class TraceHeap { | ||
int i; | ||
public: | ||
static void* operator new(size_t siz) { | ||
void* p = ::operator new(siz); | ||
cout << "Allocating TraceHeap object on the heap " | ||
<< "at address " << p << endl; | ||
return p; | ||
} | ||
static void operator delete(void* p) { | ||
cout << "Deleting TraceHeap object at address " | ||
<< p << endl; | ||
::operator delete(p); | ||
} | ||
TraceHeap(int i) : i(i) {} | ||
int getVal() const { return i; } | ||
}; | ||
|
||
int main() { | ||
auto_ptr<TraceHeap> pMyObject(new TraceHeap(5)); | ||
cout << pMyObject->getVal() << endl; // Prints 5 | ||
} ///:~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//: C01:Autoexcp.cpp | ||
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. | ||
// (c) 1995-2004 MindView, Inc. All Rights Reserved. | ||
// See source code use permissions stated in the file 'License.txt', | ||
// distributed with the code package available at www.MindView.net. | ||
// No matching conversions. | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class Except1 {}; | ||
|
||
class Except2 { | ||
public: | ||
Except2(const Except1&) {} | ||
}; | ||
|
||
void f() { throw Except1(); } | ||
|
||
int main() { | ||
try { f(); | ||
} catch(Except2&) { | ||
cout << "inside catch(Except2)" << endl; | ||
} catch(Except1&) { | ||
cout << "inside catch(Except1)" << endl; | ||
} | ||
} ///:~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//: C01:BadException.cpp {-bor} | ||
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. | ||
// (c) 1995-2004 MindView, Inc. All Rights Reserved. | ||
// See source code use permissions stated in the file 'License.txt', | ||
// distributed with the code package available at www.MindView.net. | ||
#include <exception> // For std::bad_exception | ||
#include <iostream> | ||
#include <cstdio> | ||
using namespace std; | ||
|
||
// Exception classes: | ||
class A {}; | ||
class B {}; | ||
|
||
// terminate() handler | ||
void my_thandler() { | ||
cout << "terminate called" << endl; | ||
exit(0); | ||
} | ||
|
||
// unexpected() handlers | ||
void my_uhandler1() { throw A(); } | ||
void my_uhandler2() { throw; } | ||
|
||
// If we embed this throw statement in f or g, | ||
// the compiler detects the violation and reports | ||
// an error, so we put it in its own function. | ||
void t() { throw B(); } | ||
|
||
void f() throw(A) { t(); } | ||
void g() throw(A, bad_exception) { t(); } | ||
|
||
int main() { | ||
set_terminate(my_thandler); | ||
set_unexpected(my_uhandler1); | ||
try { | ||
f(); | ||
} catch(A&) { | ||
cout << "caught an A from f" << endl; | ||
} | ||
set_unexpected(my_uhandler2); | ||
try { | ||
g(); | ||
} catch(bad_exception&) { | ||
cout << "caught a bad_exception from g" << endl; | ||
} | ||
try { | ||
f(); | ||
} catch(...) { | ||
cout << "This will never print" << endl; | ||
} | ||
} ///:~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//: C01:Basexcpt.cpp | ||
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. | ||
// (c) 1995-2004 MindView, Inc. All Rights Reserved. | ||
// See source code use permissions stated in the file 'License.txt', | ||
// distributed with the code package available at www.MindView.net. | ||
// Exception hierarchies. | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class X { | ||
public: | ||
class Trouble {}; | ||
class Small : public Trouble {}; | ||
class Big : public Trouble {}; | ||
void f() { throw Big(); } | ||
}; | ||
|
||
int main() { | ||
X x; | ||
try { | ||
x.f(); | ||
} catch(X::Trouble&) { | ||
cout << "caught Trouble" << endl; | ||
// Hidden by previous handler: | ||
} catch(X::Small&) { | ||
cout << "caught Small Trouble" << endl; | ||
} catch(X::Big&) { | ||
cout << "caught Big Trouble" << endl; | ||
} | ||
} ///:~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//: C01:Cleanup.cpp | ||
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. | ||
// (c) 1995-2004 MindView, Inc. All Rights Reserved. | ||
// See source code use permissions stated in the file 'License.txt', | ||
// distributed with the code package available at www.MindView.net. | ||
// Exceptions clean up complete objects only. | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class Trace { | ||
static int counter; | ||
int objid; | ||
public: | ||
Trace() { | ||
objid = counter++; | ||
cout << "constructing Trace #" << objid << endl; | ||
if(objid == 3) throw 3; | ||
} | ||
~Trace() { | ||
cout << "destructing Trace #" << objid << endl; | ||
} | ||
}; | ||
|
||
int Trace::counter = 0; | ||
|
||
int main() { | ||
try { | ||
Trace n1; | ||
// Throws exception: | ||
Trace array[5]; | ||
Trace n2; // Won't get here. | ||
} catch(int i) { | ||
cout << "caught " << i << endl; | ||
} | ||
} ///:~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//: C01:Covariance.cpp {-xo} | ||
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. | ||
// (c) 1995-2004 MindView, Inc. All Rights Reserved. | ||
// See source code use permissions stated in the file 'License.txt', | ||
// distributed with the code package available at www.MindView.net. | ||
// Should cause compile error. {-mwcc}{-msc} | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class Base { | ||
public: | ||
class BaseException {}; | ||
class DerivedException : public BaseException {}; | ||
virtual void f() throw(DerivedException) { | ||
throw DerivedException(); | ||
} | ||
virtual void g() throw(BaseException) { | ||
throw BaseException(); | ||
} | ||
}; | ||
|
||
class Derived : public Base { | ||
public: | ||
void f() throw(BaseException) { | ||
throw BaseException(); | ||
} | ||
virtual void g() throw(DerivedException) { | ||
throw DerivedException(); | ||
} | ||
}; ///:~ |
Oops, something went wrong.