| View previous topic :: View next topic |
| Author |
Message |
sivashankar21@gmail.com Guest
|
Posted: Sat Feb 25, 2006 5:06 am Post subject: to improve programming in pointers |
|
|
how i can improve my programming skill in pointer using c++ |
|
| Back to top |
|
 |
benben Guest
|
Posted: Sat Feb 25, 2006 6:06 am Post subject: Re: to improve programming in pointers |
|
|
by more reading and practices.
Ben |
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Sat Feb 25, 2006 7:06 am Post subject: Re: to improve programming in pointers |
|
|
sivashankar21 (AT) gmail (DOT) com wrote:
| Quote: | how i can improve my programming skill in pointer using c++
|
Go find the answers to these questions.
struct A { int a; };
struct B : A { int b; };
A xa[ 10 ];
B xb[ 10 ];
A * pa = xb;
// Q1 - what is the value of pa pointing to ?
A * pBAD = pa + 1;
// Q2 - why is pBAD, bad ?
B * pb = xb;
// - same as Q1
A * paOK = pb + 1;
// Q3 - what does paOK point to ?
A (&( * pf )( int * ))[ 10 ];
// Q4 - what is pf ?
A * paX = & pb[1];
bool b( paX == paOK );
// Q5 - what is the value of b
A * paY = & pb[3];
int iNOTGOOD( paY - paX );
// Q6 - Why is iNOTGOOD not good ?
A * paM = new B;
int main()
{
delete paM; // line Z - NOT GOOD
// Q7 - why is line Z a bad thing ?
paM = 0;
// Q8 - what is paM now ?
delete paM;
// Q9 - what does delete paM do ?
bool x( & (* xb).b == & xb->b );
// Q10 - What is the value of x ?
}
B * F()
{
B x[2];
return x; // doh !
// Q11 - Why is the line marked "doh" broken ?
} |
|
| Back to top |
|
 |
loufoque Guest
|
Posted: Sat Feb 25, 2006 3:06 pm Post subject: Re: to improve programming in pointers |
|
|
sivashankar21 (AT) gmail (DOT) com a écrit :
| Quote: | how i can improve my programming skill in pointer using c++
|
You shouldn't even use pointers.
Avoid them as much as possible and only use them when it's needed. |
|
| Back to top |
|
 |
Default User Guest
|
Posted: Sat Feb 25, 2006 5:06 pm Post subject: Re: to improve programming in pointers |
|
|
loufoque wrote:
| Quote: | sivashankar21 (AT) gmail (DOT) com a écrit :
how i can improve my programming skill in pointer using c++
You shouldn't even use pointers.
Avoid them as much as possible and only use them when it's needed.
|
At which point he won't know how to use them.
Brian
--
If televison's a babysitter, the Internet is a drunk librarian who
won't shut up.
-- Dorothy Gambrell (http://catandgirl.com) |
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Sat Feb 25, 2006 11:06 pm Post subject: Re: to improve programming in pointers |
|
|
Ian Collins wrote:
....
| Quote: |
Are there any (real world) instances where pointers can't be avoided?
|
Let's see:
a) the "this" keyword.
b) When you may or may not want to pass an interface ... e.g.
void DoThing( const A & a, ErrorReporter * e = 0 );
c) Intrusive reference counted objects.
d) When objects point to one another i.e. a<->b
+ many more |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Sat Feb 25, 2006 11:06 pm Post subject: Re: to improve programming in pointers |
|
|
Default User wrote:
| Quote: | loufoque wrote:
sivashankar21 (AT) gmail (DOT) com a écrit :
how i can improve my programming skill in pointer using c++
You shouldn't even use pointers.
Avoid them as much as possible and only use them when it's needed.
At which point he won't know how to use them.
But he may have developed sufficient skill in in avoiding them so he |
doesn't have to!
Are there any (real world) instances where pointers can't be avoided?
--
Ian Collins. |
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Sun Feb 26, 2006 1:06 am Post subject: Re: to improve programming in pointers |
|
|
Ian Collins wrote:
....
| Quote: |
With the exception of a, all these can be replace by some form of smart
pointer object.
|
You still need to understand pointers even if you're managing them using
smart pointers. |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Sun Feb 26, 2006 1:06 am Post subject: Re: to improve programming in pointers |
|
|
Gianni Mariani wrote:
| Quote: | Ian Collins wrote:
....
Are there any (real world) instances where pointers can't be avoided?
Let's see:
a) the "this" keyword.
b) When you may or may not want to pass an interface ... e.g.
void DoThing( const A & a, ErrorReporter * e = 0 );
c) Intrusive reference counted objects.
d) When objects point to one another i.e. a<->b
+ many more
|
With the exception of a, all these can be replace by some form of smart
pointer object.
--
Ian Collins. |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Sun Feb 26, 2006 7:06 am Post subject: Re: to improve programming in pointers |
|
|
Gianni Mariani wrote:
| Quote: | Ian Collins wrote:
....
With the exception of a, all these can be replace by some form of
smart pointer object.
You still need to understand pointers even if you're managing them using
smart pointers.
True enough, but I was trying to think of an example where raw pointers |
can't be replaced with smart pointers.
--
Ian Collins. |
|
| Back to top |
|
 |
benben Guest
|
Posted: Sun Feb 26, 2006 12:06 pm Post subject: Re: to improve programming in pointers |
|
|
| Quote: | True enough, but I was trying to think of an example where raw pointers
can't be replaced with smart pointers.
|
When you are writing your own smart pointers? Perhaps?
Ben |
|
| Back to top |
|
 |
|