Some Doubts

1.What is the use of this?

2.Why is the main function of type int?
1. In a class member function, it's a pointer to the class instance.
2. Just because. Or look somewhere down this thread:
http://www.cplusplus.com/forum/beginner/45740/
By the way, main is not of type int, it returns int.
1. I don't understand. Could you simplify it?
1. Have you learn about classes and pointers yet? When inside a class member function, it is often useful to have a pointer to the instance of the class from which the member function has been called. Run the following program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
class foo {
public:
   void bar() { std::cout << this << std::endl; }
};

int main()
{
   using namespace std;
   foo foo1, foo2;
   cout << "foo1\n" << &foo1 << endl;
   foo1.bar(); 
   cout << "foo2\n" << &foo2 << endl;
   foo2.bar();
  
   return 0;
}

I haven't actually tested this program...

The address of foo1 is the same as the value printed by foo1.bar(), because foo1.bar() prints this which is a pointer to the instance from which the function was called, i.e. a pointer to foo1.

2. main() returns int by convention I suppose. In the past, this return value was used as an error code by the operating system (0 = success; nonzero signifies some kind of failure). I don't think this error checking is often performed anymore, but the integer return type has stuck.
Last edited on
Yeah I had learnt about pointers, and was reading about Classes (in this site's tutorial).
That explains this.

Thanks a lot!!
Another Doubt:
What's the difference between a Template and a Function?
All I noticed was that the Template call has <>, rather ().
Which of those is better?
Neither is better. They are different.

Templates provide a way of parametrising functions and classes at compile time. Consider the following contrived example:
1
2
3
4
int add(int m, int n)
{
   return m + n;
}


Clearly, this works for floats, doubles, long doubles, unsigned intgerers, short integers, etc. So do we write the function out for each one. Of course not - it would take hours. We use template instead:
1
2
3
4
5
template <class T>
T add(T m, T n)
{
   return m + n;
}

We we can say add<int>(3, 5) or add<float>(1.0f, 5.0f). They are two different functions as far as the linker is concerned, but the compiler has autogenerated them from a single template function.

Note that by something called "template argument deduction", you can in fact just write add(3, 5) or add(1.0f, 5.0f). In the first case, the compiler knows that 3 and 5 are integers so in the first case it calls add<int>(). In the second case, it knows that 1.0f and 5.0f are floats, so it calls add<float>().

We can go a step further and let the function add two variables of different types, but that involves the C++0x decltype keyword, so I shan't go in to that.

You can do a similar thing with classes, but there is no template argument deduction for classes.
Thanks.
But what is class T?
And does it require any specific header file (apart from iostream)?
Template are a part of the core language. No header is required to write template functions (not even iostream...)

class T is a template argument. You may write typename T if you want. While functions have paremters of intrinsic and class type, a template's parameters must be either of integral type (int, unsigned, etc.) or otherwise they are an actual type.

class T is a parameter which can have the value of a particular type, e.g. int or float or std::string.

I haven't explained this very well as I'm short of time. Read this site's template tutorial for details.
http://cplusplus.com/doc/tutorial/templates/
Topic archived. No new replies allowed.