Friday, July 31, 2009

Polymorphic on Smar Pointer

Polymorphic on smart pointer is allow.

Staff : class Human

shared_ptr(human) sph;
shared_ptr(staff) sps;

list(shared_ptr(human) ) cont;

cont.push(sph); -> OK
cont.push(sps); -> OK


Monday, July 20, 2009

Named Parameter Idiom

C++ supports only positional parameters and not parameters mapping.


Solution:

The idea, called the Named Parameter Idiom, is to change the function's parameters to methods of a newly created class, where all these methods return *this by reference. Then you simply rename the main function into a parameterless "do-it" method on that class.

Wraps all parameter into member function of a class and its required parameters as private member.


class File;



class OpenFile {

public:

OpenFile(const std::string& filename);

// sets all the default values for each data member

OpenFile& readonly(); // changes readonly_ to true

OpenFile& readwrite(); // changes readonly_ to false

OpenFile& createIfNotExist();

OpenFile& blockSize(unsigned nbytes);

...

private:

friend class File;

std::string filename_;

bool readonly_; // defaults to false [for example]

bool createIfNotExist_; // defaults to false [for example]

...

unsigned blockSize_; // defaults to 4096 [for example]

...

};



inline OpenFile::OpenFile(const std::string& filename)

: filename_ (filename)

, readonly_ (false)

, createIfNotExist_ (false)

, blockSize_ (4096u)

{ }



inline OpenFile& OpenFile::readonly()

{ readonly_ = true; return *this; }



inline OpenFile& OpenFile::readwrite()

{ readonly_ = false; return *this; }



inline OpenFile& OpenFile::createIfNotExist()

{ createIfNotExist_ = true; return *this; }



inline OpenFile& OpenFile::blockSize(unsigned nbytes)

{ blockSize_ = nbytes; return *this; }

class File {

public:

File(const OpenFile& params);

...

};

Construct on First Use Idiom

You need to control the evaluation order of static initialization.

This approach solved the problem.


static Fred& X()
{
static Fred* obj = new Fred();
return *obj;
}

Named Constructor Idiom

you would like to overload constructor on same type argument.

class Rational
{
private:
int x, y;

static Rational rectangular(int, theX, theY)
{
return Rational(theX, theY);
}

static Rational polar(int userX, int userY)
{
return Rational(userX * angle, userY * angle);
}
};

Iteration

Instead of advance iterator with a big step in loop, advance one step ahead each time.

Wrong:
while (LoginIte != loginResult.end())
{
loginResult.insert(LoginIte + 2, "\n");

if (LoginIte + 3 != loginResult.end())
{
LoginIte += 3;
}
else
{
break;
}


Correct:
while (iter != loginResult.end() && ++iter != loginResult.end())
{
++iter;
iter = loginResult.insert(iter, "\n");
++iter;
}

Invalid Iterator

Iterator become invalid after all STL container erase or insert.

Thursday, July 9, 2009

Playing Game .........

AIX && Sparc 1085 32MB

............

Validation Logic ...... TL

bool Human::IsNotAllDigit(const std::vector& result)
{
bool flag(false);
size_t MAX = result.size();

for (size_t loop = 0; loop < MAX;++loop)
{
if (result[loop] == 0)
{
flag = true;
}
}

return flag;
}

Negate of true become false. T....

Wednesday, July 8, 2009

HK

Makiyo, Livana. E

Tuesday, July 7, 2009

Traits

#ifndef EDGETYPETRAITS_H
#define EDGETYPETRAITS_H

// General
template
class edgeTypeTraits;
// =============================================
// Edge Specialization
template <>
class edgeTypeTraits
{
public:
typedef edge edgeType;
};
// =============================================
// Arcs Specialization
template <>
class edgeTypeTraits
{
public:
typedef arcs edgeType;
};
// =============================================
#endif


#ifndef VERTEXTYPETRAITS_H
#define VERTEXTYPETRAITS_H

class edge;
class arcs;

template
class vertex;


// Base class no definition
template
class vertexTypeTraits;


// Partial Specialization
template
class vertexTypeTraits
{
public:
typedef boost::shared_ptr > vertexPtr;
typedef std::vector allVertexVector;
typedef edge edgeType;
};


// Partial Specialization
template
class vertexTypeTraits
{
public:
typedef boost::shared_ptr > vertexPtr;
typedef std::vector allVertexVector;
typedef arcs edgeType;
};
#endif

/*
Traits
= A class template used to describle
another class template parameter
= If/else of type
= Used to Type Parameterize

The compiler looks up information
about the edgeType from graph class template
in the vertex's traits class

Example:
1. char_traits
2. float_traits(std::numeric_limits)

*/



Usage in Graph.h

template
class graph
{
public:

typedef edgeType edgeType;
typedef typename vertexTypeTraits::vertexPtr vertexPtr;
}




Out of Difficulties Makes Miracle

..............