C++ :: Pass Pointer To Member Function (polymorphic Structure)?
Jan 3, 2015
I am trying to create a callback system for input events in my game engine.
Here is a cut down version of the EventManager.h file
#include "Controls.h"
#include "Object.h"
enum MouseEventType{PRESSED, POINTER_AT_POSITION, PRESSED_AT_POSITION };
[Code].....
This works if the function pointer being passed to the event manager is not a member function.
I have two other classes Scene and Object that could potentially use this EventManager to create callback events. Scene and Object are both pure virtual objects. How can I pass a pointer to a member function of the child classes of both Scene and Object? I am fine with just having two separate watchEvent functions for Scene and Object but how do I pass the type of the Object or the type of the Scene? The child classes are unknown as they are being created by someone using this game engine.
For example, if I want to make a player object it would be something like
class PlayerObject : public Object{...};
Now that PlayerObject type has to find its way to PlayerObject::functionToCall(). I think I need templates to do this but, since I never used them before
This is how I intend to use this
class OtherScene : public Scene{
void p_pressed(void){
//pause
}
[Code].....
View 6 Replies
ADVERTISEMENT
Dec 7, 2014
Why doesn't this compile?
struct hi(){
void other();
}histructure;
void hi::other(){
std::cout << "Hi!" << std::endl;
[Code] ....
Makes no sense the structure is written before the structure member function is called so why is there compile errors ??...
View 3 Replies
View Related
Oct 31, 2013
I have a struct with several members, for instance:
insert
Code:
typedef struct {
float u;
float v;
float w;
} mystruct;
main ()
mystruct test;
...
I have a piece of code that should operate on test.u that looks like this:
switch (j) {
case 0:
f = test.u + ...;
break;
case 1:
f = X - test.u;
break;
case 2:
f = test.u + 2;
break;
}
Now I would like to use a similar piece of code but for the other members, test.v and test.w. In principle I could copy the piece of code above and just replace test.u by test.v or test.w. Since it is the same code for all members, I would like to avoid this (copying many times the same piece of code) and replace it by a call to function. The question is, how to pass the name of the struct member I am considering to the function ? How can I tell the function, operates on member .u or .v ? Would it be possible to have a generic piece of code
insert
Code:
function (member y,...)
switch (j) {
case 0:
f = test.y + ...;
break;
case 1:
f = X - test.y;
break;
case 2:
f = test.y + 2;
break;
}
where the function could be called with function(u) or function(w) and would replace automatically .y by .u or .w ?
View 3 Replies
View Related
Mar 14, 2013
I'm trying to call a function via a function pointer, and this function pointer is inside a structure. The structure is being referenced via a structure pointer.
Code:
position = hash->(*funcHash)(idNmbr);
The function will return an int, which is what position is a type of. When I compile this code,
I get the error: error: expected identifier before ( token.
Is my syntax wrong? I'm not sure what would be throwing this error.
View 3 Replies
View Related
Nov 8, 2013
In my project, GreedyKnap::calKnap(int nItem, int nCapacity, float fWeights, float fProfits);
This function include two array member pass as parameter. how can i do this?
View 1 Replies
View Related
Apr 12, 2013
i need to pass myboard.board (board is in the class Cboard and it is an array of int) to a function in a class called piece however this is troubling . i need to pass it as pointer os that i could change its value here under is my code.
main.cpp Code: #include<iostream>
#include"board.h"
#include "pieces.h"
[Code].....
View 7 Replies
View Related
Jan 2, 2014
I'm having a problem understanding something with pointers. I was trying to pass a pointer into a function in MSVC-2013, like
char* charptr;
and then calling
myfunct(charptr);
and then inside the function i would set charptr equal to another char ptr, simply like
charptr = anothercharptr;
But this actually caused a compile failure in MSVC, saying charptr is being used without being initialized. in Code::Blocks it just gives buggy output.
I solved this issue by calling the function like
myfunct(&charptr);
and declaring the function like
myfunct(char**);
and then I had to dereference the charptr in the function when assigning it to another ptr, so
*charptr = anothercharptr;
It seems like you should be able to just pass a ptr into a function and change its address to that of another pointer? My main question is really, what is the value of a pointer? I thought the value of a pointer was just the memory address it contains. But then I had to reference it to pass it into the function.
What is the difference between the value of the char* charptr written as either charptr and &charptr?
View 1 Replies
View Related
Sep 25, 2014
#include <iostream>
using namespace std;
void myfunc(int* ); // what do i put in these parameters to accept a mem loc of a pointer
int main () {
int x = 5;
[Code] .....
SOLUTION:
#include <iostream>
using namespace std;
//Purpose to create a function that returns a pointer to a pointer
int** myfunc(int**);
int main () {
int x = 5;
[Code] ....
View 3 Replies
View Related
Oct 4, 2013
I don't understand how my code not run.
#include "stdafx.h"
#include<iostream>
using namespace std;
struct student{
char name[30];
char birthday[20];
char homeness[50];
float math;
[Code] ....
View 3 Replies
View Related
Jun 13, 2013
void extf(int a) { }
template<typename P>
struct A {
// 1
template< void (*F)(P) >
static void call(P arg) {
[Code]...
Why it is not working? What would be a proper way to pass function pointer as a template parameter?
View 6 Replies
View Related
Apr 21, 2014
I am trying use a print function to print out data in a struct. My questions are:
1. I have to use pass by reference. For the print function, I am passing the struct pointer as a reference, however, I don't want the print function to accidentally change anything. How can I make it use const to ensure that?
2. The deleteprt function doesn't look right to me. I feel like it should just be delete ptr not delete [] ptr.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
struct Inventory {
[Code] .....
View 9 Replies
View Related
Jun 6, 2012
I am trying to use "remove_if" with a predicate function inside a class. The code intends to remove the grid cells which an agent cannot move into (from among all possible cells).
Code:
void classname::function1()
{
vector<MoorePoint> neighbors;
....
[Code]....
That code would work if it was not in a class and the predicate was not a member function. However, now I receive long error messages which I guess refer to incompatibility of remove_if template with the predicate parameter (one error includes : error C2064: term does not evaluate to a function taking 1 arguments).
View 2 Replies
View Related
Aug 24, 2014
I am trying to wright a program that takes student grade data from a command line file, calculates a final grade, and copies the final grades to an output file. So far I have two functions, one that creates a student structure by allocating memory for it and returning its address, and one that should take that address and fill it with data from a line from the input file. My ultimate goal is to use a linked list to connect all the structs, but for now I just want to get the functions working. When I run what I have so far, I get an error C2440 (using visual 2010) that says "cannot convert from 'cStudent *', to 'cStudent', and points to the line where I call my fill function. How should structure pointers be passed?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student // Declaring student structure globally.
[Code] .....
Also, here is a sample of what a line from the input file would look like:
Bill Gates, 60, 54, 38, 62, 65, 60, 50
View 2 Replies
View Related
May 15, 2013
I have a class with member functions and a pointer like this:
Code:
class MyClass{
private:
typedef void(MyClass::*memFnPtr_t) ();
public:
memFnPtr_t fnptr;
void fn1();
void fn2();
};
Now in a regular function I create an instance of the class and try to assign the pointer:
Code:
MyClass mc;
mc.fnptr = &mc.fn1;
The compiler does not like it and suggests this instead:
Code:
mc.fnptr = &MyClass::fn1;
This seems to work but what if I have two instances of the class:
Code:
MyClass mc1, mc2;
How does the compiler know to distinguish between
Code:
mc1.fn1
and
Code:
mc2.fn1
when the assignment now looks identical:
Code:
mc1.fnptr = &MyClass::fn1;
mc2.fnptr = &MyClass::fn1;
View 14 Replies
View Related
Aug 19, 2014
I have the following problem: I am using NLOpt for optimization. The API provides functions to set the objective. This is done as follows:
double objective(const vector<double> &x, vector<double> &grad, void *data)
{
return x[1]*x[0];
}
int main(){
nlopt::opt opti(nlopt::LD_MMA,2);
opti.set_min_objective(objective,NULL);
vector<double> x(2);
[Code]....
Now I want to make the function objective a member of a class:
class Foo {
public:
double objective(...){..}
};
How can I give this method to opti.optimize? If I make objective static I can use
opti.optimize(Foo::objective,NULL);
but I do not want to have a static member. Is it possible to create an object of type Foo and give it to opti.optimize?
View 1 Replies
View Related
Nov 26, 2013
I have the following piece of code.
Code:
#include<iostream>
using namespace std;
class Test {
public:
Test(){cout<<"Test"<<endl;}
void fun() {
int i=5;
[Code] ...
Compiled with g++.
Executing this give output fun5.
It is correct? I have not allocated any object and so this pointer is not created. Then how it is able to run and call the function.
View 4 Replies
View Related
Oct 7, 2014
Is it possible to assign a value to structure member inside the structure.like.....
struct control{
char tbi:2 =0;
char res:1 =0;
};
View 6 Replies
View Related
Jan 6, 2013
I'm currently programming a server which uses multiple threads- I have a class for one map in the game. Each map has a thread for timed events(tile regeneration, NPC regeneration, etc.), and a thread for handling NPCs(movement, combat, etc.). A basic structure of the class looks like this:
class Region {
public:
/* game values are here, they are public so
they can be accessed from outside of the class
inside of packet-handling functions and such */
int value;
void *Function();
[Code] ....
The program crashes when I use a member of the same class the function is located in- in the context I have shown about it would crash on "value++".
View 11 Replies
View Related
Jun 20, 2014
i really don't know why has a error in my code, that pass a pointer of pointer (name of a matrix with 2 dimensions). Here is the source code of a simple example where appears segmentation fault when execute (but compiles normal):
#include <stdio.h>
#define LINHAS 3
#define COLUNAS 5
float a[LINHAS][COLUNAS];
void zeros(float **p,float m, float n){
int i,j;
for(i=0;i<m;i++)
[Code]...
View 6 Replies
View Related
Jul 13, 2014
I have a project for class where I have to create a structure and get user input for 3 structure variable arrays of 10. I am trying to figure out how I can use the same function to fill my different section of variables.
My Structure is an employee file of ID number, name, hours, payrate, and then gross pay. I have to create a function for each input function. I am confused on how to pass the structure variable so that I do not have to write 3 functions for each input. I would like to be able to get all the info for the first structure variable and then recall the same 5 functions for the next before moving along. I hope that I have been able to make this clear. Here is my code:
#include<iostream>
#include<iomanip>
#include<cctype>
#include<string>
#include<cstring>
using namespace std;
struct PayPeeps_CL//Payroll Structure {
[Code] .....
View 14 Replies
View Related
Dec 29, 2012
Code:
struct NewPlayer {
int level;
int intelligence;
int damage;
};
int CharacterInfo(NewPlayer MageWizard, int Clevel,int Cint, int Cdam)
[Code] .....
View 1 Replies
View Related
Jan 7, 2015
I have started working with structures so here's a side project from my text book. It's purpose is fairly simple; it asks for the sales of each quarter of the year from 4 different divisions and then calculates the average quarterly sales and total annual sales and finally displays all the data. My problem is that in the function "displayCompanyInfo" the statement
std::cout << "Division " << R.division_name << std::endl;
does not display the name of the division. With that in mind here is the code:
#include <iostream>
#include <string>
struct CompanyInfo
{
[Code]....
As you can see the last part of the output has statements that say "Division" however they do not say the name of the division afterwards. I don't understand why that is?
View 2 Replies
View Related
Dec 16, 2012
In my MFC, CMyPorpertyPageDlg is derived from CPropertyPage. How to access its member function from a nonmember function in the same CPP file?.
void Non_Member_Get_PorpertyPage()
{
CMyPorpertyPageDlg* pPageDlg = ....
}
View 4 Replies
View Related
Feb 17, 2015
See the simple code below. The compiler gives message:
assigning to type "char[20]" from type "char *".
I've tried everything else that seems reasonable but none works. How can I assign string Hello to the structure member?
Code:
int main() {
struct strc1
{
char msg1[20];
char msg2[5];
}
talk;
talk.msg1 = "Hello";
}
View 5 Replies
View Related
Mar 24, 2014
is it possible to make something like that?
struct type_name
{
char Status[i];
string Status[j];
.
.
} object_names;
The problem is I dont know how many statuses my object will have. Is it possible to make it in an dynamic array?
View 3 Replies
View Related
Sep 3, 2013
template <typename T> struct avl_tree {
T data;
int balance;
struct avl_tree <T> *Link[2];
static int (*comp)(T, T);
};
In main, I have a function like so:
int compare(int a, int b) {
return ( a - b );
}
Now how do I assign the function pointer in my avl_tree class to the compare function?
I did this:
int (avl_tree<int>::*comp)(int, int) = compare;
But I got the compiler error:
Tree_Test.cc: In function ‘int main()’:
Tree_Test.cc:27:42: error: cannot convert ‘int (*)(int, int)’ to
‘int (avl_tree<int>::*)(int, int)’ in initialization
View 12 Replies
View Related