Sending a struct to a class

Get answers to all your basic programming questions. No Ogre questions, please!
Post Reply
User avatar
lonewolff
Ogre Magi
Posts: 1207
Joined: Wed Dec 28, 2005 12:58 am
x 6

Sending a struct to a class

Post by lonewolff »

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 :lol:

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! 8)
Slicky
Bronze Sponsor
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

Post by Slicky »

How about defining the struct in a separate header file and including it in both classes?
User avatar
xavier
OGRE Retired Moderator
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

Post by xavier »

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.
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
arrowdodger
Gnoblar
Posts: 11
Joined: Sun Mar 27, 2011 12:23 pm
x 1

Re: Sending a struct to a class

Post by arrowdodger »

Or read about C++ RTTI.
User avatar
xavier
OGRE Retired Moderator
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

Post by xavier »

@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)?
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
kneeride
Bugbear
Posts: 807
Joined: Sun May 14, 2006 2:24 pm
Location: Melbourne, Australia

Re: Sending a struct to a class

Post by kneeride »

So, I can edit the values of the struct from within the class.
ok you probably don't what to use templates then. eg:

Code: Select all

template <class T>
class X
{
  T m_pStruct;

  void registerStruct(T& pStruct)
  {
    m_pStruct = pStruct;
  }
}
It may depends what you want to do with the struct in the class. if you wanted to print the values from the struct, then c# reflection solution might come in handly (as xavier mentioned) or you could use another concept such as delegates/function pointers/lamba expressions to pass in the logic of how you want to handle the void*.
User avatar
lonewolff
Ogre Magi
Posts: 1207
Joined: Wed Dec 28, 2005 12:58 am
x 6

Re: Sending a struct to a class

Post by lonewolff »

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 :D
User avatar
lingfors
Hobgoblin
Posts: 525
Joined: Mon Apr 02, 2007 12:18 am
Location: Sweden
x 79

Re: Sending a struct to a class

Post by lingfors »

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.
User avatar
lonewolff
Ogre Magi
Posts: 1207
Joined: Wed Dec 28, 2005 12:58 am
x 6

Re: Sending a struct to a class

Post by lonewolff »

I am sending the sizeof() as another parameter so the value I am getting is correct. :wink:
User avatar
volca
Gnome
Posts: 393
Joined: Thu Dec 08, 2005 9:57 pm
x 1
Contact:

Re: Sending a struct to a class

Post by volca »

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 :)
Image
Post Reply