Cool c++ tricks

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

adlep

Diamond Member
Mar 25, 2001
5,287
6
81
to get the output formated in hex:
int x = 14;

cout << hex << x;

 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
Originally posted by: arcain
And...

*(3 + p) is equivalent to *(p + 3)...

which means 3[a] is equivalent to a[3].

I doubt that very much, might have to try it.....

*(p+3) figures out that p is an int * and actually adds 3 * sizeof(int) = 12 before dereferencing.

3[a] shouldn't work for several reasons:
1. The compiler will complain that "3" is not a pointer type
2. Even if it treated 3 as a void*, adding the value of "a" as an int wouldn't work unless your array elements are 1 byte each.


 

arcain

Senior member
Oct 9, 1999
932
0
0
Originally posted by: glugglug
Originally posted by: arcain
And...

*(3 + p) is equivalent to *(p + 3)...

which means 3[a] is equivalent to a[3].

I doubt that very much, might have to try it.....

*(p+3) figures out that p is an int * and actually adds 3 * sizeof(int) = 12 before dereferencing.

3[a] shouldn't work for several reasons:
1. The compiler will complain that "3" is not a pointer type
2. Even if it treated 3 as a void*, adding the value of "a" as an int wouldn't work unless your array elements are 1 byte each.

You should give it try..

Here, with non-bytesized elements:

#include <stdio.h>

int main()
{

float array[4] = { 0.0, 1.0, 2.0, 3.0 };
int i;

for (i = 0; i < 4; i++) {
printf("i = %d, array[ i ] = %e\n", i, array[ i ]);
printf("i = %d, i[array] = %e\n", i, i[array]);
}

printf("0[array] = %e\n", 0[array]);
printf("1[array] = %e\n", 1[array]);
printf("2[array] = %e\n", 2[array]);
printf("3[array] = %e\n", 3[array]);

return 0;
}
 

prayansh

Junior Member
Sep 15, 2012
1
0
0
here is a code i wrote to output all characters

char ch;
int i;
for (i=0;i<256;i++) //there are 255 characters in total
{ ch=i;
cout<<ch;
}
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
the more "tricks" you use; the more difficult it can be to read/understand code when trying to maintain it.

It may make you look cool but also be a PITA

and some of those tricks may be compiler or OS dependent.
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
the more "tricks" you use; the more difficult it can be to read/understand code when trying to maintain it.

It may make you look cool but also be a PITA

and some of those tricks may be compiler or OS dependent.

Nothing in the thread so far even really qualifies as a "trick".... it's all standard C++, some of it is just poor coding, like the infinite loop or "breaking" cin instead of using a debugger...
 

brotj7

Senior member
Mar 3, 2005
206
0
71
C++ class templates are a cool little "trick". Can generalize a function to work for multiple datatypes.
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,675
146
106
www.neftastic.com
Not really C++, and very Microsoft specific - in fact particularly ATL specific... but ATL has a handy set of macros. If you set at the top of a function:

USES_CONVERSION;

You can use a set of macros including W2A(), A2W(), etc. to convert string types between wide, ascii and mbcs without having to dick around with API function calls.