Wednesday, September 12, 2007

Example of template linking error

B.H
template '<' class t '>'
class b
{
public:
b() ;
~b() ;

} ;

// B.CPP
#include "B.H"
template '<' class t '>'
b'<' t '>'::b()
{
}
template '<' class t '>'
b'<' t>::~b()
{
}


//MAIN.CPP
#include "B.H"
void main()
{
b '<' int '>' bi ;
b '<'float '>' bf ;
}

When compiling b.cpp, the compiler has both the declarations and the definitions available. At this point the compiler does not need to generate any definitions for template classes, since there are no instantiations. When the compiler compiles main.cpp, there are two instantiations: template class
b '<'int'>' and b'<'float'>'. At this point the compiler has the declarations but no definitions!

If do g++ main.cpp ,error will come
if we write g++ main.cpp b.cpp the result is same.
Solution to this we have allready discussed, pls see in my site itselt template instatiation problem.
Regards,
sahu

Why do we need template?

It is a generic type. Something we relate to void * in C language.
It can take any type values. I mean ,It is easy to define a class like Vecror or List.But it is difficult to use the class with another type, we must replace all type names in the original file with the new type, this is a tedious and an error-prone process .
C++ provides a way to write a generic definition that works with any type --Here the answer is "templates". We use templates only to define classes that we want to work with many data types.

It can be used with classes or functions.

Boss it is also irritating if you are using GCC compiler.It's linker allways expect the template declaration and definition at a time while instatiating .If it does not see then there will be linking error some thing indefined error.



I had faced the same problem in STL some 4 years back. Actually In stl we can not have separate file for declaration and definion . It will have the linking problem.Because while instatiating , compile must see the declaration and definition at a time. . So while instatiating , by including mere .h will give the declarion , not the definition, so the linking problem.
In this case
Solution 1;
If we have 2 separate file i.e .h and .cpp , then while instatiting we should include .cpp instead of .h
Solution 2:
we have to give the definition in .h itself, and while instatiting compiler does not face any problem because it see the both declaration and definition at a time.
Note:- the gcc has implemented the vector,list all thes predefined STL in this way ,if we open any of stl header file we can see these implemetation.
This linking proble generally we don’t face in Solarice, But is a ovious problem for gcc linker

Regards,
Haramohan sahu

What is template instantiation

A C++ template is a framework for defining a set of classes or functions.
The instantiation, creates a particular class or function , by resolving the C++ template with a group of arguments that may be types or values.

Lets have an example.

#include < iostream >
#include < new >
#include < stdexcept >

using namespace std;
template < class T >
class Array
{

T *data; // pointer to heap allocated space
int size; // maximum size
public: T &operator[](int);
Array(int max);

};

template < class T >
Array::Array(int max)
{
size = max;
data = new T[size];

}//end constructor

template < class T >
T& Array::operator[](int index)
{
if (index < 0 || index > = size)
{
throw out_of_range("Array");
}
return data[index];
}//end Array

int main()
{
Array< double > test1(100);
test1[25] = 3.14;
cout< < test2(200);
Array < int > test2(200);
test2[0] = 55;
return 1;

}//end main

Now we will discuss what is instantiation ?
Here ,Array instantiates Array with type int.
This instantiation generates the following class definition:
class Array
{
int *data;
int size;
public:
int &operator[](int);
};

Like that it instatiate for float etc.
So now, we have understood what is instantiation.

Q 2) How to write the methos definition out side the class if it is a template class?

fist write the template type that the class use. Here in our case
template
then in as usual manner like reurn type then class name then method name with signature
Array::Array(int max)
{
size = max; data = new T[size];

}//end constructor



Regards,

sahu

Saturday, August 25, 2007

Why do i need dynamic casting in c++?

Hi ,
We know how to use dynamic casting,but why do i need it ? In which situation ,we will feel about this mechanism....

there are 4 type of casting c++ provides,
1) static_casting
2 const_casting
3) dynamic_casting
4) reinterprit_casting
Here i will discuss about only dynamic one,
whatever i do with dynamic_casting , i can achive the same by C Style casting. So why do i need it...? There are some situation , we should go for dynamic casting.
First i would like to discuss what is dynamic casting.

1) Run-time checked casts in an inheritance hierarchy

2) Finding the beginning of an object

3) Meant for downcasting from a base class to a derived class. This check is done at runtime (as opposed to static casting which can generate the necessary code to do the casting at compile time).

4)Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class.

5)The base class must be a polymorphic one (by declaring virtual)


example1

class B
{
public:
void show()
{
cout<<"I am in Class B \n"; } /*virtual*/ ~B() { } }; class D : public B { public: void dis() { cout<<"i am in derive class Dis\n"; } }; int main() { B *bPtr = new B; D *dPtr = dynamic_cast(bPtr);//DownCast
// D *dPtr = (D*)(bPtr);
dPtr->show();

}

Here the above programm will show error because source type is not polymorphic

Funtion return array of integer

/*
This example says about the function takes no argument and returns pointer to array
of 3 interger.
*/

#include < iostream >
using namespace std;

int arr[]={11,22,33};

int (*f())[3]
{
cout << arr[1];
return &arr;

}


int main()
{
int(*(*g)())[3]= f;
(**g());
}


Cheer,
Sahu

Friday, August 24, 2007

Some more example of Function pointer.

Example1
Here i just want to write a program which deals with function pointer.
A function taking no argument returning a pointer to function which takes an arument of integer and that returns an integer.
/*
This methos return an int and takes an int
*/

int incr(int x)
{
x++;
cout<< "X val after incremented "<< x <<"\n";
return x;
}
/*
Here f is a function takes no argument ,returns a pointer to function(here say our incr() function) which takes an intger as argument that returns an integer..
Means f returns a function pointer that function pointer points to or hold the address of a function .Now tell me, what kind of fuction.... It can hold function of taking one int and return one int.
*/
int (*f())(int)
{
cout<< "Pls method returned \n";
return plus;
}
int main()
{
cout<<"Inside Main method Func ptr called "<< f()(5) << "\n";
}

cheer,
sahu

Thursday, August 23, 2007

Can i cast char* to a unsigned char* or vice versa ?

No boss , we can not cast char* to a unsigned char* or vice versa.
The reason is , lets first understand static_cast .

Few points about stati cast listed below
1) Only allowed for conversions which the compiler can check
2) we can not use static_cast to a pointer to a non-pointer
3) we can not use static_cast to remove constness(orvolatileness)
4) we can not use static_cast from a virtual base class
5) Static_cast is only for related types.pointer type (remember pointer type, it works fine for general primitive data type i.e int,char ,float)

So here Char* and unsigned char* are two unrelated pointer types. One more thing ,here one of the pointer is void * then we can do it.
Lets have an example,
#include
using namespace std;


void uchar_f1(unsigned char *x)
{
cout<<"uchar_f1 "<< x <<"\n";
}

void char_f1(char *x)
{
cout<<"char_f1"<< x << "\n";
}int main()
{
unsigned char uc[]={ 'A' , 'B' , 'C' };
uc[3]='\0';
char c[]={ '1' , '2' , '3'};
c[3]='\0';
uchar_f1(uc);
char_f1(c);
uchar_f1(static_cast(c));

}

If we write uchar_f1(static_cast(static_cast(c)));

So this will work.
Cheer,
sahu