to access private/public members
okie, i got a few questions, hopefully someone can help clear
my doubts.
lets say i got 2 classes, A & B.
if class B declares an "array of pointers" of "class type" B,
in public, why cant class A''s functions use that array of pointers?
A should be able to use whatever is declared in B as public. Post some code.
---visit #directxdev on afternet <- not just for directx, despite the name
alright heres some code:
This class represents class B.
This is one of class A''s functions, set to public as well.
Both class A & B do not reside in any inheritance hierarchy.
My errors are funny. it says ''object'' and others are unidentified.
This class represents class B.
class GameObject {protected: GameObject(D3DModel* m); public: //virtual destructor so that derived classes'' destructors //can be called using a GameObject pointer virtual ~GameObject() { } virtual void Update() =0; virtual int Render(); //for positioning stuffz void SetX(float xx) { x = xx; } void SetY(float yy) { y = yy; } void SetZ(float zz) { z = zz; } void SetXYZ(float xx, float yy, float zz) { x=xx; y=yy; z=zz; } float GetX() { return x; } float GetY() { return y; } float GetZ() { return z; } float GetXYZ(float& xx, float& yy, float& zz) { xx=x; yy=y; zz=z; } D3DModel* GetModel() { return model; } //set to public as trial----------- GameObject* object[4]; GameObject* object1[200]; //---------------------------------private: D3DModel* model; //to be used in constructor above float x, y, z; //positioning variables};
This is one of class A''s functions, set to public as well.
Both class A & B do not reside in any inheritance hierarchy.
void CrystalApp::ResetState() //to change{ //delete the objects 1st for (int i=0; i<4; i++) { delete crystal_a[i]; delete object[i]; ...}
My errors are funny. it says ''object'' and others are unidentified.
ResetState() is called in AppBegin(), in which, AppBegin()
is a private member function of class A.
setting AppBegin() in public oso doesnt seem to help.
[edited by - edwinnie on May 24, 2002 10:42:16 PM]
is a private member function of class A.
setting AppBegin() in public oso doesnt seem to help.
[edited by - edwinnie on May 24, 2002 10:42:16 PM]
delete object[j];
What are you accessing here? Think about it - you are trying to directly access class B's variables ... but where in memory is your class B? Is class A supposed to hold an instance of class B? Perhaps your code should look something like this:
Edit - changed i to j so tags don't get messed up ...
[edited by - Martee on May 24, 2002 11:01:36 PM]
What are you accessing here? Think about it - you are trying to directly access class B's variables ... but where in memory is your class B? Is class A supposed to hold an instance of class B? Perhaps your code should look something like this:
class A{ B b; void ResetState(void) { for(int i = 0; i < 4; ++i) { delete b.object[i]; } }}
Edit - changed i to j so tags don't get messed up ...
[edited by - Martee on May 24, 2002 11:01:36 PM]
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
so i changed the access specifiers of class B to PUBLIC,
then added the instance to class A.
well...it still din work...
but it says that the instance is using an undefined class A.
Didnt i jus change the constructor to public?
or it there something else i missed out?
then added the instance to class A.
well...it still din work...
but it says that the instance is using an undefined class A.
Didnt i jus change the constructor to public?
or it there something else i missed out?
Show us you code again. It is hard to see what you have changed without it.
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Listen to Martee; you must have an instance of the GameObject class to manipulate its members unless the member is declared as static.
This will work:
This will work:
class CrystalApp{public: void ResetState() { for(int i = 0; i < 4; ++i) { delete moGameObject.object;<br> }<br> }<br>private:<br> GameObject moGameObject;<br>}; </pre> This will also work:<pre>class GameObject<br>{<br>public:<br> static GameObject* object[4];<br>};<br><br>class CrystalApp<br>{<br>public:<br> void ResetState()<br> {<br> for(int i = 0; i < 4; ++i)<br> {<br> delete GameObject::object;<br> }<br> }<br>}; </pre> <br><br><SPAN CLASS=editedby>[edited by - dalleboy on May 25, 2002 6:14:24 AM]</SPAN>
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
many thanks aniwae!
erm..I think i better fallback abit, this time, those data
members are private, and not make them public.
I think i just developed a clearer picture this time:
I posted in xtreme''s bulletin board.
heres a copy and the most recent problem:
now the actual problem:
lets see...this time i am looking at a slightly different situation.
i have 4 files, crystal.cpp, gameobjects.cpp,
crystal.h,gameobjects.h.
The concept behind crystal.cpp, as i realize how
the template that gameinstitute.com gave was
that this file(renamed) is used as an application
where game init., game main, etc. are found.
One portion deals with allocating memory, thus
creating the objects. And immediately before the
memory was allocated, the objects are destroyed.
Thus, all memory allocation & destruction took
place here, jus in one function. Which makes it
very easy to manage.
But this will require us to "retrive" the addresses of those pointers in crystal.cpp,
because the "main game code" resides in gameobjects.cpp.
Time to ponder upon the code, in crystal.h,
Assuming this in crystal.cpp,
Somehow, I noticed that last line of code above
forces us to a default constructor, which
could be the problem.
Then in gameobjects.h:
The main problem resides here in gameobjects.cpp,
compiler complains that "cpa" is not a member of
Crystal_A.
I mean, i didnt want that to be a member.
So i guess, there must be something that i missed
out, and seems to be in the direction of the usage of default constructor.
Many thanks to catalyst again.
and to anyone who can help mi.
erm..I think i better fallback abit, this time, those data
members are private, and not make them public.
I think i just developed a clearer picture this time:
I posted in xtreme''s bulletin board.
heres a copy and the most recent problem:
quote:
okie, i got that pong clone sometime back from
gameinstitute.com''s c++ course.
i decided to remake it such that it has a neat
template which will be easy to use.
and i have been trying to fit in my tetris clone
which was previously in a pure procedural format.
in other ways, i am trying to convert my tetris clone to class form, and using gameinstitute.com
framework. (its quite hard to get the instructor to reply asap)
i got to the point where i began thinking of how
class objects, sometimes, arrays of pointers declared in classes, can be used to interact
with each other, jus like in the old procedural
way.
so, now then i realize this weird stuffz:
I have 2 classes A & B(non-inherited).
if class B declares an array of pointers in PUBLIC.
so, why cant class A''s member functions use that
particular array of pointers??
as off course, i am using that pong clone provided
to test and try to figure out whats wrong.
It would seem funny(in the pong clone) that
we do not declare the whatever arrays that the
class would use, in its own class. It was declared in the application class instead. and as PRIVATE data members as well.
So, their solution was to use some kind of accessor functions. So class A would have
"Set_B()" function, and vice-versa for class B. Somewhere in the pong application class,
such accessor functions would execute, so
class A would have addresses of pointers of class B. Thus, the objects interact. It was termed
as "introduction of objects to each other".
But, what if they are not single pointers, and they are arrays of pointers. I mean, so
we introduce an array of pointers to another
array of pointers? Its quite an insane thought.
Why not if class A & B declare their own arrays
of pointers, and as PUBLIC? Then, its faster
this way, though it looked global and defeats
the purpose of using classes(not entirely).
Also, they would contain the addresses of the array of pointers
that was declared and mem allocated in the application class.
now the actual problem:
lets see...this time i am looking at a slightly different situation.
i have 4 files, crystal.cpp, gameobjects.cpp,
crystal.h,gameobjects.h.
The concept behind crystal.cpp, as i realize how
the template that gameinstitute.com gave was
that this file(renamed) is used as an application
where game init., game main, etc. are found.
One portion deals with allocating memory, thus
creating the objects. And immediately before the
memory was allocated, the objects are destroyed.
Thus, all memory allocation & destruction took
place here, jus in one function. Which makes it
very easy to manage.
But this will require us to "retrive" the addresses of those pointers in crystal.cpp,
because the "main game code" resides in gameobjects.cpp.
Time to ponder upon the code, in crystal.h,
class CrystalApp : public D3DApplication{//other stuffzprivate: GameObject* object[4]; Crystal_A* crystal_a[4];}
Assuming this in crystal.cpp,
//now, i know have to declare instances of classesextern Crystal_A theCrystal_A;void CrystalApp::ResetState() { //delete the objects 1st for (int i=0; i<4; i++) { delete crystal_a[i]; delete object[i]; } //mem alloc for (int m=0; m(lessthan)4; m++) { crystal_a[m] = new Crystal_A( &crystalModel ); object[m] = crystal_a[m]; } //retrieve that array of pointers theCrystal_A.SetPointerArray(crystal_a);}
Somehow, I noticed that last line of code above
forces us to a default constructor, which
could be the problem.
Then in gameobjects.h:
class Crystal_A : public CrystalMaster{public: Crystal_A(D3DModel* model); //constructor //Crystal_A(); //do i need this?? void SetPointerArray(Crystal_A* cpa[4]);public: Crystal_A* crystalA[4];};
The main problem resides here in gameobjects.cpp,
compiler complains that "cpa" is not a member of
Crystal_A.
I mean, i didnt want that to be a member.
So i guess, there must be something that i missed
out, and seems to be in the direction of the usage of default constructor.
void Crystal_A::SetPointerArray(Crystal_A* cpa[4]){ assert(cpa); theCrystal_A.crystalA = theCrystal_A.cpa;}
Many thanks to catalyst again.
and to anyone who can help mi.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement