c++ - Choosing to make a function a member, non-member, private, public, etc -
i've searched around descriptions of difference between member , non-member functions and, while i'm still quite confused, thought i'd give example clear things me bit. here's question old test our instructor gave study material:
assume have main() program uses queue2.h , node2.h template implementations our text, creating queue of letters (queue letters).
- we want write stream operator insert characters of string (thing) queue (letters << thing;).
(b) should make << member or non-member? private, public, friend, or neither?
- we want provide tool (call get_front) returns head pointer of queue of characters future manipulations using linked list tool kit.
so, list_head_insert(head_ptr, '2') place ‘2’ @ front of queue when coded properly.
(b) should make get_front member or non-member? private, public, friend, or neither?
i'm guessing first 1 should implemented non-member friend function, i'm not sure on specifics why?
thanks lot!
(b) should make << member or non-member? private, public, friend, or neither?
member, public. create queue object , overload insertion operator member function. can used insert characters queue.
reason: wanted insert in queue stream , not in external stream such cout. better practice use member functions whenever can. friends should used when members can't used.
get_front() becomes public member function.
Comments
Post a Comment