Skip to main content
GameDev.net gamedev.net
🔒 Locked

Classes: Variables& Functions

Started by Prozak Nov 21, 2001 at 6:50 PM 11 replies 450+ views
Original Post
Prozak
Prozak
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."
invective
invective
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".
merlin9x9
merlin9x9
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).
invective
invective
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;
}
Arild Fines
Arild Fines
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."
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Kylotan
Kylotan
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.
Mayrel
Mayrel
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
krez
krez
ok, so i''ve just started relearning c++, but i think you people are reading into this too much.
can''t you just make an accessor function, and put the other stuff in there?
  
class Feh
{
private:
bool p_doorclosed;
public:
bool getDoorClosed(void) {return p_doorclosed;};
void setDoorClosed(bool IsClosed)
{
p_doorclosed = IsClosed;
if (p_doorclosed) DoOtherStuff();
};
};

that''d do it, no?
without making new classes or overloading operators?

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.