•
•
•
•
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
![]() |
•
•
Join Date: Nov 2005
Posts: 27
Reputation:
Rep Power: 4
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.
I'm here to prove you wrong.
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
Location: india
Posts: 1,085
Reputation:
Rep Power: 9
Solved Threads: 163
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,
;
cplusplus 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 ; };
cplusplus 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 ; // ... };
cplusplus 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 ; // ... };
cplusplus 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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- 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



Linear Mode