Using info between classes
Hiya,
It''s been quite a while since I was really into programming (DOS, ANSI C) and things have drastically changed. I feel like I''m starting all over again, and I have a few (hundred) questions that hopefully somebody can help me with.
I have a class that precomputes tables (cos,sin tables) called "CPrecomp", another that does particles called "CParticles". What I''m trying to do is have the the CPrecomp array "CosSinTable[]" (a public dec, btw) be available to use in the CParticles Class.
I can copy the CPrecomp table to a clone defined in CParticles from the main(), but I''m hoping there''s a smarter way. I''ve looked through books and sites, but I can''t find anything on this.
Any ideas?
Also, are there any really good books that are targetted for the beginner/intermediate that really focus on these types of issues?
Thanks for any help you can give!
-Krylar
When you say public declaration, you mean inside the class, not global, right? I must be misunderstanding something terrible here... It sounds to me like you could have your particle class have a pointer to the table. Or I read your post wrong.
--------------------
You are not a real programmer until you end all your sentences with semicolons;
--------------------
You are not a real programmer until you end all your sentences with semicolons;
Visit the ROAD Programming Website for more programming help.
--------------------
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
Hiya,
Yep, I mean that it''s listed under "public:" in the Class definition. Sorry about that!
Also, I fully admit that I get lost pretty quickly in pointer hell. I can handle most things, but I''ve never found a really good pointer tutorial (or I''m slow...but that thought hurts my ego too much, so I choose to blame the books I''ve read ) Any ideas on good references for pointers?
I guess what I''m asking is: If you had to access data elements from one class to another, how would you do it? Do you have to set up an instance to each class that your going to use from each class...actually, that makes sense, huh? doh.
So, in the Particles Class I could do something like:
// start
#include "cprecomp.h"
CPrecomp PreComp;
...
ValueCos = PreComp.CosTable[4];
ValueSin = PreComp.SinTable[4];
// end
If that''s the case, I''m going to put my head in a bag cause I feel really dumb. If that''s not the case, than I really feel dumb.
So...is that the case?
Thanks!
-Krylar
"I may be slow, but my work is poor!"
Yep, I mean that it''s listed under "public:" in the Class definition. Sorry about that!
Also, I fully admit that I get lost pretty quickly in pointer hell. I can handle most things, but I''ve never found a really good pointer tutorial (or I''m slow...but that thought hurts my ego too much, so I choose to blame the books I''ve read ) Any ideas on good references for pointers?
I guess what I''m asking is: If you had to access data elements from one class to another, how would you do it? Do you have to set up an instance to each class that your going to use from each class...actually, that makes sense, huh? doh.
So, in the Particles Class I could do something like:
// start
#include "cprecomp.h"
CPrecomp PreComp;
...
ValueCos = PreComp.CosTable[4];
ValueSin = PreComp.SinTable[4];
// end
If that''s the case, I''m going to put my head in a bag cause I feel really dumb. If that''s not the case, than I really feel dumb.
So...is that the case?
Thanks!
-Krylar
"I may be slow, but my work is poor!"
Are there single instances of your CPreComp and CParticle classes? If so, yes you can refer to the elemenets of the CPreComp just by doing the PreComp.ValueCos[4], etc. thing.
SiCrane,
If I want to use additional instances of these, I would just have to name them differently, right?
So, for example, say I have CParticles, CSprites, CTiles classes. Can''t I make individual instances of CPrecomp in each one of those? I think they''d all have to have different names, but I don''t have a compiler nearby to check it out.
Is it sloppy to do it that way? If so, do you know of a better way to do it, by chance? I''m hoping to move by the bad habits as quickly as possible
Thanks again,
-Krylar
If I want to use additional instances of these, I would just have to name them differently, right?
So, for example, say I have CParticles, CSprites, CTiles classes. Can''t I make individual instances of CPrecomp in each one of those? I think they''d all have to have different names, but I don''t have a compiler nearby to check it out.
Is it sloppy to do it that way? If so, do you know of a better way to do it, by chance? I''m hoping to move by the bad habits as quickly as possible
Thanks again,
-Krylar
Some Pseudo Source that might help
I''m dumb so it could be wrong...
40
www.databyss.com
www.omlettesoft.com
"Don''t meddle in the affairs of wizards, for they are subtle and quick to anger."
class PreComp { public: int sinTable[360]; int cosTable[360]; PreComp();};PreComp<img src="tongue.gif" width=15 height=15 align=middle>reComp() { // fill sin table // fill cos table};class CParticle { private: PreComp * pPreComp; public: CParticle(PreComp *pTemp); double getSin(int x);};CParticle::CParticle(PreComp *pTemp) { pPreComp = pTemp;};double CParticle::getSin(int x) { return(*pPreComp->sinTable[x]);};
I''m dumb so it could be wrong...
40
www.databyss.com
www.omlettesoft.com
"Don''t meddle in the affairs of wizards, for they are subtle and quick to anger."
-40
Hiya,
Thanks, 40. I''ll give that a whirl too.
I really appreciate everyone''s help on this...you all rock!
-Krylar
Thanks, 40. I''ll give that a whirl too.
I really appreciate everyone''s help on this...you all rock!
-Krylar
quote: Original post by Krylar
Any ideas on good references for pointers?
What, like:
MyClass*& refToPtr[1000];
Or maybe some good pointers to references would be better?
MyClass&* ptrToRef[1000];
Okay, here's something crazy for you to chew on:
There are certainly reasons to avoid globals, and this can be done in others ways (static members, etc...). But this should give you the idea. Do the same for Cos, and write it better than I did!
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
Edited by - mossmoss on June 9, 2000 8:00:14 PM
class CSine {private: double table[360];public: CSine() { for (int x = 0; x < 360; ++x) { table[x] = sin(DEG_TO_RAD(x)); } } double operator()(int deg) { return table[deg]; }}; //globals areaCSine Sin; //elsewhere...double sin_of_30_deg = Sin(30);
There are certainly reasons to avoid globals, and this can be done in others ways (static members, etc...). But this should give you the idea. Do the same for Cos, and write it better than I did!
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
Edited by - mossmoss on June 9, 2000 8:00:14 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement