Archived

This topic is now archived and is closed to further replies.

Dwiel

Objects with only pointers to data

Recommended Posts

I would like some input on this idea of mine... I have a class thats lets say a label... this class, CLsabel, handels the initilization, drawing, and destorying of its self, on its own. Now lets say it has these data members: std::string text; int top; int left; D3DCOLOR color; now the problem I am having is I really don't like the SetText(), GetText, SetTop... ect. method. So I came with this method:
      

class CLabel
{
    std::string *text;
    int *top;
    int *left;
    D3DCOLOR *color;
    char owner;

    ~CLabel()
}

CLabel::~CLabel()
{
    if(owner && 1)
        delete text;
    if(owner && 2)
        delete top;
    ect.
}

    
in this way, you can just set the pointer to be whatever variable you want... I would rather not have to acces the members by any outside code any time but durring constructor and destructor. What r your opinions on this method? thanx for all of the input! tazzel3d ~ dwiel [edited by - tazzel3d on August 15, 2002 11:19:42 AM]

Share this post


Link to post
Share on other sites
I''m confused about what you are trying to do???

Why not just declare the data public: instead of private:
Class CLabel
{
public:
...
...
}

Share this post


Link to post
Share on other sites
I, too, am confused by what you expect to accomplish here. Are you just being careful not to delete unallocated memory? What advantage do pointers give you?



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
ok..sorry im not very good at explaining... My game is set up where different 'screens'(all children of the Screen class) can be set to the pointer curScrenn, which is rendered... The way I implement most objects desplayed on the screen such as labels, buttons, sprites, ect. are created dynamically... heres some code:


    
// Create Interface

circletex = tm.CreateTexture(this, "Media\\bmp\\GalaxyScreen\\Cursor\\cursor.bmp", D3DCOLOR_ARGB(255, 255, 255, 255), &circlenfo);
D3DXIMAGE_INFO spritenfo;
IDirect3DTexture8 *tex = tm.CreateTexture(this, "Media\\bmp\\GalaxyScreen\\1024\\title.bmp", D3DCOLOR_ARGB(255, 255, 255, 255), &spritenfo);
im.AddObject(this, new ImageLabel((vp.Width - spritenfo.Width) / 2, 0, 0, 0, 10, tex, 0), otImageLabel);
im.AddObject(this, new ImageLabel(0, 0, 0, 0, 10, circletex, &circlepos), otImageLabel);
im.AddObject(this, new DynamicTextLabel(&closetext, 10, 10, 0, 0, 10, "impact", 18, D3DCOLOR_ARGB(255, 0, 0, 255)), otDynamicTextLabel);
im.AddObject(this, new DynamicTextLabel(&galaxy.SolarSystems[selectedsys].name, 10, vp.Height - 59, 0, 0, 10, "impact", 10, D3DCOLOR_ARGB(255, 0, 0, 255)), otDynamicTextLabel);
im.AddObject(this, new DynamicTextLabel(&selectedsystext, 10, vp.Height - 42, 0, 0, 10, "impact", 10, D3DCOLOR_ARGB(255, 0, 0, 255)), otDynamicTextLabel);
im.AddObject(this, new DynamicTextLabel(&numofplanets, 10, vp.Height - 25, 0, 0, 10, "impact", 10, D3DCOLOR_ARGB(255, 0, 0, 255)), otDynamicTextLabel);

im.InitScene(this);


im, btw, is a global InterfaceManager, which fairly obviously, initilizes, draw deinitilizes all of the objects making the inteface and such. Now the way this works, I have created static objects. The only time I set thier values is when they are created. I can't access the objects anytime else, because they are held by the interfaceManager.

current system:

so now i want to have a label desplay some info thats not static. So i create a dynamiclabel and give it a pointer to the string i want it to desplay. Now, this way, I have a string that I can change as much as i want, when ever i want, without ever accessing any members of the label class

proposed system:

instead of having different classes for dynamic objects and static objects, include best of both... Have only pointers to objects... when the object is created, give it pointers to all of the data it needs. problem with this is, it makes creating static objects a pain... Lets say all we want is a background texture displayed every frame... no movement nothing.. what we can do is, tell the object all of the data it needs to know, position texture, ect. And also tell it that it should create its own objects to hold the data....

sorry if this is very confusing... I am having a very hard time explaining....

if i can have some specific questions that would be nice.. I know if you completely dont understand its hard to ask specific questinos though so yeah...
thanx for the help despite its difficulty to undersatnd

tazzel3d ~ dwiel


[edited by - tazzel3d on August 15, 2002 12:35:09 PM]

Share this post


Link to post
Share on other sites
If the information is not static, but instead is changing constantly, ask yourself whether you need a class at all, rather than a function that draws a label ONCE.

I would suggest that you NOT use pointers like this. Issues of ownership and lifetime management will come into play, and a myriad of teeny, subtle bugs will pop up.

You have two options, as I see it:
1. Bite the bullet and use getters/setters or public members.

2. Implement a listener pattern, under which the label registers itself to the component whose info it displays, so it can automatically update when that info changes.



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
I guess a good question might be how much time it takes to call a set/get function...

wait.....

new idea, but similar
ok...
here we go..

I think this would b ebetter explained by a commented example:


  

class CLabel
{
int top;
int left;
std::string text;

CLabel(int ntop, int nleft, std::string ntext, int** lptop = 0, int** lpleft = 0, std::string** lptext = 0);
}

//Init CLabel class

// ntop - [in] value for top member

// nleft - [in] value for left member

// ntext - [in] value for text member

// lptop - [in/out] address of a pointer to top

// lpleft - [in/out] address of a pointer to left

// lptext - [in/out] address of a pointer to text


CLabel::CLabel(int ntop, int nleft, std::string ntext, int** lptop = 0, int** lpleft = 0, std::string** lptext = 0)
{
top = ntop;
left = nleft;
text = ntext;
lptop = ⊤
lpleft = &left;
lptext = &text;
}

SomeFunction()
{
std::string *text = 0;
CLabel *label1 = new CLabel(10, 20, "some text", 0, 0, &text);
(*text) = "some different text";
}


does this make sence? Instead of having all of the dat abieng pointers, have the constructor return pointers to all of the data objects so I can acces them directly... with out having acces to the object its self...

Ideas?

thanx for the help
Agian

tazzel3d ~ dwiel

Share this post


Link to post
Share on other sites
No.

Firstly, playing with pointers like that is gonna make your code ugly and hard to maintain.

Secondly, accessor functions cost almost nothing to call, and can usually be inlined later if they''re really a problem (99.99% of the time, they are not).

Thirdly, it destroys all encapsulation. A string in a class should belong to that class. You shouldn''t be changing it through some pointer somewhere, as that doesn''t let the class know that it''s changed, it allows more than one place to delete the string, and so on.

It''s horrible. Sorry.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
lol ok... thanx for the critisism... lol

quote:

It''s horrible. Sorry.



your fine... its what i wanted... well i wanted it to be good, but I also wanted to know if it was bad so i didnt bother implementing it...

the only problem is.....

as of now, I can''t access the classes once thier created... The idea was to never have an interface hardcoded, if ...
I guess this just makes things very difficult..

huh

one question now...

how is an interface normally created? What are some common systems?

Thanx for the help guys!

tazzel3d ~ dwiel

Share this post


Link to post
Share on other sites
What do you mean by ''an interface''? If you mean the list of functions and so on, then that just follows on from the list of things you need the class to do. Will other classes need to be able to read the text? If so, implement a GetText() accessor. And so on.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites