Template/Pointer questions

Okay so I have a couple of questions.
Q1 : How come this works unsigned int a = 0, *p; p = &a; *p += 1; cout << *p << endl; cout << a << endl; //get output of 1 /n 1.
but this does not workunsigned int a = 0, *p; p = &a; *p++; cout << *p << endl; cout << a << endl;
I am curious why you can set the pointed value equal to that value plus 1 but you can not increment the pointed value by one?

Q2: When would you ever use a Template and what is the point of it?
1
2
3
4
5
6
7
8
9
10
11
12
 //templated
template <class t>
t function(t a)
{
// do stuff
}
//with out template

void function(unsigned int a)
{
// do stuff
}

and is there a benefit to using templates?
seems like a pain to do something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template <class t>
t function(t a);
int main()
{
unsigned int num;
function(num);
}
template <class t>
t function(t a)
{
//do stuff with a
}
//versus:
void function(unsigned int);
int main()
{
unsigned int num;
function(num);
}
void function(unsigned int a)
{
//do stuff with a
}


Q3: what exactly is the point of using pointers? is it so you can set variables equal to something or one another dynamically?
ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
unsigned int *a, b = 0, c = 0;
    a = &b;
    cout << "a: " << *a << endl;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl << endl;
    b++; c++;
    cout << "a: " << *a << endl;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl << endl;
    b = 10;
    c++;
    cout << "a: " << *a << endl;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl << endl;
    *a = 5;
    c++;
    cout << "a: " << *a << endl;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl << endl;
    a = &c;
    b++;
    c++;
    cout << "a: " << *a << endl;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl << endl;


output:

a: 0
b: 0
c: 0
// a == b; c does something else
a: 1
b: 1
c: 1
// a == b; c does something else
a: 10
b: 10
c: 2
// a == b; c does something else
a: 5
b: 5
c: 3
// a == b; c does something else
a: 4
b: 6
c: 4
// a == c; b does something else


thanks for any information =]

EDIT:
Q4: what is the point of using static numbers? and why/when would you ever use them?
ex:
1
2
3
4
5
6
7
8
for(unsigned int i = 0; i<5; i++){
        unsigned int a = 0;
        a++;
        cout << "auto " << i+1 << ": " << a << endl;
        static unsigned int b = 0;
        b++;
        cout << "static " << i+1 << ": " <<  b << endl;
    }


get an output of

auto 1: 1
static 1: 1
auto 2: 1
static 2: 2
auto 3: 1
static 3: 3
auto 4: 1
static 4: 4
auto 5: 1
static 5

so this means when you delcare something static you only set it equal to a value the very first time I am guessing based on the results.

EDIT: i was wrong about the setting equal thing on statics. I believe it is once you declare it once, it will not re-declare each time it is called like the auto numbers.
so basically if you do a loop and declare a new variable it will only do that if it is auto and not static.
Last edited on
After playing around with templates I realized that they could be quite useful if you want a function to do different things based on the class. ex
if the function has a parameter of a string it will do one thing say print "Hello" and if the function has a parameter of a int it will do something else like say double the number then print "int * 2 = " << a * 2

but my question on that is how come if I do not call the function as a static function I get this [quote[error: cannot call member function 'void type_name<int>::num()' without object[/quote]

//working code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template <class T>
struct type_name {
    void name();
};

template<> struct type_name<int> {
    static void name()
    {
        cout << "Int" << endl;
    }
};
template<> struct type_name<char> {
    static void name()
    {
        cout << "Char" << endl;
    }
};


int main()
{
    type_name<int>::name();
    type_name<char>::name();
}

and if I remove the static before void it does not work when I call for it in the main function. but if I do not call for it in the main function with out static I get no syntax error either. any info would be greatly appreciated.
Last edited on
Q1: It might be a precedence issue, but that's just a guess. Try (*p)++.

Q2: That's exactly it; you do that to allow something to work for various types. The standard vector class is a good example; it works for any type, including ones you create yourself, because it is a template. Otherwise, you'd have to make a version yourself for every type of object it could contain.

Q3: Pointers really shine when you have to work with polymorphism. That way, you can have a function that deals with any derived type of an object so you don't need to write unique functions to handle every new subtype you come up with, especially when they are all handled the same way.

Q4: static variables are kind of like globals, but with a limited scope. In my programming, I've really only used them to store values that all instances of a class need to share.

-

I see that you are defining all your static classes as template specializations... That's not really the point of templates. Have you ever used the vector class? It's templated so it can hold an internal array of whatever type you like.
I have used vectors before but I didn't know you could use vectors for functions
I used them for storing variable values like this
1
2
vector<int> nums;
for(unsigned int i = 0; i<5; i++) { nums.push_back(i); cout << nums[i] << endl; } 

and at Q1 that was just me not thinking about it...I keep forgetting that if you do variable++ it adds after the end(;) and not before like the ++variable because ++*p works fine thanks for the response and information
Q1:

It's to do with the precedence of the operators. ++ binds tighter than * so: *p++ increments to the next memory address then dereferences it, which is invalid in your example because there is probably garbage at that address. To increment the variable at address p, do this: (*p)++

Q2:

Templates are used so you can write one piece of code that will work for a variety of objects - as you discovered with your int & char. One of their uses in the STL is with containers that are independent of the objects within them. Same story with algorithms like sort & find for example.

Q3:

Pointers are useful for when you have large objects - rather than copy the whole object by value (it might be 10Mb say), we use a pointer instead. The compiler can use this to access individual parts of the object. Pointers were used a lot in C, but they are still used in C++ for some things - the new operator returns a pointer to memory on the heap for example.

C++ also has references, which are kind of similar to pointers. A reference must be associated with a variable, whereas a pointer can be made to point at anything - which can be a big source of errors. There are traps with references too- like them going out of scope.

Google smart pointers like std::shared_ptr for even more advanced stuff.

Q4:

Google C++ static

BTW - have you read any of the tutorial or reference material on this site?



@giblit

++*p works because the association is right to left.
Yeah I have read the references and did the tutorials its just I couldn't find a use for them although its probably because I haven't made any large programs yet. and I still can't figure out how you would use a vector to make a function do different things
for example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class T>
T function(T);
int main()
{
int a;
char b;
function<int>(a);
function<char>(b);
}
template<class T>
T function(T a)
{
//do something if int
//do something else if char
}


//as I am typing this I figured it out sorry didnt realize there was a class
//section at the bottom only thought it was the function temp.
//this seems to work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<class T2>
class cls {
public: void function(T2);
};

template<> class cls<int> { public: static void function(int a) { cout << "int " << a << endl; } };
template<> class cls<char> { public: static void function(char a) { cout << "char " << a << endl; } };
int main()
{
    int num = 8;
    char ch = 'c';
    cls<int>::function(num);
    cls<char>::function(ch)
}


output:


int 8
char c


Thanks for all the help again I appreciate it a lot.
Last edited on
Q3. Memory for local variables is allocated from stack. Stack has (relatively) limited size. Local variables have lifetime of their enclosing scope. Local variables can be used via their name, references, or pointers.

There is an another memory to allocate from: heap. The 'new' and 'delete' operators (C has 'malloc' and 'free' functions). You do need a pointer to refer to heap memory. Lifetime is explicit and not limited to scope.

The key in pointers is that you can change where they point to.
@giblit

Maybe you need a good book - one by Bjarne Stoustrup maybe?
okay I finally figured out how to use the templates correctly thanks again here is an example code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
template<class T>
void function(T);

template<> void function<int>(int a)
{
    cout << a << " is an int." << endl;
}

template<> void function<char>(char a)
{
    cout << a << " is a char." << endl;
}

template<> void function<string>(string a)
{
     cout << a  << " is a string." << endl;
}

int main()
{
     int num = 9;
     char ch = '9';
     string str = "9";
     function(num);
     function(ch);
     function(str);
     function(num);
     function(num);
}

output is:
9 is an int.
9 is a char.
9 is a string.
9 is an int.
9 is an int.
Topic archived. No new replies allowed.