Sending a struct to a class
- lonewolff
- Ogre Magi
- Posts: 1207
- Joined: Wed Dec 28, 2005 12:58 am
- x 6
Sending a struct to a class
Hi Guys,
I have an interesting problem that I am trying to work out a solution for and no it is not homework, I am 36, I don't have homework
This is what I am trying to accomplish...
I have a class that is defined in its own header file and cpp file. I want it to be able access data from user-defined data structures. So, the class files are included even before the data structures are even defined.
If I create a struct within my main.cpp file I can send the address of the struct to the class instance by using something like...
thing->registerStruct(&myStruct)
...and recieve the address in to the class with;
void registerStruct(void *pStruct)
{
std::cout<<"Address of struct in memory is:"<<pStruct<<std::endl;
}
But, the function in the class doesn't know what the data members are meant to be. So, all I have is an address.
How can I go about sending the information to the class as to what the data structure is meant to contain (if it can even be achieved)? So, I can edit the values of the struct from within the class.
Any ideas would be absolutly awesome!
I have an interesting problem that I am trying to work out a solution for and no it is not homework, I am 36, I don't have homework
This is what I am trying to accomplish...
I have a class that is defined in its own header file and cpp file. I want it to be able access data from user-defined data structures. So, the class files are included even before the data structures are even defined.
If I create a struct within my main.cpp file I can send the address of the struct to the class instance by using something like...
thing->registerStruct(&myStruct)
...and recieve the address in to the class with;
void registerStruct(void *pStruct)
{
std::cout<<"Address of struct in memory is:"<<pStruct<<std::endl;
}
But, the function in the class doesn't know what the data members are meant to be. So, all I have is an address.
How can I go about sending the information to the class as to what the data structure is meant to contain (if it can even be achieved)? So, I can edit the values of the struct from within the class.
Any ideas would be absolutly awesome!
-
- Bronze Sponsor
- Posts: 614
- Joined: Mon Apr 14, 2003 11:48 pm
- Location: Was LA now France
- x 25
Re: Sending a struct to a class
How about defining the struct in a separate header file and including it in both classes?
- xavier
- OGRE Retired Moderator
- Posts: 9481
- Joined: Fri Feb 18, 2005 2:03 am
- Location: Dublin, CA, US
- x 22
Re: Sending a struct to a class
And give the struct some (pure) virtual members (make it an interface, IOW) so that you have a type you can hang on those pointers.
-
- Gnoblar
- Posts: 11
- Joined: Sun Mar 27, 2011 12:23 pm
- x 1
Re: Sending a struct to a class
Or read about C++ RTTI.
- xavier
- OGRE Retired Moderator
- Posts: 9481
- Joined: Fri Feb 18, 2005 2:03 am
- Location: Dublin, CA, US
- x 22
Re: Sending a struct to a class
@lonewolff: I think I understand what you are asking. You are basically asking for "reflection" in the .Net sense of the word? Essentially, your code only gets a reference to void* and you can't cast that to anything in particular because the object can be anything (and there's no way to include all of the headers you might need)?
-
- Bugbear
- Posts: 807
- Joined: Sun May 14, 2006 2:24 pm
- Location: Melbourne, Australia
Re: Sending a struct to a class
ok you probably don't what to use templates then. eg:So, I can edit the values of the struct from within the class.
Code: Select all
template <class T>
class X
{
T m_pStruct;
void registerStruct(T& pStruct)
{
m_pStruct = pStruct;
}
}
- lonewolff
- Ogre Magi
- Posts: 1207
- Joined: Wed Dec 28, 2005 12:58 am
- x 6
Re: Sending a struct to a class
Thinking deeper about this one, I think I have realised that I probably dont really need to modify the contents of the data within the struct from the class.
For the purposes I require, I believe that I will only need the start address of the struct and the size of the struct. Which I can pass both of these easily to the class with a void pointer and a sizeof() call.
I'll get back to you
For the purposes I require, I believe that I will only need the start address of the struct and the size of the struct. Which I can pass both of these easily to the class with a void pointer and a sizeof() call.
I'll get back to you
- lingfors
- Hobgoblin
- Posts: 525
- Joined: Mon Apr 02, 2007 12:18 am
- Location: Sweden
- x 79
Re: Sending a struct to a class
If you do sizeof on the pointer, it will return "4" (or whatever the pointer size is on your system, but most likely 4). If you try to dereference a void pointer, I think you get an error.
- lonewolff
- Ogre Magi
- Posts: 1207
- Joined: Wed Dec 28, 2005 12:58 am
- x 6
Re: Sending a struct to a class
I am sending the sizeof() as another parameter so the value I am getting is correct.
- volca
- Gnome
- Posts: 393
- Joined: Thu Dec 08, 2005 9:57 pm
- x 1
- Contact:
Re: Sending a struct to a class
If you'd be still wanting to access the fields of the structs, you'd have to supply that info to the code working with the structs. I once written something similar - There was a function that was called at the beginning of the code execution (once) and mapped the fields of the struct one by one (there was a method "field" that took at least two parameters - name, and relative member pointer - see line 327 in the link below). This mapping could then be used to access the member variables.
See here: http://opde.svn.sourceforge.net/viewvc/ ... iew=markup
Here is the basic initialization code:
http://opde.svn.sourceforge.net/viewvc/ ... iew=markup
I hope this will help you
See here: http://opde.svn.sourceforge.net/viewvc/ ... iew=markup
Here is the basic initialization code:
http://opde.svn.sourceforge.net/viewvc/ ... iew=markup
I hope this will help you