Thursday, July 13, 2006
STL
INTRO
-----------
Containers
----------
onedimensional array of T §16.3
INTRO
-----------
Containers
----------
- doublylinked list of T §17.2.2
Wednesday, July 12, 2006
Chapter11. exercise #1
using namespace std;
struct X {
int i;
// This does not utomatically convert an int to X. I was WRONG here.
X(int pi) : i(pi) {
std::cout << "i is " << pi << "\n";
}
X operator+(int j) // Adding int to class aka x+1 types... Must return another x or x&
{
cout << "j is " << j <<"\n";
return X(i+j);
}
};
struct Y {
int i;
// This does not automatically convert an X to Y. I was WRONG here.
Y(X x)
{
std::cout << "In y(x) ctor\n";
i = x.i;
}
Y operator+(X pX) // Adding x to class aka y+x types. Must return another y or y&
{
std::cout << "In y::operator+(x) with val " << pX.i << "\n";
return Y( X(i + pX.i) );
}
// Convert y to int
operator int() // Converrts y to int. Will return int even if the func sig doesn't say so..
{
return i;
}
};
extern X operator*(X pX, Y pY)
{
std::cout << "x is " << pX.i << " and y is " << pY.i << "\n";
return X(pX.i * pY.i);
}
extern int f(X pX)
{
std::cout << "In f(X)\n";
return pX.i;
}
/*
extern int f(int x)
{
std::cout << "In f(int)\n";
return x;
}
*/
X x = 1;
Y y = x;
int i = 2;
int main()
{
std::cout << "Before\n";
std::cout << "**" << i + 10; // Simple Integer Add
std::cout << "After\n";
std::cout << "Before\n";
x = x + 10; // Simple Integer Add
std::cout << "**" << x.i << "\n";
std::cout << "After\n";
std::cout << "Before\n";
//x = 10 + x; // Simple Integer Add not permitted..
std::cout << "**" << x.i << "\n";
std::cout << "After\n";
/*
std::cout << "Before\n";
y = y + 10;
std::cout << "**" << y.i << "\n";
std::cout << "After\n";
*/
/*
std::cout << "** Before\n";
//y =
10 + y;
std::cout << "**" << 10 + y << "\n";
std::cout << "After\n";
*/
//y + X(10); // y operator+(X)
//y + X(10) * y; // extern op followed by y operator+(X) ;
/*
std::cout << "Before\n";
x + y + i; // INVALID or x operator+(int) after y reduces to an int followed Anothr x operator+(int)
std::cout << "After\n";
std::cout << 106 + y; // x+y -> y + y -> y + x .. Actually it outputs an int :P
std::cout << "After\n";
*/
std::cout << "B4 x is " << x.i << " and i is " <x * x + i; // extern X operator*(X, Y) ; followed by x operator+(int) ;
std::cout << "After\n";
std::cout << "Since there is ctor conv between int and x. The following is valid\n";
f(7) ; // extern int f(X) ;
std::cout << "Since there is no conv from y to x, the following is invalid\n";
//f(y) ; // INVALID or f(y) to f(int) to f(X)
std::cout << "####### Before $$$$$$$$\n";
y + y; // y operator+(X)
std::cout << y + y << "\n";
std::cout << "GHJHHJJHJHn After *((**(()( \n";
return 0;
}
Tuesday, July 11, 2006
Operator Overloads
#include
using namespace std;
class X {
int mVal;
public:
void operator+(int) {
std::cout << " In x::operator+(int)\n";
}
X(int x) : mVal(x)
{
std::cout << "In X(int)\n";
}
};
void operator+(X,X)
{
std::cout << " In operator+(X,X)\n";
}
void operator+(X,double)
{
std::cout << " In operator+(X,double)\n";
}
void f(X a)
{
a+1; // a.operator+(1)
1+a; // ::operator+(X(1),a)
a+1.0; // ::operator+(a,1.0)
}
int main(void)
{
X x(10);
f(x);
return 0;
}
#include
using namespace std;
class X {
int mVal;
public:
void operator+(int) {
std::cout << " In x::operator+(int)\n";
}
X(int x) : mVal(x)
{
std::cout << "In X(int)\n";
}
};
void operator+(X,X)
{
std::cout << " In operator+(X,X)\n";
}
void operator+(X,double)
{
std::cout << " In operator+(X,double)\n";
}
void f(X a)
{
a+1; // a.operator+(1)
1+a; // ::operator+(X(1),a)
a+1.0; // ::operator+(a,1.0)
}
int main(void)
{
X x(10);
f(x);
return 0;
}
Saturday, July 08, 2006
Object Pool
===========
#include
#include
#include
using namespace std;
class Fred {
public:
static Fred* create(std::string name) throw(bad_alloc); //<-- 1
virtual void discard() throw(); //<-- 2
static bool flushPool() throw(); //<-- 3
std::string name() { return mName; }
private:
Fred(std::string name) throw(); //<-- 4
~Fred() throw(); //<-- 5
void init(std::string name) throw(); //<-- 6
Fred* nextRecycled_;
static Fred* headRecycled_;
std::string mName;
};
void Fred::init(std::string name) throw()
{
if (mName != name)
std::cout << "Old Name : " << mName << " New Name: " << name << "\n";
mName = name;
// ... //<-- 7
nextRecycled_ = NULL;
}
Fred::Fred(std::string name) throw() : mName(name)
{ init(name); }
Fred::~Fred() throw()
{ } //<-- 8
Fred* Fred::headRecycled_ = NULL;
Fred* Fred::create(std::string name) throw(bad_alloc)
{
// If there isn't any discarded entry in the list, create new one
if (headRecycled_ == NULL) {
std::cout << "Creating new Fred with name " << name << ".\n";
return new Fred(name);
}
// Recycle old entries..
Fred* p = headRecycled_;
headRecycled_ = headRecycled_->nextRecycled_;
p->init(name); // <-- 9
return p;
}
void Fred::discard() throw()
{
nextRecycled_ = headRecycled_;
headRecycled_ = this;
}
bool Fred::flushPool() throw()
{
bool stuffGotDeleted = (headRecycled_ != NULL);
// Check out this logic
while (headRecycled_ != NULL) {
std::cout << "Deleting " << headRecycled_->name() << "\n";
delete create(headRecycled_->name());
}
return stuffGotDeleted;
}
int main(void)
{
Fred* manu = Fred::create("Manu");
Fred* pooja = Fred::create("Pooja");
Fred* mummy = Fred::create("Mummy");
manu->discard();
Fred::flushPool();
return 0;
}
===========
#include
#include
#include
using namespace std;
class Fred {
public:
static Fred* create(std::string name) throw(bad_alloc); //<-- 1
virtual void discard() throw(); //<-- 2
static bool flushPool() throw(); //<-- 3
std::string name() { return mName; }
private:
Fred(std::string name) throw(); //<-- 4
~Fred() throw(); //<-- 5
void init(std::string name) throw(); //<-- 6
Fred* nextRecycled_;
static Fred* headRecycled_;
std::string mName;
};
void Fred::init(std::string name) throw()
{
if (mName != name)
std::cout << "Old Name : " << mName << " New Name: " << name << "\n";
mName = name;
// ... //<-- 7
nextRecycled_ = NULL;
}
Fred::Fred(std::string name) throw() : mName(name)
{ init(name); }
Fred::~Fred() throw()
{ } //<-- 8
Fred* Fred::headRecycled_ = NULL;
Fred* Fred::create(std::string name) throw(bad_alloc)
{
// If there isn't any discarded entry in the list, create new one
if (headRecycled_ == NULL) {
std::cout << "Creating new Fred with name " << name << ".\n";
return new Fred(name);
}
// Recycle old entries..
Fred* p = headRecycled_;
headRecycled_ = headRecycled_->nextRecycled_;
p->init(name); // <-- 9
return p;
}
void Fred::discard() throw()
{
nextRecycled_ = headRecycled_;
headRecycled_ = this;
}
bool Fred::flushPool() throw()
{
bool stuffGotDeleted = (headRecycled_ != NULL);
// Check out this logic
while (headRecycled_ != NULL) {
std::cout << "Deleting " << headRecycled_->name() << "\n";
delete create(headRecycled_->name());
}
return stuffGotDeleted;
}
int main(void)
{
Fred* manu = Fred::create("Manu");
Fred* pooja = Fred::create("Pooja");
Fred* mummy = Fred::create("Mummy");
manu->discard();
Fred::flushPool();
return 0;
}