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 work
unsigned 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.