News:

Simutrans Wiki Manual
The official on-line manual for Simutrans. Read and contribute.

code::blocks

Started by Roads, September 24, 2012, 03:51:48 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Roads

Thanks Isidoro.

Guys, I'm having a real hard time with this and one reason finally occurred to me.  It is because of all the application work I did with FoxPro and Oracle.  Sure those things gave me an understanding loops, arrays and some other stuff.  The problem is variables as I understood them and I don't think until now did I really understand that C++ is a whole different animal and I need to forget everything I ever knew about variables as defined by FoxPro and Oracle.  It seems like to me now there is only data in C++ and pointers to data.  Guess I'm thinking out loud.  At any rate maybe eventually this will make sense to me.

VS

You're spot on, Roads - with both observations :) And eventually it will make sense!

My projects... Tools for messing with Simutrans graphics. Graphic archive - templates and some other stuff for painters. Development logs for most recent information on what is going on. And of course pak128!

Ters

Makes me wonder what variables are in FoxPro.

While I risk confusing you more with this: A pointer in C and C++ is also itself a form of data. It can be viewed as simply a number. One can to arithmetic on it. Looking at it from the hardware side, it makes perfect sense. In the end, it's that way for all programming languages, as everything is numbers on a computer, but most of them hide this low level stuff from the programmer. But C, and C++ in turn, doesn't do much to hide the underlying mechanics, which has good and bad conseqences. C++ also has something it calls references, which can be thought of as a pointer, but doesn't really behave like one (from the programmers perspective). References in other languages are more like pointers in C/C++.

Roads

With your help (all of you), eventually or at least partial eventuality only took a couple days.  It seemed like a lot longer!  I have a much better understanding of this and something that also helped a lot was inserting a lot of cout into the code and watching how the program executed.

What you do in FoxPro is declare variables, say x=1.  If you ever say x=2, then x=1 is lost forever.  It is over written.  This made it real difficult to see that x could be equal to 3 and also later equal to 1 and the program remember both values.

No Ters you did not confuse me more.  I kinda understood pointers were data because you can increment them.  The tut sometimes mentions passing stuff by value which is almost always pretty clear and passing stuff by reference which is often pretty much unclear.  I have a feeling it is going to be awhile before I have a handle on this stuff enough that I can actually create something useful with it.

Ters

Quote from: Roads on December 07, 2012, 04:03:33 AM
What you do in FoxPro is declare variables, say x=1.  If you ever say x=2, then x=1 is lost forever.  It is over written.  This made it real difficult to see that x could be equal to 3 and also later equal to 1 and the program remember both values.

Sounds like it doesn't have scoping. That if you declare a variable, it is available from everywhere, whereas most other languages limit the variable to the scope it's declared in, and those scopes nested within that scope.

Roads

Yeah.  It has been a long time since I did any FoxPro and have forgotten a lot about it but there was no scope.  What I would always do is create programs and UDF's (user defined functions) to do specific things and call them as needed.

One thing I wanted to say about C++ and the data/pointers thing - is what I was thinking when I said C++ is only data and pointers - is that it seems the addresses of data is always saved and can be accessed with pointers.  As obvious as this appears now it was never stated in any tut I've read and with my back ground it was not at all obvious.

Ters

Quote from: Roads on December 07, 2012, 04:57:00 PM
One thing I wanted to say about C++ and the data/pointers thing - is what I was thinking when I said C++ is only data and pointers - is that it seems the addresses of data is always saved and can be accessed with pointers.

Data doesn't so much have an address that is saved, as the program needs the address for the data in order to access it. That's an inherent property of data on a computer (at least the kind we deal with here), but many (most) programming languages don't expose this to you. However, you can have data in C/C++ that doesn't have an address, or at least not in the sense that you can create a pointer to it. But then we're into low level hardcore optimization techniques.

Markohs

C++ is all about pointers and data, yep. It's a low level language after all.

isidoro

Quote from: Roads on December 07, 2012, 04:03:33 AM
[...]
The tut sometimes mentions passing stuff by value which is almost always pretty clear and passing stuff by reference which is often pretty much unclear.
[...]

The problem is, as Markohs points out, that C++ (and C) is a pretty much low level language.  The historical goal of C was to devise an assembly language independent of a concrete processor, so that you didn't have to rewrite entire software, especially OSes, each time you bought a new machine.

