Original Post
If I have a struct, how can I send the pointer of that struct over to a function so I can access or modify the data within the struct?
Thanks in advance.
Thanks in advance.
struct foo{
int m;
};
void dofoo( foo* afoo ){
++afoo->m;
}
foo myfoo;
dofoo( &myfoo );
struct MyStruct
{
int a;
}
Thing.h
struct MyStruct;
class Thing
{
public:
void RegisterData(MyStruct *myStruct);
};
Thing.cpp
#include "Thing.h"
#include "MyStruct.h"
// We can now use MyStruct
class Thing
{
public:
[color="#0000ff"][color="#0000ff"] typedef [color="#0000ff"][color="#0000ff"]void (*Func)(Thing*, MyStruct*);
void RegisterData(MyStruct *myStruc, Func func)
{
func(this, myStruct);
}
};
// Some header
namespace detail
{
void reallyFrobnicate(void *pointer, int size);
}
template<typename T>
void frobnicate(T *pointer)
{
detail::reallyFrobnicate(pointer, sizeof(T));
}
// Your source file
struct PrivateStruct
{
// ...
};
void example()
{
PrivateStruct s;
frobnicate(&s);
}
// in one file
struct
{
int myInt;
int myInt2;
unsigned char myByteArray[12];
} myStruct;
void myFunction(void *myData)
{
myStruct *pMyStruct;
pMyStruct = (myStruct *)myData;
pMyStruct->myInt++;
}
//in a different file
{
SomeDataType myData;
.
.
.
myFunction((void *)&myData);
You are right, but they said they were using a struct. I know that a class is essentially a struct with functions, but can a struct inherit from a class? I haven't tried, but even if it can't, I was going to suggest they use a class instead of a struct, since it would work the same, and give them what they want.
But I was waiting for them to give me more info on what they need... :-)
You are right, but they said they were using a struct. I know that a class is essentially a struct with functions, but can a struct inherit from a class? I haven't tried, but even if it can't, I was going to suggest they use a class instead of a struct, since it would work the same, and give them what they want.
But I was waiting for them to give me more info on what they need... :-)
struct foo{
int m;
};
void dofoo( foo* afoo ){
++afoo->m;
}struct bar : public foo{
int a;
};bar mybar;
dofoo( &mybar );This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more