Re: How to make code accepting differet types work?
"Mark P" <usenet@fall2005REMOVE.fastmailCAPS.fm> wrote in message
news:%i3tg.118914$H71.105949@newssvr13.news.prodigy.com...
Jim Langston wrote:
This is something I've been thinking about creating, and am trying to get
the pieces together.
I want to be able to assign values in a method accepting different types.
I.E.
MyInstance.MyMethod("IntField") = 1;
MyInstance.MyMethod("FloatField") = 2.34f;
MyInstance.MyMethod("StringField") = std::string("Hello");
Is this possible?
I know I could do it using function overloading by passing the parms,
I.E.
MyInstance.MyMethod("IntField", 1);
MyInstance.MyMethod("FloatField", 2.34f);
MyInstance.MyMethod("StringField", std::string("Hello");
I'm thinking to use the assignment I would need to return a LHV, a
reference to what was being assigned. I think I just answered my own
question. Whatever I am returning would need to have operator=()
overloaded for each type.
Is this the way to go?
Why do the functions need to have the same name? A far more conventional
approach would be, e.g.,
MyInstance.IntField() = 1;
MyInstance.FloatField() = 2.34;
etc.
with corresponding fcn declarations:
int& IntField();
float& FloatField();
etc.
Style-conscious folk would probably go with the even more conventional
approach of setter functions:
void setIntField(int i);
void setFloatField(float f);
Yes, normally it would, but I plan on using this to assign values to a SQL
table where the string value will be a key into the field name. Keeping the
method names the same would simplify the interface for the end user (who
will probably remain me). So I could do something like:
DynSQL PlayerTable( ServerConnInfo, "Player" );
PlayerTable.reset();
PlayerTable.SetField("Name") = Player.Name;
PlayerTable.SetField("Age") = Player.Age;
PlayerTable.SetField("Sex") = Player.Sex;
PlayerTable.SetField("Strength") = Player.Str;
std::string Result = PlayerTable.insert();
So that the user doesn't have to care what types the variables are, only
have to know their names. This makes it easier for some tables such as my
item table that has 47+ fields.