Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Things I don't understand about C++

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Heartless
I post too much
Reputation: 0

Joined: 03 Dec 2006
Posts: 2436

PostPosted: Sat Oct 06, 2007 3:26 pm    Post subject: Things I don't understand about C++ Reply with quote

Code:
int soldiers = 5;
int tanks = 10;
int max = (b>a) ? b : a;
int min = (b<a) ? b : a;
cout << "The largest value is: " << max << endl;
cout << "The smallest value is: " << min << endl;


Where does the a and b come from and how does it compare those two values?

Code:
int n = 25;
...
sqrt(static_cast<double>(n))

What does the static_cast<double>(n) do?

Code:
int martix[9][9];


What is that code used for?

Code:
int arr[5] = {0, 10, 30, 25, 50};
swap(&arr[2], &arr[3]);


What does the (&arr[2], &arr[3]) mean? I know that & tells the point go there, but the pointer can be pointing at two different things at once?

_________________
What dosen't kill you, usually does the second time.
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Sat Oct 06, 2007 3:45 pm    Post subject: Reply with quote

& just means, the value of..(basically)
Back to top
View user's profile Send private message
appalsap
Moderator
Reputation: 0

Joined: 27 Apr 2006
Posts: 6753
Location: Pakistan

PostPosted: Sat Oct 06, 2007 4:20 pm    Post subject: Reply with quote

The b and a are compared with the ternary operator, the syntax is
Code:

(condition) ? if_true_do_this : if_false_do_this;


static_cast is a new way of casting in c++, in c, you would do an explicit cast (ex: (double)an_integer) but static_cast has a few more rules, there is also dynamic_cast to cast at runtime. static cast is used as so
Code:

static_cast<type_to_cast_to>(variable_to_be_cast)


int matrix[9][9] is an array of array of integers.

smartz993 wrote:
& just means, the value of..(basically)


wrong, '&' is the reference operator which means address of. to get the value from the address use the dereference operator, '*'.
ex:

Code:

int add(int *a, int *b)
{
    return ((*a) + (*b));     
}

void sample
{
     int a = 12, b = 2, sum;

     sum = add(&a, &b);
     //sum contains 14
}


this is most useful with structures and large data types.


Last edited by appalsap on Sat Oct 06, 2007 4:24 pm; edited 1 time in total
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Sat Oct 06, 2007 4:21 pm    Post subject: Re: Things I don't understand about C++ Reply with quote

HornyAZNBoy wrote:
Where does the a and b come from and how does it compare those two values?
I think you mean
Code:
#define max (b>a) ? b : a;
? That way it'd be a macro returning the max val. Your way, a and b would be variables defined earlier. The actual code means that it first checks if b is higher than a and if it's the macro returns b, else it returns a.

HornyAZNBoy wrote:
What does the static_cast<double>(n) do?
It casts an int to double. Google for "casting."

HornyAZNBoy wrote:
Code:
int martix[9][9];

What is that code used for?
To declare a 9x9 matrix.. >_>

HornyAZNBoy wrote:
What does the (&arr[2], &arr[3]) mean? I know that & tells the point go there, but the pointer can be pointing at two different things at once?
(&arr[2], &arr[3]) are the parameters for the function ::swap. See the last quote too :P

smartz993 wrote:
& just means, the value of..(basically)
It's the reference operator, it tells the address of that variable/function/w/e are you referring to, NOT the value.

EDIT: fixing typos. I also noticed that I just got beaten by 1 minute :<

appalsap wrote:
Code:
void sample
{
Code:
void sample()
{
;)
Back to top
View user's profile Send private message
Qvazzler
Advanced Cheater
Reputation: 0

Joined: 02 Jan 2007
Posts: 68

PostPosted: Sun Oct 07, 2007 10:38 am    Post subject: Reply with quote

The whole casting thing is something way out of hand to me (and generally I don't see the great use of it either), but what I can tell you about is the multi-dimensional array, which even though I don't personally call it so, can be referred to as a matrix of variables.

Code:
int array[5];


As you may well know already, an array contains several variables. There's not much else to know than that. The reason why you do this is do be able to easily make alot of variables that will contain the same information, like 6 bags, the value of one variable being the amount of peanuts in one bag.

Code:
int array[10][3]


Basically the same thing. This is what we call a multidimensional array and if you were to picture it, it would be something like this:

OOOOOOOOOOO
OOOOOOOOOOO
OOOOOOOOOOO
OOOOOOOOOOO

where the length would be index value 10+1 (zero is also an index value so that makes 11 in length) and the height would be index value 3+1. If we were to do this:

Code:
array[3][1] = 1337;


We'd visualise it somewhat like:

OOOOOOOOOOO
OOOXOOOOOOO
OOOOOOOOOOO
OOOOOOOOOOO


The X being the position where we just set a value to.

Of course we can make more dimensions, but it is less likely to be necessary when there are things like struct(ure)'s and classes. Smile

For the long(er) and more thorough explanation, go here: http://www.cplusplus.com/doc/tutorial/arrays.html
Back to top
View user's profile Send private message
DeltaFlyer
Grandmaster Cheater
Reputation: 0

Joined: 22 Jul 2006
Posts: 666

PostPosted: Sun Oct 07, 2007 10:52 am    Post subject: Reply with quote

Qvazzler wrote:
where the length would be index value 10+1 (zero is also an index value so that makes 11 in length) and the height would be index value 3+1. If we were to do this:

When declaring arrays, the dimensions entered in the square brackets are not the last index of the array, but rather the size of the array. So your example of int array [10][3] would actually produce a matrix of 10x3, not 11x4.

_________________

Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites