Simple C++ Questinon


'Jan-Nik' said:
Think of Smart Pointers like boost::shared_ptr. There the "." operator has a meaning.
I would rather say the "->" operator has a valid meaning for a non-pointer. As a good example : boost::shared_ptr.

You use it like "boost::shared_ptr false_ptr = true_pointer;" and not like "boost::shared_ptr *false_ptr = true_pointer;". So it is an object and not a pointer when instantiated but its -> operator is defined so it can be handled as if it is a pointer.

On the contrary, the "." operator has never a valid meaning for a true pointer.
 
Last edited by a moderator:
'Kramy' said:
How would you lose control? It could transparently support both.

This isn't the most complex thing for a compiler to figure out. It's more than doable to check if something is a pointer and replace a period with a "->" ;)

C/C++ favour complexity to humans when there's no valid reason for it, because a compiler/computer can figure it out just fine. I write it off to the choices of C/C++'s creators.
We shouldn't forget the historical aspect of C and so of C++. The first C compilers weren't smart enough to optimize language machine production as we are used to nowadays. But as it allows a fine control on language machine production, C was a "natural" candidate to make an operating system like Un*x to avoid as much as possible assembly source. For this reason, C offers both abstract programming (and more portability) and precise control on language machine production.

C++ inherits most aspect of C, so it keeps a lot of C features like explicit pointer (through "*") but also add "&" as a replacement for "*", so you can handle an identifier to an implicit constant pointer as a variable in the same manner you will do with local or global variables.

And if you ask why an implicit *CONSTANT* pointer, just think "TYPE &X" as "TYPE * const Y;" where X acts as *Y.
 
Last edited by a moderator:
You freaked me out for a second there, I felt like I'd missed something.
:lol: I always heard "implicit pointers" referred to as references.

They're good for passing data back out of functions. You don't have to worry about null values and they can't be re-assigned to point to somewhere invalid. It stops a few segfault-type errors from being possible.
 
Last edited by a moderator:
'lulzfish' said:
You freaked me out for a second there, I felt like I'd missed something.
:lol: I always heard "implicit pointers" referred to as references.

They're good for passing data back out of functions. You don't have to worry about null values and they can't be re-assigned to point to somewhere invalid. It stops a few segfault-type errors from being possible.
:lol:, you're totally right, we call them references in C++ term. I was just speaking in C equivalence term. Note that the first C++ compiler (CFront) produces a C source as output - another reason why people may think C++ favours complexity.
 
Last edited by a moderator:
C++ makes complexity an option, but it doesn't enforce it.
I think I'm going to give Scala shot, it looks pretty cool. It runs on the JVM, has dynamic language features, and static typing.
Basically does everything I've been thinking would be convenient in a language.
And there's a direct interpreter in addition to bytecode compiling.
 
Last edited by a moderator:
lulzfish said:
C++ makes complexity an option, but it doesn't enforce it.
I think I'm going to give Scala shot, it looks pretty cool. It runs on the JVM, has dynamic language features, and static typing.
Basically does everything I've been thinking would be convenient in a language.
And there's a direct interpreter in addition to bytecode compiling.
Welcome, brother, to the Scala side! ;)
Been using Scala for a long while now (you MIGHT have noticed me spamming about it in a few threads), and am actually writing a game for the Pandora in it...
(It's that simple idea from the game concepts thread; I have to start with something, don't I?)
It's very easy to make games in Scala; the most difficult part so far for me has been the OpenGL stuff (I haven't done OpenGL in AGES; DirectX has swamped my brain)

One word of warning, though: Many of the IDE plugins suck (aka those that handle your project, provide things like refactoring and code completion to support Scala in IDEs in general); I'm stuck with the NetBeans beta plugin and it has some minor annoying issues, and so do all the other plugins. The language support is very 'beta'. If I were you, I'd stick with a text editor and the interpreter in the beginning.

If you at all decide to use Scala, that is.
 
Last edited by a moderator:
Yeah, I heard you mention it, and when the Wikipedia article show some cool dynamic features alongside static typing, so I think I'll try it.

And I've already come to hate IDEs. They make everything complicated and I can never find one I really like. Text editors, on the other hand, always work, are totally interchangeable, and can be run over SSH.

The problem I usually have with languages that aren't C or C++ is the bindings. How can I call, say, SDL functions from Scala?
 
Last edited by a moderator:
'lulzfish' said:
The problem I usually have with languages that aren't C or C++ is the bindings. How can I call, say, SDL functions from Scala?
You can't, at least not easily (JNI and JNA don't count). You'll have to use java libraries, like Java2D/Java3D/lwjgl.

Non-jvm languages are easier to bind to SDL (Pygame, Ruby-game, etc.)
 
Last edited by a moderator:
lulzfish said:
Yeah, I heard you mention it, and when the Wikipedia article show some cool dynamic features alongside static typing, so I think I'll try it.

And I've already come to hate IDEs. They make everything complicated and I can never find one I really like. Text editors, on the other hand, always work, are totally interchangeable, and can be run over SSH.

The problem I usually have with languages that aren't C or C++ is the bindings. How can I call, say, SDL functions from Scala?
You can use lwjgl for most things, but it's more leaned towards OpenGL than SDL (it enables you to use all OpenGL commands in Java, so you can follow the NeHe tutorials almost directly). If you REALLY want to use SDL, there's sdl4java, javasdl etc. Many different projects. I haven't tried any of them because I only do 3D.

