Thanks in advance.
Passing pointer of a struct to a function
#3 Members - Reputation: 269
Posted 05 May 2011 - 10:22 PM
What if the function is in a separate class which doesn't know what type of data *foo is?
The reasoning behind this is that I want to be able to make use of user defined data structures in the class.
Generally, the data structures will be defined after the class headers have been included in to the project. So, initially, the class wont know about what this data is.
#4 Members - Reputation: 269
Posted 05 May 2011 - 11:32 PM
From main program;
thing->registerData(&t1); \\ sends address to class
Within the class;
void registerData(void *pSt1)
{
std::cout<<"Check address within class:"<<pSt1<<"\r\n";
}
How do I tell the function within the class what the structure member data types were, without declaring the structures globally?
#6 Members - Reputation: 269
Posted 06 May 2011 - 12:00 AM
But, I see your point. My brain has been frying just thinking about it
I am probably thinking about it the wrong way. In greater thought (if there is such a thing) I probably only need to know the address and size of the structure for the purposes that I am after, not the data in-between. Which I now have both.
I'll get back to you after a couple of tests.
#8 Members - Reputation: 101
Posted 06 May 2011 - 01:12 AM
#9 Members - Reputation: 296
Posted 06 May 2011 - 01:29 AM
MyStruct.h
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
Another option might be to pass in (alongside the struct), a function. The function would be the delegate for adding to the class.
class Thing
{
public:
[size="2"][color="#0000ff"][size="2"][color="#0000ff"] typedef[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] (*Func)(Thing*, MyStruct*);
[/size] void RegisterData(MyStruct *myStruc, Func func)
{
func(this, myStruct);
}
};
Probably incorrect syntax there though.
#10 Moderators - Reputation: 5034
Posted 06 May 2011 - 02:11 AM
// 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);
}
But if you tell us what you are trying to do, as SenetorBobDole suggests, we can give you concrete advice.
#11 Members - Reputation: 2408
Posted 06 May 2011 - 02:13 AM
or supplying a typedef.
In C++ the only differences between a struct and a class are the default access and inheritance types (public for structs, private for classes).
#12 Members - Reputation: 1551
Posted 06 May 2011 - 11:44 AM
// 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);
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
#13 Members - Reputation: 100
Posted 06 May 2011 - 11:45 AM
But I was waiting for them to give me more info on what they need... :-)
#14 Crossbones+ - Reputation: 1158
Posted 06 May 2011 - 12:20 PM
In C++ a struct is a class, the compiler generates the same code when it sees "struct" or "class". So yes you can inherit from whatever you want, you can even define a struct as interface.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... :-)
#15 Members - Reputation: 2408
Posted 06 May 2011 - 02:42 PM
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... :-)
With respect, I'd prefer these fora not to descend into guesswork. There are enough sites like that around already.
God, I miss the old GDNet sometimes.
#17 Members - Reputation: 277
Posted 07 May 2011 - 05:23 AM
I think what PrestoChung did was quite good
struct foo{
int m;
};
void dofoo( foo* afoo ){
++afoo->m;
}foo contains all the data that will be used by dofoo.All types that you wish to use in dofoo inherits from foo.
struct bar : public foo{
int a;
};You can now pass a bar pointer to dofoobar mybar; dofoo( &mybar );dofoo does not have to know that there is a type named bar. All it has to know is the type foo.
When using inheritance it is very common to make use of virtual functions. If you don't know what that is I suggest you learn about it because it allows you to have different functions run for different types.







