Tag Archives: C++

C++0x is ready to rule.

In March 2011, a star was born and no one noticed. Atleast, the interpreted types did not. 🙂

The C++0x standard is approved and soon we will see compilers shouting from the top of their roofs that they support C++0x.

I am still reading about it, so here is the link dump of the most important C++0x links:

Also, there is one video which sums up C++0x quite well:

CPP Common Knowledge notes!!

[cpp]
class TestCode
{
//This is a test for Syntax highlighter!!!
};
[/cpp]
Item #11 : Compiler puts stuff in classes.
Compiler puts a virtual function table ptr in each class object with virtual functions.
Virtual function table pointer not same across platforms!!
Compiler may not put all these extra constructs in a struct.
Virtual inheritance is required when the diamond inheritance is causing issues.
For example:
[cpp]
class Base
{
public:
virtual void foo();
};

class D1 : public virtual Base
{
public:
void foo2();
};

class D2 : public virtual Base
{
public:
void foo3();
};

class SD : public D1, public D2
{
// We have taken care of the diamond inheritance issue with the virtual keyword
// so compiler is gonna shut up now!!
};
[/cpp]
Item #12 : Assignment & initialization are different.
These two ops not so diff in built-in types like int, char etc.
For user-defined types, it would not be advisable to perform user-defined assignment.
[cpp]

class X
{
char* buf;
mutable bool modified;
Y obj;
public:X()
{
buf = new char[100];
for(int i = 0;i<100;i++)
modified = false;
if( < 98)
return;
buf[index] = c;
modified = true;
}

void printdata()
{
cout << "Data is "<<;
ptr =static_cast(::operator new(sizeof(X)+100));// X::buf is not init!!
ptr->setData(0,’c’);
}
[/cpp]
Item #13 : Copy operations.
Copy construction and copy assignment are different operations.
[cpp]
class Hoo
{
char* buf;
int size;
public:
//copy CTor
Hoo()
{
buf = 0x00;
size = 0x00;
}

~Hoo()
{
if(buf)
{
delete[] buf;
buf = NULL;
}
size = 0x00;
}

void setData(char* data, int asize)
{
size = asize;
buf = new char[size + 1];
memset(buf, 0x00, size+1);
strncpy(buf, data, size);
}

Hoo(Hoo const &amp; hoo)
{
setData(hoo.getBuf(), hoo.getSize());
}

Hoo& operator= (Hoo const &hoo)
{
if(this != &hoo)
{
if(buf)
{
delete[] buf;
buf = NULL;
}
size = 0x00;
setData(hoo.getBuf(), hoo.getSize());
}
return *this;
}

char* getBuf() const
{
return buf;
}

int getSize() const { return size;}

};

void copy_ctor_assgn()
{
Hoo data;
data.setData("Hello",5);
Hoo dat2(data);
//copy ctor invoked here
Hoo dat3;
dat3 = dat2;// assgn operator invoked here
}
[/cpp]
Item #14 :Function Pointers.
Legal to point to inline functions, but the resultant call will not be inline.The compiler will not be able to determine at compile time what function is being called.
Can also point to an overloaded function. The particular function called will depend on the match between the function ptr type and the overloaded functions.

Notes….

[cpp]
8/25/2010
———
>>POM#1
Read 10 pages of Modern C++ design.
Realised that it needs more knowledge about templates.
Started with reading the book : C++ Templates, The Complete Guide 2002.chm

>>POM#2
Function templates
function template syntax:
template <typename a1,….>
funnction declaration or definition using T
‘typename’ introduces type parameter
historical alternative to typename is ‘class’.
templates are generated once for each type for which it is used
process of replacing template types with concrete types is called instantiation.
use of function template triggers the ‘instatiation’.
templates are compiled twice:
1. once for checking the syntax of the template.
2. second time to check the operations mentioned are valid for
the type used for template instantiation.

template parameters are determined by the arguments passed at the call time.
no automatic type conversion is allowed.

>>POM#3
Function templates have two kinds of parameters
1. template params – declared in angle brackets, N no. of them can be declared,
no default template arguments can be specified.
e.g.: template <typename T> // T is template param
2. call params – declared in parenthese after function template name
foo(T p1, T p2) //p1,p2 are call params
call params deduced from template params, so they are related.
function template argument deduction
when template return type cannot be deduced from template and
call params then needs to be explicitly mentioned.
template argument deduction does not try to determine the return type
another method to just declare one type and then let the compiler deduce the rest.
template <typename RT, typename T1, typename T2>
inline RT max (T1 const& a, T2 const& b);
// OK: return type is double, T1 is deduced as int and T2 is deduced as double.
max<double>(4,4.2)
overloading function templates->overloading resolution revisited
not part of every function call
— calls to functions thru function ptrs
— calls to member functions thru ptr to member functions
not subjected to resolution as they are determined at runtime.

function like macros cannot be overloaded and hence not subjected to reso

8/26/2010
———
Task for today:
1. chk the tower of hanoi problem
2. chk two design patterns and make notes abt them
[/cpp]