Then we have higher level stuff, like the jMonkeyEngine; it can be pretty difficult to compile so I prefer using the binaries; but otherwise it's great: It uses lwjgl for drawing but adds things on top of that like a physics engine, scene node management, texture handling, shaders and materials made easier etc. It's kinda like OGRE, but for Java. (You can of course use OGRE in Java/Scala too, but that's a whole different story)

If you are interested, I'll probably make my own project open source pretty soon, it uses jBullet for 3D physics and lwjgl for drawing; I seem, however, to have issues with drawing simple primitives in OpenGL (I want to make it ES 1.1-compatible, because I heard that someone will port lwjgl to the Pandora) so it might take a while.

EDIT:
@sindbad yes, but all the other languages use technologies similar to JNI, so if that 'doesn't count', then what does? JNI is like dynamic linking in C++ but for Java... Don't really see the problem here.
Or can you use SDL directly in Python? No, didn't think so, you have to go through pygame :p

Another EDIT: BTW, I don't see a reason why you wouldn't want to use lwjgl (ok the only reason might be that there might be difficulties while porting it but it *should* almost be a recompile and done), since it is WAY faster than SDL can even dream of being, since it uses hardware for ALL the rendering. And if we compare pygame vs lwjgl: I made 2 apps once, to compare the performance of the two. Both apps drew a simple white square. With SDL, I got 120 FPS on a low-end machine. Same machine, lwjgl: 3000 FPS.
 
Last edited by a moderator:
'dflemstr' said:
@sindbad yes, but all the other languages use technologies similar to JNI, so if that 'doesn't count', then what does? JNI is like dynamic linking in C++ but for Java... Don't really see the problem here.
Or can you use SDL directly in Python? No, didn't think so, you have to go through pygame :p
Sorry, for some reason I assumed there weren't projects for SDL on the JVM.

JNI is horrible to use. Python has ctypes, you can use SDL or other C libs directly without pygame (look at pygame-ctypes and pyglet). JNA gets close to ctypes in ease of use, but it's much slower (figure that).
 
Last edited by a moderator:
I was just using SDL as an example, I would really like to call functions from any C / C++ library without needing to write or download bindings, but I don't know if this is possible.
 
Last edited by a moderator:
lulzfish said:
I was just using SDL as an example, I would really like to call functions from any C / C++ library without needing to write or download bindings, but I don't know if this is possible.
Not possible, not without bindings. In C++ you have the header file as your binding, but you obviously lack that in other languages. You can't use another language's libraries directly in any language.
There ARE tools for Java that lets you do things like "lib = loadLibrary("blabla.so"); lib.call("myFunc")" but that's a binding, too, (and has performance issues) so it's not good to use such methods.

Now, LWJGL uses a C library for it's backend and does all the binding for you (you just tell it where this library is stored).
So, in Scala I create a display in lwjgl and say:
CODE

import org.lwjgl.opengl.GL11._ //this makes all the GL 1.1 stuff available, there are bindings for all OpenGL versions including GL30 and GLU

glBegin(GL_LINES) //EDIT: I failed at simple OpenGL calls, I have difficulties as I said :p
glColor3f(1, 0, 0)
glVertex3f(1, 0, 0)
glVertex3f(0, 0, 0)
glEnd()



Similar libraries exist for most C libraries (so that you can access a C library from java).

Examples:
OpenAL -> lwjgl
OpenGL -> lwjgl
DevIL -> many different libraries available
ODE -> odejava
Bullet -> jBullet
OGRE -> ogre4j
Qt -> QtJava
GTK -> various

etc etc
 
Last edited by a moderator:
I only ever use "." to access structures. My editor knows whether the variable I'm referencing is a pointer or not and automatically replaces "." with "->" if necessary. Since you need to use the shift key to get the right arrow, that saves me 2 key presses each time and I don't have to worry about typing the wrong one :)

But yes, the compiler also (obviously) knows you've typed the wrong one and issues an error if thats the case. You would have thought there would be a option to auto-convert "." to "->" if the referenced variable was a pointer.
 
Last edited by a moderator:
'Squidge' said:
I only ever use "." to access structures. My editor knows whether the variable I'm referencing is a pointer or not and automatically replaces "." with "->" if necessary. Since you need to use the shift key to get the right arrow, that saves me 2 key presses each time and I don't have to worry about typing the wrong one :)

But yes, the compiler also (obviously) knows you've typed the wrong one and issues an error if thats the case. You would have thought there would be a option to auto-convert "." to "->" if the referenced variable was a pointer.
Which editor/IDE do you use? :D
 
Last edited by a moderator:
QtCreator is THE best C++/C IDE, without hesitation. It has all features you could ever need (ok, except maybe some old emacs features, but who uses those anyways)
 
Lol, very simple question, but I just cant find the answer anywhere.

Im writing a very simple guess the number game, but I have the problem that I want to use a variable declared in a function in a other function and I just don't how to do that. Ive tried making the variables global but they reset to 0 when the function goes out of scope, can anyone help me?
 
Last edited by a moderator:
Mithrildor said:
Lol, very simple question, but I just cant find the answer anywhere.

Im writing a very simple guess the number game, but I have the problem that I want to use a variable declared in a function in a other function and I just don't how to do that. Ive tried making the variables global but they reset to 0 when the function goes out of scope, can anyone help me?

Two words: reference parameters
 
Last edited by a moderator:
CODE

#include <iostream>

int c=1;

void foo(int a)
{
++a;
}

void fooo(int &a)
{
++a;
}

int main(int argc, char *argv[])
{
std::cout << "c is " << c << " at start." << std::endl;
foo(c);
std::cout << "c is " << c << " after foo(c)." << std::endl;
fooo(c);
std::cout << "c is " << c << " after fooo(c)." << std::endl;
return 0;
}
 
Back
Top