🔒 Locked
Class Question
Advertisement
Original Post
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
Now all that I said above is just ''normal'' practice really you could do it how ever you''d like.
Pat
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.
Unless you''re more interested in speed than proper design and encapsulation.
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.
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;
}
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.
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.
Advertisement