Skip to main content
gamedev.net
🔒 Locked

Class Question

Started by lanchfn21 Feb 20, 2003 at 2:14 PM 8 replies 250+ views
Original Post
lanchfn21
lanchfn21
If i am using a class where the user has to enter a specific thing, would the cin statement be in the Accessor function(get functions) or in the set functions? if anyone knows what im asking and has an answer post away. thanks
lanchfn21
lanchfn21
in C++ microsoft visual
(i dontk know if that is needed)
patindahat
patindahat
Ok, normally any get methods (class functions) return a copy of the value stored in the object, or a pointer to something inside the object. Set methods usually alter a member of the object, so a good practice is to make all get functions const. As for receiving a ''specific thing''. Do you mean a string? and int? a short? a float? a long? an object?

Now all that I said above is just ''normal'' practice really you could do it how ever you''d like.

Pat
cgoat
cgoat
In general, my opinion is neither. The cin should be outside of the class, and then the set method is used to set the value to what was read in.


  
class LongClass
{
public:
long GetValue() const
{
return value;
}

void SetValue(long _value)
{
value = _value;
}

private:
long value;
};

int main()
{
LongClass lc;
long inputvalue;

cin >> inputvalue;
lc.SetValue(inputvalue);

return;
}


Unless you''re more interested in speed than proper design and encapsulation.
SabreMan
SabreMan
quote:
Original post by cgoat
Unless you''re more interested in speed than proper design and encapsulation.

What does speed have to do with it?
cgoat
cgoat
Or you could make your class use the >> directly (a bit more advanced, and maybe not done properly in my following example).


  
class LongClass
{
public:
friend istream& operator>>(istream& lhs, LongClass& rhs);

long GetValue1() const
{
return value1;
}

long GetValue2() const
{
return value2;
}

protected:
long value1, value2;
};

istream& operator>>(istream& lhs, LongClass& rhs)
{
lhs >> rhs.value1;
lhs >> rhs.value2;
return lhs;
}

int main()
{
LongClass lc;
long testvalue;

cin >> lc;

testvalue = lc.GetValue1();
cout << "Test Value 1 Is " << testvalue << endl;
}
cgoat
cgoat
quote:
Original post by SabreMan
What does speed have to do with it?



I was just assuming that there's some extra function overhead or something. I guess not.


[edited by - cgoat on February 20, 2003 6:44:03 PM]
daerid
daerid
Proper design should always give you the best possible performance.

They go hand in hand.
daerid@gmail.com
andy_fish
andy_fish
quote:
Original post by daerid
Proper design should always give you the best possible performance.

They go hand in hand.


I dunno about that statement. There''s a difference between the most logical designs(ie the most intuitive to humans), and the best performance designs.

Of course one should always start with the former.
cgoat
cgoat
quote:
Original post by andy_fish
There''s a difference between the most logical designs(ie the most intuitive to humans), and the best performance designs.


That was basically what I meant about speed. You might take shortcuts that break down the logical design and put things where they don''t logically belong to increase performance.

Topic Locked

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

Sign in to reply to this topic.