| | |
Stack Abstract Data Type Question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2005
Posts: 27
Reputation:
Solved Threads: 0
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?
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?
>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.
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.
New members chased away this month: 4
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.
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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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)
struct book // fully insulated, least efficient { book( const atring& t, const list<string>& a, const string& n ) ; book( const book& ) ; book& operator=( const book& ) ; ~book() ; string title() const ; list<string> authors() const ; string isbn() const ; // ... private: struct implementation ; implementation* opaque_pointer ; };
C++ Syntax (Toggle Plain Text)
struct book // encapsulated, partially insulated // more efficient { book( const atring& t, const list<string>& a, const string& n ) ; book( const book& ) ; book& operator=( const book& ) ; ~book() ; string title() const ; list<string> authors() const ; string isbn() const ; // ... private: string _title ; list<string> _authors ; string _isbn ; // ... };
C++ Syntax (Toggle Plain Text)
struct book // encapsulated, not insulated // even more efficient (inlining) { book( const atring& t, const list<string>& a, const string& n ) ; : _title(t), _authors(a), _isbn(n) { /* ... */ } string title() const { return _title ; } list<string> authors() const { return _authors ; } string isbn() const { return _isbn ; } // ... private: string _title ; list<string> _authors ; string _isbn ; // ... };
C++ Syntax (Toggle Plain Text)
struct book // less encapsulated, not insulated // most efficient (no copy of objects) { book( const atring& t, const list<string>& a, const string& n ) : _title(t), _authors(a), _isbn(n) { /* ... */ } const string& title() const { return _title ; } const list<string>& authors() const { return _authors ; } const string& isbn() const { return _isbn ; } // ... private: string _title ; list<string> _authors ; string _isbn ; // ... };
Last edited by vijayan121; Sep 25th, 2007 at 11:03 pm.
![]() |
Similar Threads
- C++: problam with data type(int,float,double) (C++)
- Working with SQL server's Image data type (ASP.NET)
- help understanding "char" data type (C++)
- Problems with changing values returned by RegQueryValueEx (C)
- help on data type conversion (Visual Basic 4 / 5 / 6)
- Help With Data-type Declaration Symbols (Pascal and Delphi)
- Working with SQL's Text data type (ASP.NET)
- Text data type troubles in T-SQL (Database Design)
Other Threads in the C++ Forum
- Previous Thread: what wrong with this code block?
- Next Thread: Some math and MFC
Views: 1572 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






