|
|||
|
I'm trying to learn and adopt some new programming techniques, and
I've reached a roadblock. Specifically, although I have most of a class defined and declared, I don't know how to use it. In the code below I have functions that populate the elements of a class object, but I don't know how to (1) store the object in the declared map and (2) access/update the information in the objects that are stored. My intent here is to have one or more objects stored in the map, but to instantiate each and use information from one for a cycle of activity, followed by placing it back into the map. These activities will repeat off and on throughout the program's execution, and I need to reacquire some of the objects (one at a time) to use and update the information that's current in the object. So, I need to initially instantiate each object as needed, use and modify its data values, save the object back into the map, use another map object, and (possibly) reuse any of the objects I've processed. My quandry is how to for each object initially store, access/update it as needed, restore it and build/use another object, etc. The following code doesn't compile, telling me I'm trying to use my declarations incorrectly: pfWork.setFileName(printFile);// store new print file info object pfWork.setIsOpen(true), pfWork.clrRecCount(); print_map[cRCode] = pfWork; // cRCode is a character variable The class declarations are: class PrintFile // Print File class { bool bIsOpen; // private members bool bIsUsed; int nRecords; string strFileName; public: PrintFile(const string sFileName) : // constructor bIsOpen(true), bIsUsed(true), nRecords(0), strFileName(sFileName) {}; PrintFile() : // default constructor bIsOpen(false), bIsUsed(false), nRecords(0), strFileName("") {}; PrintFile(const PrintFile &pf) : // copy constructor bIsOpen(pf.bIsOpen), bIsUsed(pf.bIsUsed), nRecords(pf.nRecords), strFileName(pf.strFileName) {}; PrintFile &operator= (const PrintFile &rhs) // operator = { if(this == &rhs) return *this; // don't assign to self bIsOpen = rhs.bIsOpen; bIsUsed = rhs.bIsUsed; nRecords = rhs.nRecords; strFileName = rhs.strFileName; return *this; }; // access private members bool getOpened () const { return bIsOpen; }; bool getUsed () const { return bIsUsed; }; int getRecords () const { return nRecords; }; string getFileName () const { return strFileName; }; void setFileName (const string sfn) { strFileName = sfn; }; void setIsOpen (const bool bState) { bIsOpen = bState; }; void setIsUsed (const bool bState) { bIsUsed = bState; }; void addRecCount () { nRecords++; }; void clrRecCount () { nRecords = 0; }; }; // end of class PrintFile typedef map<char, PrintFile> print_map; print_map printFiles; // 1 object per event PrintFile pfWork; // working print file object |
|
|
||||
|
||||
|
|
|
|||
|
On 7/31/2012 4:38 PM, Mike Copeland wrote:
> I'm trying to learn and adopt some new programming techniques, and > I've reached a roadblock. Specifically, although I have most of a class > defined and declared, I don't know how to use it. Huh? My experience tells me that you have to know how to use your class *before* you even attempt to declare/define it. Are you defining it in a vacuum? What are your requirements? Where do they come from if not from the *use* of that class? > In the code below I have functions that populate the elements of a > class object, but I don't know how to (1) store the object in the > declared map and (2) access/update the information in the objects that > are stored. > [..] > The following code doesn't compile, telling me I'm trying to use my > declarations incorrectly: > [..] Have you read the FAQ? Pay close attention to #5.8. V -- I do not respond to top-posted replies, please don't ask |
|
|||
|
On Tue, 2012-07-31, Victor Bazarov wrote:
> On 7/31/2012 4:38 PM, Mike Copeland wrote: >> I'm trying to learn and adopt some new programming techniques, and >> I've reached a roadblock. Specifically, although I have most of a class >> defined and declared, I don't know how to use it. > > Huh? My experience tells me that you have to know how to use your class > *before* you even attempt to declare/define it. Are you defining it in > a vacuum? What are your requirements? That was my problem when trying to read the code. I saw a PrintFile class, but had a hard time imagining what the OP wanted it to *mean*. The posting didn't provide any clues (I read it very quickly though). I warmly recommend writing a sentence or two of class documentation *before* starting to write down the class declatation. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o . |
|
|||
|
In article <jv9hnf$h38$1@dont-email.me>, v.bazarov@comcast.invalid
says... > > I'm trying to learn and adopt some new programming techniques, and > > I've reached a roadblock. Specifically, although I have most of a class > > defined and declared, I don't know how to use it. > > Huh? My experience tells me that you have to know how to use your class > *before* you even attempt to declare/define it. Are you defining it in > a vacuum? What are your requirements? Where do they come from if not > from the *use* of that class? > That's what I'm trying to do - learn how to use a class structure. In this example, I'm converting an existing implementation that uses a more simple struct and array, hoping to make the logic more flexible and "up-to-date". I don't know how to blend a class and a container object, both of which I've had some experience but not together. > > In the code below I have functions that populate the elements of a > > class object, but I don't know how to (1) store the object in the > > declared map and (2) access/update the information in the objects that > > are stored. > > [..] > > The following code doesn't compile, telling me I'm trying to use my > > declarations incorrectly: > > [..] > > Have you read the FAQ? Pay close attention to #5.8. No, I haven't. I'll do that... |
|
|||
|
In article <slrnk1glis.ph6.grahn+nntp@frailea.sa.invalid>,
grahn+nntp@snipabacken.se says... > On Tue, 2012-07-31, Victor Bazarov wrote: > >> I'm trying to learn and adopt some new programming techniques, and > >> I've reached a roadblock. Specifically, although I have most of a class > >> defined and declared, I don't know how to use it. > > > > Huh? My experience tells me that you have to know how to use your class > > *before* you even attempt to declare/define it. Are you defining it in > > a vacuum? What are your requirements? > > That was my problem when trying to read the code. I saw a PrintFile > class, but had a hard time imagining what the OP wanted it to *mean*. > The posting didn't provide any clues (I read it very quickly though). > > I warmly recommend writing a sentence or two of class documentation > *before* starting to write down the class declatation. > As I stated to Victor's reply, I'm trying to blend several disciplines (class and STL container) in an attempt to make something I've already developed that's crude and clumsy. I have some experience with each, but my difficulty is in trying to combine them into something that might be more elegant and performent than what I've already done. Is there something wrong with trying to "get better" in this field? |
|
|||
|
On 7/31/2012 6:29 PM, Mike Copeland wrote:
> In article <jv9hnf$h38$1@dont-email.me>, v.bazarov@comcast.invalid > says... >>> I'm trying to learn and adopt some new programming techniques, and >>> I've reached a roadblock. Specifically, although I have most of a class >>> defined and declared, I don't know how to use it. >> >> Huh? My experience tells me that you have to know how to use your class >> *before* you even attempt to declare/define it. Are you defining it in >> a vacuum? What are your requirements? Where do they come from if not >> from the *use* of that class? >> > That's what I'm trying to do - learn how to use a class structure. > In this example, I'm converting an existing implementation that uses a > more simple struct and array, hoping to make the logic more flexible and > "up-to-date". > I don't know how to blend a class and a container object, both of > which I've had some experience but not together. Those concepts are orthogonal. If your class is suitable to be kept in a container, it doesn't matter whether the container is an array, or a map, or whatnot. Essentially, if you already have some code that stores your object in an array, all you really should need to do is replace the declaration of the array with your map, and you should be pretty much set. There is no difference in *how you use your class* whether it's stored in a map or it's a pure stand-alone object. None. Whatsoever. >[..] V -- I do not respond to top-posted replies, please don't ask |
|
|||
|
On Jul 31, 11:33*pm, mrc2...@cox.net (Mike Copeland) wrote:
> In article <slrnk1glis.ph6.grahn+n...@frailea.sa.invalid>, > grahn+n...@snipabacken.se says... > > On Tue, 2012-07-31, Victor Bazarov wrote: > > >> I'm trying to learn and adopt some new programming techniques, and > > >> I've reached a roadblock. *Specifically, although I have most of aclass > > >> defined and declared, I don't know how to use it. > > > > Huh? *My experience tells me that you have to know how to use your class > > > *before* you even attempt to declare/define it. *Are you defining it in > > > a vacuum? *What are your requirements? it does seem backwards... > > That was my problem when trying to read the code. I saw a PrintFile > > class, but had a hard time imagining what the OP wanted it to *mean*. > > The posting didn't provide any clues (I read it very quickly though). could the OP provide a short explanation of what PrintFile does? how does a file get opened and shouldn't some sort of file handle be included if the file is opened? Do you really need a copy CTOR and assignment operator? > > I warmly recommend writing a sentence or two of class documentation > > *before* starting to write down the class declatation. > > * *As I stated to Victor's reply, I'm trying to blend several > disciplines (class and STL container) there isn't (or usually isn't) anything special about classes whne it comes to the STL. If you can store an int or a string (which is really a class) in a map why is your class a problem? > in an attempt to make something > I've already developed that's crude and clumsy. *I have some experience > with each, but my difficulty is in trying to combine them into something > that might be more elegant and performent than what I've already done. > * *Is there something wrong with trying to "get better" in this field? none but we aren't clear what your problem is. here's some docuemntaion for std::map http://www.cplusplus.com/reference/stl/map/ can't you use insert() (or operator[]), find(), and erase() (though consider not removing things from the map until you've actually finished with them). |
|
|||
|
In article <MPG.2a820a1822d381dc989716@news.eternal-september.org>
mrc2323@cox.net (Mike Copeland) writes: >In article <jv9hnf$h38$1@dont-email.me>, v.bazarov@comcast.invalid >says... >> > I'm trying to learn and adopt some new programming techniques, and >> > I've reached a roadblock. Specifically, although I have most of a class >> > defined and declared, I don't know how to use it. >> >> Huh? My experience tells me that you have to know how to use your class >> *before* you even attempt to declare/define it. Are you defining it in >> a vacuum? What are your requirements? Where do they come from if not >> from the *use* of that class? >> > That's what I'm trying to do - learn how to use a class structure. >In this example, I'm converting an existing implementation that uses a >more simple struct and array, hoping to make the logic more flexible and >"up-to-date". Been there, done that, strangled myself with the t-shirt. My advice, for the level of familiarity you seem to have, is not to do that. Start with a new class (collection of classes) and work with using them in containers. When you have a handle on that, start with a blank class, define your interfaces and then (where possible) merge in the code from the old implementation. There is a good chance that you will only be able to use the old code as a guide, unless the original implementation did a good job of encapsulation. (You can do that in C, but most programmers don't seem to bother.) My normal web reference for containers is http://www.cplusplus.com/reference/ They may also have tutorials, I don't recall. -- Drew Lawson | Savage bed foot-warmer | of purest feline ancestry | Look out little furry folk | it's the all-night working cat |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|