943,972 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2089
  • C++ RSS
Sep 25th, 2007
0

Stack Abstract Data Type Question

Expand Post »
Sorry~~, I don't know where to post this quetion.

Can someboy help me with this question, plz....

Suppose someone designed a stack abstract data type in which the top function returned an access path (or pointer), rather than returning a copy of the top element. This is not true data abstraction. Why?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
jack223 is offline Offline
27 posts
since Nov 2005
Sep 25th, 2007
0

Re: Stack Abstract Data Type Question

>This is not true data abstraction.
Sure it is. The abstraction is the stack and its implementation, not the contents of the stack. Returning a copy of the top of the stack instead of a pointer to it makes no difference in how the stack works. Presumably the question is assuming that a malicious coder can break umpteen language rules to get a hold of other items if given a pointer to one item, but that's equally likely with nothing more than a stack object regardless of how much abstraction you put into it.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 25th, 2007
0

Re: Stack Abstract Data Type Question

It's certainly poor encapsulation.

Giving someone a pointer to the innards of your class exposes you to all sorts of problems. What if they try to write to your class data via the pointer? What if they increment the pointer, where does it point to then?
Applying all sorts of const to the result can only go so far.

Yes, std::string has the c_str() method which returns a pointer to something on the inside, but that's more a "necessary evil" rather than an example of good design IMO.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 25th, 2007
0

Re: Stack Abstract Data Type Question

very often, encapsulation/insulation and efficiency pull in opposite directions. and c++ programmers have to do a fine balancing act between the two and decide what is most appropriate in a particular case. for example,
C++ Syntax (Toggle Plain Text)
  1. struct book // fully insulated, least efficient
  2. {
  3. book( const atring& t, const list<string>& a, const string& n ) ;
  4. book( const book& ) ;
  5. book& operator=( const book& ) ;
  6. ~book() ;
  7. string title() const ;
  8. list<string> authors() const ;
  9. string isbn() const ;
  10. // ...
  11. private:
  12. struct implementation ;
  13. implementation* opaque_pointer ;
  14. };
C++ Syntax (Toggle Plain Text)
  1. struct book // encapsulated, partially insulated
  2. // more efficient
  3. {
  4. book( const atring& t, const list<string>& a, const string& n ) ;
  5. book( const book& ) ;
  6. book& operator=( const book& ) ;
  7. ~book() ;
  8. string title() const ;
  9. list<string> authors() const ;
  10. string isbn() const ;
  11. // ...
  12. private:
  13. string _title ;
  14. list<string> _authors ;
  15. string _isbn ;
  16. // ...
  17. };
C++ Syntax (Toggle Plain Text)
  1. struct book // encapsulated, not insulated
  2. // even more efficient (inlining)
  3. {
  4. book( const atring& t, const list<string>& a, const string& n ) ;
  5. : _title(t), _authors(a), _isbn(n) { /* ... */ }
  6. string title() const { return _title ; }
  7. list<string> authors() const { return _authors ; }
  8. string isbn() const { return _isbn ; }
  9. // ...
  10. private:
  11. string _title ;
  12. list<string> _authors ;
  13. string _isbn ;
  14. // ...
  15. };
C++ Syntax (Toggle Plain Text)
  1. struct book // less encapsulated, not insulated
  2. // most efficient (no copy of objects)
  3. {
  4. book( const atring& t, const list<string>& a, const string& n )
  5. : _title(t), _authors(a), _isbn(n) { /* ... */ }
  6. const string& title() const { return _title ; }
  7. const list<string>& authors() const { return _authors ; }
  8. const string& isbn() const { return _isbn ; }
  9. // ...
  10. private:
  11. string _title ;
  12. list<string> _authors ;
  13. string _isbn ;
  14. // ...
  15. };
;
Last edited by vijayan121; Sep 25th, 2007 at 11:03 pm.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: what wrong with this code block?
Next Thread in C++ Forum Timeline: Some math and MFC





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC