• What do you need help with? Version 5
    5,752 replies, posted
pointers
Also, instead of making "public" precede every member, you can just do this [cpp]class A { public: A(A link) { this.link = link }; A next; }[/cpp]
[QUOTE=Dark7533;39382275]I'm starting to lean c++ after learning java and I'm have a but of trouble. (I feel like such a noob D=) So lets say I have class A, see example [code] class A{ public A next; public A(A link){ this.link = link; } } [/code] How would I go about doing next = next.next in c++?[/QUOTE] First, you have "this.link = link;" where link is not a member of A, so here I'll assume you mean "this.next = link;" [cpp] class Node { public: Node* next; /* A pointer (memory address) to next Node. */ Node(Node& link) /* link is a *reference* to a Node object. */ { this->next = &link; /* &link gets the memory address to link, because Node::next is a pointer. */ } }; [/cpp] [editline]27th January 2013[/editline] If you have trouble understand that, I suggest you understand how [URL="http://www.cplusplus.com/doc/tutorial/pointers/"]pointers work.[/URL]
[QUOTE=ief014;39382577]...[/QUOTE] why would you use pointers for this
[QUOTE=dajoh;39382730]why would you use pointers for this[/QUOTE] So that next would actually be the object you want to point to and not a copy of it?
Certainly there are other ways. But what's the harm in doing so?
[QUOTE=ief014;39382577]First, you have "this.link = link;" where link is not a member of A, so here I'll assume you mean "this.next = link;" [cpp] class Node { public: Node* next; /* A pointer (memory address) to next Node. */ Node(Node& link) /* link is a *reference* to a Node object. */ { this->next = &link; /* &link gets the memory address to link, because Node::next is a pointer. */ } }; [/cpp] [editline]27th January 2013[/editline] If you have trouble understand that, I suggest you understand how [URL="http://www.cplusplus.com/doc/tutorial/pointers/"]pointers work.[/URL][/QUOTE] Lol I just realized I forgot to rename link in the constructer to next. Thanks for the help =D
[QUOTE=esalaka;39382757]So that next would actually be the object you want to point to and not a copy of it?[/QUOTE] [cpp] class Node { public: Node& next; Node(Node& link) : next(link) { } }; [/cpp] No copy will be created.
[QUOTE=dajoh;39383097][cpp] class Node { public: Node& next; Node(Node& link) : next(link) { } }; [/cpp] No copy will be created.[/QUOTE] You can't rebind references and generally you would want to have the possibility to relink lists.
[QUOTE=esalaka;39383210]You can't rebind references and generally you would want to have the possibility to relink lists.[/QUOTE] Of course, but with a reference as a parameter to the constructor, you'll never be able to instantiate the class.
I'm trying to sort an array of void pointers given to me in the following way in a c++ program: [code]void mysort(int n, int elementSize, void * array, int ascending, CompareFunction compFunc)[/code] If I want to save a temporary variable from one of the items in the array, how would I state that? I can't do [code]void *temp = array[i][/code]
[QUOTE=Zwolf11;39383672]I'm trying to sort an array of void pointers given to me in the following way in a c++ program: [code]void mysort(int n, int elementSize, void * array, int ascending, CompareFunction compFunc)[/code] If I want to save a temporary variable from one of the items in the array, how would I state that? I can't do [code]void *temp = array[i][/code][/QUOTE] I believe an array of void pointers would be void **array, wouldn't it? Right now you're trying to dereference array which you can't do without a cast (you're trying to store what array[i] points to, rather than the pointer itself, in temp).
[QUOTE=account;39383947]I believe an array of void pointers would be void **array, wouldn't it? Right now you're trying to dereference array which you can't do without a cast (you're trying to store what array[i] points to, rather than the pointer itself, in temp).[/QUOTE] Yeah, but I can't really do this either. [code]void temp = array[i][/code]
[QUOTE=Zwolf11;39384014]Yeah, but I can't really do this either. [code]void temp = array[i][/code][/QUOTE] But you can do [code]void **array ... void *temp = array[i];[/code]
[QUOTE=account;39384034]But you can do [code]void **array ... void *temp = array[i];[/code][/QUOTE] Hmm...this seems to help a little, but I don't know how I can swap two elements using this method. I'm currently trying [code] void *search = &array[j]; void *temp = &array[i]; *temp = *search; *search = *temp; [/code] But that doesn't make any sense because I'm not actually swapping the data within the two array locations.
[QUOTE=Zwolf11;39384097]Hmm...this seems to help a little, but I don't know how I can swap two elements using this method. I'm currently trying [code] void *search = &array[j]; void *temp = &array[i]; *temp = *search; *search = *temp; [/code] But that doesn't make any sense because I'm not actually swapping the data within the two array locations.[/QUOTE] Why are you taking references? array[j] and array[i] are void pointers, so I don't think you should need to take references. Also are you trying to swap array[j] and array[i]? [code]void *temp = array[j]; array[j] = array[i]; array[i] = temp; [/code]
[QUOTE=account;39384132]Why are you taking references? array[j] and array[i] are void pointers, so I don't think you should need to take references. Also are you trying to swap array[j] and array[i]? [code]void *temp = array[j]; array[j] = array[i]; array[i] = temp; [/code][/QUOTE] Nevermind, I think I've figured it out. I decided to just cast them from void pointers to char pointers and work from there. Thanks for the help.
How do I set up a workspace for OpenGL C++? I'd prefer not to use Visual Studio; currently I use MinGW/g++ for compiling but I just get errors when compiling both on Windows and Linux.
Having so much trouble with Python code. "Design a modular program that asks the user to enter a distance in kilometes, and then converts that distance to miles. The conversion formula is as follows: Miles = kilometers x0.6214 Where do I even start? I've tried writing I keep getting syntax errors in IDLE so I don't know X_X Thanks <3
[QUOTE=Quesadilla;39386054]Having so much trouble with Python code. "Design a modular program that asks the user to enter a distance in kilometes, and then converts that distance to miles. The conversion formula is as follows: Miles = kilometers x0.6214 Where do I even start? I've tried writing I keep getting syntax errors in IDLE so I don't know X_X Thanks <3[/QUOTE] Use [B]raw_input [/B]to get their input, store it in a var, do the conversion, and print it out using [B]print[/B].
Pretty sure he's using Python 3, and [I]raw_input()[/I] doesn't exist in Python 3. Use [I]input()[/I] instead.
[QUOTE=esalaka;39364374]The long long type is required to be at least 64 bits, and as far as I'm aware it is exactly 64 bits on x86_64.[/QUOTE] "x86_64" is more than one thing. Either way, C and C++ define it differently so it's stupid to rely on this.
[QUOTE=Quesadilla;39386054]Having so much trouble with Python code. "Design a modular program that asks the user to enter a distance in kilometes, and then converts that distance to miles. The conversion formula is as follows: Miles = kilometers x0.6214 Where do I even start? I've tried writing I keep getting syntax errors in IDLE so I don't know X_X Thanks <3[/QUOTE] I'd love to help, add me on Steam or Google+ or something. Helping people will help me understand things more as well :)
okay my question runs down to Can you get a value from an object elsewhere while said object is having a method ran from a thread and updating the value?..
I know this isn't [I]exactly [/I]a programming related question, but it has to do with computer science, so I'll ask it here. I'm going to be applying to college soon and I'm having a bit of trouble understanding the application process. So here's my plan: -Attend a community college for 2 years and receive my Associates degree in Computer Science. -Transfer to a state college for 2 more years and receive my Bachelors degree in Computer Science. Now, is this idea practical, or even possible? The only reason I don't want to spend 4 years at the state school is because it would be too expensive. Another problem I'm having is applying for certain courses. Is Computer Science actually called Computer Science? Because I don't see that on the application for the community college. [IMG]http://i.imgur.com/FuiQrVn.png[/IMG] Is Computer Systems Technology the same as Computer Science? If I take Computer Systems Tech will I be able to transfer into a Computer Science course at the state school and earn my BS? I'm really confused and I'm sure you guys had to go through stuff like this, so any help is appreciated. Thanks.
[QUOTE=cody8295;39392184]I know this isn't [I]exactly [/I]a programming related question, but it has to do with computer science, so I'll ask it here. I'm going to be applying to college soon and I'm having a bit of trouble understanding the application process. So here's my plan: -Attend a community college for 2 years and receive my Associates degree in Computer Science. -Transfer to a state college for 2 more years and receive my Bachelors degree in Computer Science. Now, is this idea practical, or even possible? The only reason I don't want to spend 4 years at the state school is because it would be too expensive. Another problem I'm having is applying for certain courses. Is Computer Science actually called Computer Science? Because I don't see that on the application for the community college. [IMG]http://i.imgur.com/FuiQrVn.png[/IMG] Is Computer Systems Technology the same as Computer Science? If I take Computer Systems Tech will I be able to transfer into a Computer Science course at the state school and earn my BS? I'm really confused and I'm sure you guys had to go through stuff like this, so any help is appreciated. Thanks.[/QUOTE] Go to the state school, and ask their academic advisors.
Does anyone know the best way to determine if someone's OpenGL supports GLSL's texelFetch function? After doing some research I haven't found any good way. One library I found checked if the GLSL version was greater than 1.3 OR if GL_EXT_gpu_shader4 is supported, but my linux machine meets both of these requirements and does not support texelFetch. Perhaps there is some resource online to outline the requirements for all GLSL functions? ([img]http://www.facepunch.com/fp/ratings/rainbow.png[/img]?)
[QUOTE=aurum481;39390637]okay my question runs down to Can you get a value from an object elsewhere while said object is having a method ran from a thread and updating the value?..[/QUOTE] Which language? I know that in C/C++ you can do this, but you might get unexpected results depending on what this value is. If it's C#, then you might get cross thread exceptions if you're trying to access for example Form/WPF widgets from other threads than the main thread.
[QUOTE=Robbis_1;39399017]Which language? I know that in C/C++ you can do this, but you might get unexpected results depending on what this value is. If it's C#, then you might get cross thread exceptions if you're trying to access for example Form/WPF widgets from other threads than the main thread.[/QUOTE] I posted earlier with C# code. And I had it throw me cross thread exceptions but fixed them.
Does anyone know of a place where i can learn about code and project formatting? How to name variables, how to use headers and general coding ethics. I feel i get lost in my own code sometimes because i havent placed my things properly. Basically I want to get better structure in my projects. Currently reading c++
Sorry, you need to Log In to post a reply to this thread.