Click here to Skip to main content
15,899,475 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Making DlgBox without its class..... Pin
Vipin Aravind25-Jan-06 18:52
Vipin Aravind25-Jan-06 18:52 
QuestionCSpiltterWnd Pin
LeeeNN25-Jan-06 14:30
LeeeNN25-Jan-06 14:30 
QuestionReturning data from VC++(DLL) to VB Client Pin
techratna25-Jan-06 13:52
techratna25-Jan-06 13:52 
AnswerRe: Returning data from VC++(DLL) to VB Client Pin
Stephen Hewitt25-Jan-06 20:51
Stephen Hewitt25-Jan-06 20:51 
AnswerRe: Returning data from VC++(DLL) to VB Client Pin
Phil J Pearson25-Jan-06 23:29
Phil J Pearson25-Jan-06 23:29 
Questionchild frame fills main frame? Pin
LeeeNN25-Jan-06 13:38
LeeeNN25-Jan-06 13:38 
AnswerRe: child frame fills main frame? Pin
Owner drawn26-Jan-06 17:04
Owner drawn26-Jan-06 17:04 
QuestionMember Get/Set methods? Pin
Rob Caldecott25-Jan-06 11:55
Rob Caldecott25-Jan-06 11:55 
Many people recommend using member Get/Set functions in order to access class data, with the data members themselves being private/protected. I am pretty much sold on the idea, and though it means it takes longer to knock together even a basic class, the benefits are worth the effort IMHO.

However, I also make use of STL containers in my classes, but I have yet to find a satisfying way in which I should allow access to a private container. I have seen many classes that simply return a reference to the container, e.g.:

class CFoo
{
private:
  std::list<std::wstring> m_strings;
public:
  inline std::list<std::wstring>& GetStrings(void) { return m_strings; }
};


I'm sure you'll agree that this is pretty useless - you may as well make the container public and be done with it (OK, you can change the name of the container without having to change code that uses it, but this is still fairly insignificant).

So, how do others here offer access to such containers (doesn't have to be an STL container - could be an MFC/ATL CList/CArray/etc.)?

Take an example - I have a class that enumerates installed printers and stores the name of each printer in a std::vector. Now, if I wanted to fill a listbox with these printer names, I *could* offer a class method to do this for me, and thus hide the internal container implementation from the caller. However, in reality, I might also want the list of printers in a listbox, or a list control, or as text in an edit, etc. etc. so allowing calling code access to the vector is essential. How would you go about this while adhering to the Get/Set methodology?

One thought - for STL containers - would be to return const iterators, e.g.:

class CFoo
{
private:
  std::list<std::wstring> m_strings;
public:
  inline std::list<std::wstring>::const_iterator begin() const { return m_strings.begin(); }
  inline std::list<std::wstring>::const_iterator end() const { return m_strings.end(); }
};
...
CFoo foo;
for (std::list<std::wstring>::const_iterator it = foo.begin(); it != foo.end(); ++it)
{
// Do something with the iterator
}


Thoughts?


The Rob Blog
Google Talk: robert.caldecott
AnswerRe: Member Get/Set methods? Pin
Maximilien25-Jan-06 16:45
Maximilien25-Jan-06 16:45 
AnswerRe: Member Get/Set methods? Pin
Bob Stanneveld25-Jan-06 21:07
Bob Stanneveld25-Jan-06 21:07 
Questionhow to define the customer paper type Pin
yujia12025-Jan-06 8:15
yujia12025-Jan-06 8:15 
AnswerRe: how to define the customer paper type Pin
Phil J Pearson25-Jan-06 23:32
Phil J Pearson25-Jan-06 23:32 
QuestionRe: how to define the customer paper type Pin
yujia12026-Jan-06 5:38
yujia12026-Jan-06 5:38 
QuestionHow to obtain the current directory pidl of a windows explorer window? Pin
z530025-Jan-06 6:21
z530025-Jan-06 6:21 
QuestionRe: How to obtain the current directory pidl of a windows explorer window? Pin
David Crow25-Jan-06 6:27
David Crow25-Jan-06 6:27 
AnswerRe: How to obtain the current directory pidl of a windows explorer window? Pin
z530025-Jan-06 14:20
z530025-Jan-06 14:20 
QuestionSerial number of USB Flash drive Pin
mrothermund25-Jan-06 6:16
mrothermund25-Jan-06 6:16 
AnswerRe: Serial number of USB Flash drive Pin
Phil J Pearson25-Jan-06 6:40
Phil J Pearson25-Jan-06 6:40 
AnswerRe: Serial number of USB Flash drive Pin
Taka Muraoka25-Jan-06 6:56
Taka Muraoka25-Jan-06 6:56 
GeneralRe: Serial number of USB Flash drive Pin
mrothermund25-Jan-06 7:46
mrothermund25-Jan-06 7:46 
AnswerRe: Serial number of USB Flash drive Pin
kakan25-Jan-06 20:13
professionalkakan25-Jan-06 20:13 
QuestionException (Access violation reading location) Pin
llp00na25-Jan-06 5:50
llp00na25-Jan-06 5:50 
AnswerRe: Exception (Access violation reading location) Pin
Blake Miller25-Jan-06 6:58
Blake Miller25-Jan-06 6:58 
GeneralRe: Exception (Access violation reading location) Pin
llp00na25-Jan-06 7:32
llp00na25-Jan-06 7:32 
AnswerRe: Exception (Access violation reading location) Pin
Michael Dunn25-Jan-06 10:26
sitebuilderMichael Dunn25-Jan-06 10:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.