class IObject
{
public:
virtual void create(void** ret_val) = 0;
virtual std::string name() const = 0;
};
template <typename _Type>
class ObjectT : public IObject
{
protected:
static std::string m_name;
public:
void create(void** ret_val)
{
*ret_val = (void*) new _Type;
}
};
typedef ObjectT<bool> BoolObject;
typedef ObjectT<int> IntObject;
template<> std::string BoolObject::m_name = "bool";
template<> std::string IntObject::m_name = "int";
BoolObject bobj;
IntObject iobj;
std::vector<IObject*> my_vector;
my_vector.push_back (&bobj);
my_vector.push_back (&iobj);
if (my_vector[n].name() == "bool") {
bool* pval = nullptr;
my_vector[n].create((void**) &pval);
*pval = true;
}
#include <iostream>
#include <fstream> // std::fstream
using namespace std;
typedef struct
{
int year;
string fio;
} Anketa;
int main ()
{
Anketa anketa;
anketa.year = 1643;
anketa.fio = "Isaac John Newton";
// *** Writing ***
fstream fs;
fs.open ("anketa.dat", std::fstream::out | std::fstream::binary);
fs.write((char*) &anketa.year, sizeof(int));
size_t len = anketa.fio.length();
fs.write((char*) &len, sizeof(size_t));
fs.write(anketa.fio.data(), len);
fs.close();
// *** Reading ***
fs.open ("anketa.dat", std::fstream::in | std::fstream::binary);
fs.read((char*) &anketa.year, sizeof(int));
fs.read((char*) &len, sizeof(size_t));
char buf[len+1];
fs.read(buf, len);
buf[len] = 0;
anketa.fio = buf;
fs.close();
cout << anketa.year << " " << anketa.fio << endl;
return 0;
}
vector<int> vec;
int& tmp = vec.operator [] (2);
operator = (tmp, 12);
void checkDirectory( const Dir& dir ) {
if (isDirBad( dir ))
throw "The directory is bad bad directory";
}
void File::open( const FileName& fn ) {
try {
checkDirectory( Dir( fn ));
openFile(fn);
}
catch( const char* msg ) {
std::cerr << msg << std::endl;
}
}
template <class _T>
class FunctionT : public function
{
public:
static const char m_name[];
function* get_function(const string& name) {
if (name == m_name)
return new _T;
return nullptr;
}
};
using namespace std;
vector<function*> functions;
functions.push_back( new FunctionT<sinus> );
functions.push_back( new FunctionT<cosinus> );
functions.push_back( new Function<tangens> );
for(vector<function*>::iterator it = functions.begin(); it != functions.end(); ++it) {
function* f = (*it)->get_function("sinus");
if ( f != nullptr )
f->get_out();
}
unsigned int calc_hash(const unsigned char* s, unsigned int len)
{
char ch = 0;
unsigned int result = 0;
unsigned int n = 0;
for ( ; n != len; ++n) {
result ^= (s[n] - ch) << (n % 3) * 8;
ch = s[n];
if (result & 0x800000) {
result <<= 1;
result |= 1;
}
else
result <<= 1;
}
result &= 0xFFFFFF;
result |= (n % 256) << 24;
return result;
}