C++ external member referencing??

Started by
3 comments, last by Crow-knee 22 years, 6 months ago
I have a fairly basic problem. I am making my debut with C++ (after beginning in C) using that wonderful API DirectX 7. To give a brief explantion of what I have : DirectDraw class - starts up direct draw +LPDIRECTDRAW4 lpdd4 +...other stuff surface class - contains the surface info +LPDIRECTDRAWSURFACE lpdds +CreateSurface() <---- requires lpdd4 The issue comes when I want to use the surface class. It requires the lpdd4 direct draw interface pointer created in the DirectDraw class. I don''t want to have to pass this everytime I want to use a surface - is there anyway I can reference the data members from the DirectDraw class like using "extern" and a global in C? Any help is appreciated. Thanks.
Advertisement
In your DirectDraw class:
friend class DDSurface; // or whatever your surface class is

The surface class will now have access to all DirectDraw data members regardless of scope. It will still need an object to dereference from, unless the member is static.


If you''re not going to bother taking advantage of object oriented methodology, then don''t program in C++! Stick to straight C instead of switching to C++ and not using any of its features.

Basically your question is asking "how can I get around data encapsulation?" If you have to ask that question, then it might be necessary to question your program or objects'' design!
So, Pyabo, you would recommend just passing the direct draw interface (lpdd4) when needed?
I guess the generalised question I am asking is :
If a class creates an instance of another class that requires a reference to a data member in the first do you...
a) redesign the classes
b) make the classes friends
c) pass the data member in each call
d) store a copy of the data member in the created class
e) some other magic I don''t know about
The thing is that the surfaces need to know about direct draw and direct draw needs to know about the surfaces - is this a circlic reference?
At the moment I am passing the parameter each time which seems to work ok - just wondering if there was another way.
try something like this
  class DirectDraw{public:// member funnctionsLPDIRECTDRAW4 getDirectDrawObject()  { return lpdd4;  }// more member fuunnctionsprivate:LPDIRECTDRAW4 lpdd4;}// in another fileclass Surface{public:  int initialize(DirectDraw* dd);private:  LPDIRECTDRAW4 lpdd4;}// the intialize fuunctionint Surface::initialize(DirectDraw* dd){  lpdd4 = dd->getDirectDrawObject();// do more stuff}  

now you can access the lpdd4 inboth classes and they both point to the same instance of direct draw

"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

This topic is closed to new replies.

Advertisement