Passing pointer of a struct to a function

Started by
15 comments, last by Wooh 12 years, 11 months ago
What's all this "It won't work with structs" stuff about? OP is clearly using C++, not C, since he/she does foo myfoo; rather than struct foo myfoo;
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).
Advertisement
Here's a simplified C solution (will work in C++)


// 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);

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

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

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.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion


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.
What are you really trying to do and why?

Do you have to use C++? C++ is a statically typed language. From what I can piece together of what you are doing, statically typed languages are a poor fit for this kind of problem.
I don't think the problem is statically typed language but people trying to solve it in the wrong way.

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

This topic is closed to new replies.

Advertisement