Jump to content
  • Advertisement

Archived

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

Prozak

Classes: Variables& Functions

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

Hi all, when, in a class we access a variable, cant we make it so that it triggers a call to a function? lets say we have the class "car" with variables "doorstatus" if i did a class.doorstatus="closed"; i would like this call to trigger the calling of another function, that would then actually "close" the door. if anyone understands what i''m talking about here, then please help me out on this one, thanx, Hugo Ferreira UniteK Future "Concentrate, and you can see it. If you see it, then it is possible. If it is possible, you can Achieve it."

Share this post


Link to post
Share on other sites
Advertisement
yes you can, but i think its more intuitive to say something like car.door.close() or car.setDoorStatus ( CLOSED ). To answer your question though, you need to make doorStatus its own class and then overload operator = to get the desired effect of car.doorStatus = "closed".

Share this post


Link to post
Share on other sites
Generally, you should never allow member variables to be manipulated directly. There are entire books on why this "information hiding" is a good thing. Anyway, the answer to your question happens to be the same for implementing "information hiding." Just make a function that does what you want called, say, close_door. So, you''d call object.close_door(), and that function would set the state variable and whatever else needs to be done. Better yet, you might decide to make a class for each component (door, window, etc.), and have the close, then you could do something like object.get_door()->close(), where get_door is a function returning a pointer to the object''s door object (use a pointer rather than a reverence to indicate the object can be modified).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
no you can''t do it. It is a good idea though, you should be proud to have thought of it. Some other languages have it but C++ doesn''t so we''ll just have to do without. Maybe someday they''ll add it to the language, until then use functions.

Share this post


Link to post
Share on other sites
quote:

no you can''t do it. It is a good idea though



I don''t think you are familiar with operator overloading. You absolutely CAN do this. Its just that its generally considered that you SHOULDN''T do it, because its bad encapsulation and poor style. But if you insist, see this example:

  
#include <iostream>
#include <string.h>

class Cdoor {
public:
int status;
Cdoor& operator = ( const char * operation);
Cdoor ();
};
Cdoor::Cdoor ()
{
status = 0;
}
Cdoor& Cdoor::operator = ( const char * operation)
{
std::string value = operation;

if (strcmp(operation, "closed") == 0)
status = 0;
else if (strcmp(operation, "open") == 0)
status = 1;
else
status = -1;
return *this;
}

class CCar
{
public:
Cdoor door;
};

void main ()
{
CCar mycar;
std::cout << mycar.door.status;
mycar.door = "open";
std::cout << mycar.door.status;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
that is not at all what we are talking about. We do not want it on a per class basis (which is what overloading the assignment operator does for you) we want it on a per member basis. It would work something like this:

  
class A
{
int value :
get(){return value;},
set(int i){
cout<<"value changed from "<<value<<" to "<<i;
value=i;};

};

A a;
int i;
a.value = 4; //this prints out the message

i=a.value; /*this just assigns 4 to i, but we could
abuse the feature to do strange things such as return a
different value, but as with all operator overloading
we should stick to what makes sense.


the advantage is that you get rid of the function syntax for things that just don''t feel like functions. Using a lot of SetX and GetX() functions makes your code harder to read but sometimes (or always, according to OOP purists) you need them to keep your object states valid. Basically it is just another kind of operator overloading, but you are overloading the . that lets you access members. You could even have virtual attributes just like you have virtual functions.

Share this post


Link to post
Share on other sites
Delphi, Borland C++ Builder and C# has syntax to allow for things like this. They''re called properties. Sample C# syntax:

  

//this is our variable

private answer answer;

//this is the property declaration:

public int Answer
{
get
{
return answer;
}
set
{
//of course we can do lots of other fun stuff here

answer = value;
}
}





Psalm 137:9: "Happy shall he be, that taketh and dasheth thy little ones against the stones."

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
that is not at all what we are talking about. We do not want it on a per class basis (which is what overloading the assignment operator does for you) we want it on a per member basis.

Yes, and that poster was saying that you make your members into classes too, and overload their assignment operators so that although they act like ints, strings, whatever, they can actually call functions as a side-effect.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hmm, yes I see that you can do it that way but it doesn''t look as nice. You could also have problems because C++ can only infer one level of implicit construction or one conversion operation, though you probably wouldn''t need it that often. However it does look a bit awkward and so I doubt many people use it, instead they use the Gets and Sets. You don''t strictly need operator overloading either, it just makes life a little nicer. The way I look at it, if there are a whole bunch of functions starting with the same verb there should be an easier way of writing those functions. So instead of adding Matrices, Angles, or Strings I just use +.

Share this post


Link to post
Share on other sites
IMO, it is a failing in C++ that it isn't possible to do this. Perhaps a later version of C++ will address this issue, since COM and CORBA use these properties, and new languages developed for interaction with these services also provide properties, I think C++ will need to support them in order to remain viable for general use.

It is my view that data hiding is only necessary because you can't control what values are assigned to your variables, and what to do when a value is so assigned. Another problem with bare variables is that if you decide to change its type, old code may not work with it. Whilst with SetXyz methods you merely need to add a new SetXyz for the new type, and leave the old SetXyz method there with suitably modified logic.

Exactly what to do about GetXyz's return type I am unsure. You might add a new version of GetXyz (Microsoft might call it GetXyzEx), add a type parameter to GetXyz, or ensure that a implicit conversion from GetXyz's return type to the old and new types is available.

Abolish Software Patents! | freepatents | lpf

Edit:

You can use classes to overload assignment, but I don't believe there is a way to overload retreiving the value of a member (although you could require that the user dereference the member to get its value and overload that.)

Edited by - Mayrel on November 23, 2001 12:50:57 PM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!