Jump to content
  • Advertisement
Sign in to follow this  
DarkRonin

Passing pointer of a struct to a function

This topic is 2696 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Thanks for the reply.

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.

Share this post


Link to post
Share on other sites
I worked out how to get the address with a void pointer. But still am not sure how I can access the struct data.

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?

Share this post


Link to post
Share on other sites
If you don't know the parameter's type at the time you write the function, you could use templates. But, if you're passing a struct, that could be any combination of data. How is your function going to know what to do with it?

Share this post


Link to post
Share on other sites
That's exactly my problem.
But, I see your point. My brain has been frying just thinking about it :lol:

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

I'll get back to you after a couple of tests. :cool:

Share this post


Link to post
Share on other sites
I planned a response that talked about interfaced and virtual classes when I realized something, it won't work with a struct. What is it that you are trying to do, as in what specific problem are you trying to solve with this?

Share this post


Link to post
Share on other sites
I might have the wrong idea, but have you looked at inheritence? It's poor form to be passing void pointers around. If you're going to have a function that operates on user defined structures, you might want to declare a base class which has the basic properties that you want to operate on, and let users define subclasses of it. This way you can pass your structure around, and if your users want to create a subclass then they can, and add stuff to it, whilst still allowing your function to work as normal on it.

Share this post


Link to post
Share on other sites
Only way you can really do it is either to provide a common interface but, as SenetorBobDole points out it won't work with structs - or you include the definitation of each struct in the implementation of the class. Thats pretty much the only option, before you can use it, it must be defined. You could do so with forward declarations but it might not be entirely what you want:

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:
[color="#0000ff"][color="#0000ff"] typedef [color="#0000ff"][color="#0000ff"]void (*Func)(Thing*, MyStruct*);
void RegisterData(MyStruct *myStruc, Func func)
{
func(this, myStruct);
}
};


Probably incorrect syntax there though.

Share this post


Link to post
Share on other sites
You could do this:

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!