User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,508 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,677 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 857 | Replies: 3
Reply
Join Date: Nov 2005
Posts: 27
Reputation: jack223 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
jack223 jack223 is offline Offline
Light Poster

Stack Abstract Data Type Question

  #1  
Sep 25th, 2007
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 489
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Stack Abstract Data Type Question

  #2  
Sep 25th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: Stack Abstract Data Type Question

  #3  
Sep 25th, 2007
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.
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,085
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Stack Abstract Data Type Question

  #4  
Sep 25th, 2007
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,
  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. };
  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. };
  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. };
  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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:45 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC