Jump to content
  • Advertisement
Sign in to follow this  
chbrules

Pointing to a struct between classes

This topic is 4831 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

I still get tripped up on pointers when I use them half the time, at least in difficult (to me) situations like this. Simple enough, I copied my struct into 2 classes, then I create identical pointers in each of the same struct datatype. Next I made a set and get function from the one class that has info stored in that struct to the class which is waiting to a pointer to that struct to read it's values. Now I Don't know which way I'm going here w/ references and stuff, so maybe someone could help me. What I have is: ptr *Ptr; -Class 1 ptr getPtr(); -Class 1 Cls2.setPtr(getPtr); - global void setPtr(); -Class 2 ptr *Ptr; -Class 2 Now I just return the reference (&) of *Ptr through the getPtr() function, then I just reassign it to *Ptr in class 2 with the dereference (*) operator. Then I get error messages about: Can't convert Class1::Ptr to Class2::ptr and stuff. These aren't the real names of course. I'm just kind of stumped why I can't just pass the memory reference to another class so I don't have to copy the entire struct over again. I hope that's clear for you guys to understand, I don't really know how to explain it. =( *Note: The pointers are private, is that the problem?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by chbrules

ptr *Ptr; -Class 1
ptr getPtr(); -Class 1
Cls2.setPtr(getPtr); - global
void setPtr(); -Class 2
ptr *Ptr; -Class 2

Now I just return the reference (&) of *Ptr through the getPtr() function, then I just reassign it to *Ptr in class 2 with the dereference (*) operator. Then I get error messages about:



From what I see your not returning a reference to a ptr object, your returning a full object. If you want to return a reference it would be

ptr &getPtr(); // returns a ptr reference
ptr getPtr(); // returns a full ptr object

if your storing a Ptr *, just do
ptr *getPtr(); and it should work...I'm guessing your in a situation like this..


struct foo
{
int m_Int;
};

class bar
{
foo m_foo;

public:
// remember that & returns the address, this is not a reference
foo *getPtr(){ return &m_foo;}
// this would be a reference
foo &getRef(){ return m_foo;}
};

class baz
{
foo *m_pMyFooToo;

public:
void setPtr( foo *pFoo ) {m_pMyFooToo = pFoo; }
};

void main()
{
bar barObject;
baz bazObject;

bazObject.setPtr( barObject.getPtr() );
}




I hope I'm understanding your problem correctly.
hth

Share this post


Link to post
Share on other sites
also in addition to the previous assumption, if you are actually passing around pointers, then you may want to try passing them around as void pointers and then casting them to the preferred type. Not exactly sure what your problem is though, after you post some code things should get cleared up.

Share this post


Link to post
Share on other sites
Sorry about that. Here's some code:

CMap Class:

struct sMap{ int *tile_ID; bool *clip; };
sMap *Map; //struct array
sMap& getMap(){ return *Map; }; //Get the map pointer


CImage class:

struct sMap{ int *tile_ID; bool *clip; };
sMap *Map; //just a pointer
void setMap(sMap *MapD){ Map = MapD; };


Inside main function:

CMap Map;
CImage Image;

Image.setMap(Map.getMap());


Hopefully that will clear up where I'm comming from. There error I get is:

main.cpp(54) : error C2664: 'CImage::setMap' : cannot convert parameter 1 from 'CMap::sMap *' to 'CImage::sMap *'

It can't convert it to the same type? Is it because I don't have the struct in the main function too?

Share this post


Link to post
Share on other sites
Quote:
Original post by chbrules
main.cpp(54) : error C2664: 'CImage::setMap' : cannot convert parameter 1 from 'CMap::sMap *' to 'CImage::sMap *'

It can't convert it to the same type? Is it because I don't have the struct in the main function too?


Even though they may be structurally the same, they aren't the same types. One is nested in CMap, the other in CImage.

Share this post


Link to post
Share on other sites
That's what I thought, is there any way around this? =/

Copying all the contents over will just waste a lot more memory, but if I must I will. Any solutions?

Share this post


Link to post
Share on other sites
Quote:
Original post by chbrules
That's what I thought, is there any way around this? =/


Use a common, non-nested type would be the simplest way to go about it. Sharing a private base class with just that struct declared in it might work. Slapping a reinterpret_cast will work, but you'll have to make sure the two struct always stay in sync (which is what the previous two approaches are trying to take care of).

Share this post


Link to post
Share on other sites
The base class idea sounds great, I think I'll give that a try. I've really never had a use for inheritence till now, might as well give it a shot, learn some new stuff. ;) Thanks!

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!