Passing by value/by reference is a clear example of that.  There isn't pass by reference in C.  There is only pass by value.  If you want pass by reference, you have to build it manually yourself.  That is, you have to pass (by value) a pointer (i.e. a memory address) so that the function/method can play with the bowels of the variable and, thus, modify it.

prissi

But C++ can be done without using pointers directly. You can even use garbage collection libraries. The power of C++ is the possibility to do low level as well as highly object oriented code ignoring pointer completly (apart from this object).

Roads

As odd as it might seem, What you guys are saying is helpful because all too often I want to say to heck with this.  As soon as I kind of understand one concept, the next is equally difficult.

Then y'all remind me I'm wrestling a bear, not playing with a puppy.  So thanks to everyone for the things you have said.

Roads

A couple of things - about pointers - this from one of the tut sites:

QuoteC++ offers two mechanisms for dealing with addresses:  references (&) and pointers(*).  Pointers are an older mechanism that C++ inherited from C, while references are a newer mechanism that originated in C++.   Unfortunately, many C++ library functions use pointers as arguments and return values, making it necessary for programmers to understand both mechanisms.

The other thing - I'm wondering if when working with arrays you guys use #include <vector> or I guess in the case of MSVC #include <apvector>?

asaphxiix

hey, I'm just starting a c++ tutorials on learn cpp.com. It's pretty nice so far, but I'm only at the beginning. I have no programming experience whatsoever. Started using MSVC Express, which is working out nicely so far.

Ters

Quote from: Roads on December 22, 2012, 12:51:13 AM
The other thing - I'm wondering if when working with arrays you guys use #include <vector> or I guess in the case of MSVC #include <apvector>?

I've never heard of apvector, and googling it doesn't yield much information about where it's from. They may not even be about the same apvector. One page mentions it as deprecated. One thing is certain: vector is standard C++. Whether apvector is a Microsoft extension, some 3rd party thing Microsoft ships with their compiler, or an old relic from some ancient C++, I can not tell.

Roads

#84
Ters, Thank you very much!  This was from the mathbits.com site.  When I tried to use some of the stuff they were using I got lots of errors.  It is good to know <vector> is standard C++.  From now on if it doesn't work with <vector> I'll just assume it is non standard and avoid using it.


Modify:  Hope you the best in your effort asa.  If XIIX is your age, then IMO, you are getting a very early start on this stuff - at least compared to me.

Roads

According to tut at learncpp.com which is the best I have found BTW, the following:


QuoteThe letter 'e' or 'E' is used to separate the two parts. Thus, a number such as 5e2 is equivalent to 5 * 10^2, or 500. The number 5e-2 is equivalent to 5 * 10^-2, or 0.05.


This all seemed obvious until I began playing around with it in code.



float x = 10e1;
cout << x << endl;
float y = 10^1;
cout << y << endl;



cout prints, in the first case 100, second case, 11.  Maybe I need to go back to first year high school algebra but I was thinking both cases should be 10. 

Ters

^ is not used for power in C++. No power operator exists. Other languages have it, and it is also used in plain text on computers where superscript is not supported.

10e1 is short for 10 x 101, which is 100. When using scientific notation (a x 10b), the absolute value of a should normally be less than ten.

Roads

Thank you Ters!  Often I feel like I'm barefoot in a blackberry patch and briars are everywhere.  For every bucket of berries I gather I donate so much blood. :(

Ters

In C, C++ and some other languages, ^ is the operator for bitwise xor. 10 ^ 1 = 1010b ^ 1b = 1011b = 11.

Roads

Ters!  OMG.  I can't pick the big juicy berries yet!  Not only are there briars in the blackberry patch but there are snakes as well.  xor sure looks like a snake to me.  That said, thank you for the post anyway as it is probably something I'll encounter someday.

Ters

XOR, like all other logical operators are quite simple. It's just remembering what the rule is for the name. The tricky part is remembering what random symbol is used for each operation in each programming language (I remember AND/&, OR/| and NOT/!, but had to look up what ^ means).

AND, OR and NOT is somthing you'll have to deal with relatively early on. XOR is more rare, more useful for clever tricks than normal human logic. Bitwise logical operators, where all the bits in a value are treated in parallel, are something that creeps up when dealing with bitfields, like ribi's in Simutrans.