Banner
Views: 236,001,079
Time: 2013-05-20 09:22:41 AM
20 users online: aj6666, Donaldthescotishtwin, Falconpunch, GaahPL, o Giga, Gloomy Star, Grav, King Dedede, lolilul, Lost ECHO, Pokeymeister80, o Richard Nixon, Rockythetigre, o RPG Hacker, o ShadowPhoenix, Sokobansolver, o Tahixham, Tarek701, TheOtherGuy25, X-cniS - Guests: 18 - Bots: 15Users: 22,848 (1,292 active)
Latest: Yasuo
Tip: If you give a muncher tile custom graphics, it will still act like a coin when a silver P-switch is active, whether it looks like a coin or not. So make sure to use ExAnimation if there's a silver P-switch in the area.
Generic C++ questions
Forum Index - Hobbies - Computers & Technology - Generic C++ questions
Pages: « 1 2 »
Okay, this question isn't really required for anything, but I'm still curious. Consider the following 65c816 ASM:

Code
JumpTable: 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.
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.
Originally posted by HuFlungDu
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.


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).
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:

Code
void (*jumps[12])();


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.
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.
Ah, thanks. I don't know why they had to go and make function pointers so amazingly unintuitive.
Originally posted by Kipernal
Ah, 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.
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.
Originally posted by Kipernal
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);



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.
Originally posted by p4plus2
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.)


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 p4plus2
Usually, 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 p4plus2
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.


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.
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.

Quote

*snip*


Sorry, but that code tells me near nothing, I mean more of an implementation not a class declaration.
Originally posted by p4plus2
Sorry, 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...?

Code
void 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 Us


Total queries: 29

Menu