 |
|
 |
|
| Generic C++ questions |
|
Forum Index - Hobbies - Computers & Technology - Generic C++ questions |
|
Pages: 1 2  |
|
|
|
| Posted on 2011-09-02 08:06:39 PM |
Link | Quote |
|
Okay, this question isn't really required for anything, but I'm still curious. Consider the following 65c816 ASM:
CodeJumpTable: db Jump1,Jump1>>8,Jump1>>16,Jump2,Jump2>>8,Jump2>>16
LDX !JumpNumber ;Is a multiple of 3.
LDA.b JumpTable,x
STA $00
LDA.b JumpTable+1,x
STA $01
LDA.b JumpTable+2,x
STA $02
JMP [$00]
Jump1:
;Code
Jump2:
;Code
I think that's the correct syntax, but if not, hopefully I'm still getting the idea across; instead of doing a bunch of CMP statements I'm just jumping straight to the code I want to execute, which saves tons of time if JumpNumber were, say, 200.
You might be able to see where I'm going with this: Is there any equivalent to this in C, or am I stuck with switch()?
|
| Last edited on 2011-09-02 08:07:24 PM by Kipernal. |
|
| Posted on 2011-09-02 08:40:30 PM |
Link | Quote |
|
Goto.
Other than that, you would have to set up each thing as a function and get a function pointer to it, and put them all in an array of some sort. However, it usually turns out to be better to do a switch for the sake of readability.
|
|
| Posted on 2011-09-02 09:22:45 PM |
Link | Quote |
|
Originally posted by HuFlungDuGoto.
Other than that, you would have to set up each thing as a function and get a function pointer to it, and put them all in an array of some sort. However, it usually turns out to be better to do a switch for the sake of readability.
Unless I'm missing something, goto doesn't accept variables. That article on function pointers, however, was exactly what I was looking for, so thank you (I figured it could be done, but I had no clue what the syntax would be).
|
|
| Posted on 2011-12-11 12:24:56 AM |
Link | Quote |
|
Whee bump.
So back on the topic of function pointers, consider the following:
In the class A's declaration I have jumps defined as an array of 12 pointers to functions:
Then in one of A's functions I have the following:
Code(*A::jumps[state])();
Once the code jumps to the function pointed to by jumps, however, the this pointer becomes corrupted, becoming some sort of random value until the called function returns. Naturally, this makes is pretty much impossible to do...almost anything within that function, so what am I doing wrong here?
|
| Last edited on 2011-12-11 12:25:51 AM by Kipernal. |
|
| Posted on 2011-12-11 02:35:48 AM |
Link | Quote |
|
Function pointers and member function pointers aren't the same. A function pointer just tells which function you want; it doesn't tell which value this should have, and it's therefore set to whatever value it had earlier, or some random value, or whatever.
This or this seems to contain some explainations on how to do it properly.
|
|
| Posted on 2011-12-11 03:13:09 AM |
Link | Quote |
|
|
Ah, thanks. I don't know why they had to go and make function pointers so amazingly unintuitive.
|
|
| Posted on 2011-12-11 04:06:46 AM |
Link | Quote |
|
Originally posted by KipernalAh, thanks. I don't know why they had to go and make function pointers so amazingly unintuitive.
If you are willing to use C++11 look into lambda functions. They are bit more sane to use than direction function pointers.
|
|
| Posted on 2012-01-20 10:09:51 PM |
Link | Quote |
|
You know, the more I use C++ the more I understand the flak people give it. Anyway.
I have a class called Program and a class called Bitmap. Program has a Bitmap member named bmp and a member function named onInit, and Bitmap has a member function named loadFromFile. In Program's onInit function, there is the following line:
bmp.loadFromFile("gfx.png", Bitmap::png);
The problem is that by that line &bmp and &program.bmp (where program is of type Program) report two different values, so any other classes that try to access program.bmp don't get the expected result. I'm guessing the problem is related to the previous one where I was mixing up member functions and normal functions, but I still can't seem to figure out where I'm going wrong.
Incidentally, for anyone wondering "well you said you were taking a programming course in college why don't you know this," it's because they're focusing more heavily on the application than the syntax. I can write code that will produce a Hadamard matrix but I can't get member functions working correctly. I'm not really complaining, though.
|
|
| Posted on 2012-01-21 05:27:02 AM |
Link | Quote |
|
Originally posted by KipernalYou know, the more I use C++ the more I understand the flak people give it. Anyway.
I have a class called Program and a class called Bitmap. Program has a Bitmap member named bmp and a member function named onInit, and Bitmap has a member function named loadFromFile. In Program's onInit function, there is the following line:
bmp.loadFromFile("gfx.png", Bitmap::png);
Any reason to use "onInit" over a constructor? (There are valid reasons, if for example you need to run init multiple times, but that does not seem to be the case here.)
Quote
The problem is that by that line &bmp and &program.bmp (where program is of type Program) report two different values, so any other classes that try to access program.bmp don't get the expected result. I'm guessing the problem is related to the previous one where I was mixing up member functions and normal functions, but I still can't seem to figure out where I'm going wrong.
Usually, you should not be accessing member variables directly outside of the class. Maybe I am missing what you are doing, but are you saying the problem happens before that line or after? This would be a lot easier to answer if you could provide a full code snippet.
|
|
| Posted on 2012-01-21 08:56:18 AM |
Link | Quote |
|
Originally posted by p4plus2Any reason to use "onInit" over a constructor? (There are valid reasons, if for example you need to run init multiple times, but that does not seem to be the case here.)
That's pretty much it, though I suppose that particular line could go in the constructor; I just put everything in onInit for clarity.
Originally posted by p4plus2Usually, you should not be accessing member variables directly outside of the class.
Isn't that the point of public member variables, though?
Originally posted by p4plus2Maybe I am missing what you are doing, but are you saying the problem happens before that line or after? This would be a lot easier to answer if you could provide a full code snippet.
It happens any time I try to access program's bmp member from within one of its member functions. By that point, when I call loadFromFile, it's stored into some Bitmap variable that no one else can access. The code basically just looks like this (without any of the other irrelevant pieces of code):
Code//Program.h
#include "Graphics.h" //includes "Bitmap.h"
class Program
{
public:
Bitmap bmp;
};
//Bitmap.h
class Bitmap
{
int width;
int height;
Uint32* data;
public:
enum filetypes
{
raw,
png
};
void loadFromFile(char* fileName,
Bitmap::filetypes fileType,
int width = 0, int height = 0);
};
In onInit, the first line is the bmp.loadFromFile, so nothing before it could be causing the problem.
|
|
| Posted on 2012-01-21 06:32:05 PM |
Link | Quote |
|
Originally posted by Kipernal
That's pretty much it, though I suppose that particular line could go in the constructor; I just put everything in onInit for clarity.
Strange... But okay,
Quote
Isn't that the point of public member variables, though?
Usually, in C++ public member variables are const. Direct public access can be slightly more error prone if you are not careful. The goal of getting/setters is to isolate how the rest of the program can access particular variables. This enables you to do any sort of tracking, preprocessing, or whatever requirements may be needed. It is similar to the global variable problem. Opinions vary, there is no set in stone approach though. Just worth a thought.
Sorry, but that code tells me near nothing, I mean more of an implementation not a class declaration.
|
|
| Posted on 2012-01-21 07:01:55 PM |
Link | Quote |
|
Originally posted by p4plus2Sorry, but that code tells me near nothing, I mean more of an implementation not a class declaration.
What else do you need, then? I can't think of anything else in the code that's relevant, since that's the only time Program's bmp member is accessed by that point in the program, unless you want the definition of loadFromFile...?
Codevoid Bitmap::loadFromFile(char* fileName, Bitmap::filetypes fileType, int w, int h)
{
if (fileType == raw)
{
char* c = openFile(fileName, false);
if (c)
{
Bitmap::loadFromMemory(c, width, height);
free(c);
}
}
else
{
std::vector<unsigned char> buffer, image;
LodePNG::loadFile(buffer, fileName);
LodePNG::Decoder decoder;
data = (Uint32*)decoder.decode(image, buffer.size() ? &buffer[0] : 0, (unsigned)buffer.size());
if(decoder.hasError())
std::cout << "error " << decoder.getError() << ": "<<
LodePNG_error_text(decoder.getError()) << std::endl;
width = decoder.getWidth();
height = decoder.getHeight();
}
}
Incidentally, I feel I should mention that it works if I put program.bmp.loadFromFile, and while that technically works, it doesn't really help much in the long run.
|
| Last edited on 2012-01-21 07:05:50 PM by Kipernal. |
|
|
Pages: 1 2  |
|
|
|
|
Forum Index - Hobbies - Computers & Technology - Generic C++ questions |
|
|
 |
|
 |
The purpose of this site is not to distribute copyrighted material, but to honor one of our favourite games.
Copyright © 2005 - 2013 - SMW Central Legal Information - Link To UsTotal queries: 29
|
|
|
|