Member is private..??

This is a discussion on Member is private..?? within the C++ Programming forums, part of the General Programming Boards category; That's right. One of the only examples I can think of where I had to perform a string to enum ...

  1. #16
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    That's right. One of the only examples I can think of where I had to perform a string to enum and vice versa was dealing with xml files and shuffling attribute values into objects that held enum data.

    and it was a snap in C#

    but in c++, not so much. You should develop a "when in Rome..." attitude when going from one programming language to another. C++ can frustrate me to no end because many things are just easier to pull-off in other languages. Sometimes I feel like a complete idiot coding things in C++. Oh well, I do it because i think it's fun and challenging.
    goto( comeFrom() );

  2. #17
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by StainedBlue View Post
    That's right. One of the only examples I can think of where I had to perform a string to enum and vice versa was dealing with xml files and shuffling attribute values into objects that held enum data.
    Yep...and that's what I pretty much need it for (sortof..). I'm working on a web coder program which can code in html and other web languages. One of the problems I ran into was I needed some way to get the string names of enum values, and seeing as the enum for my ChtmlTags class is pretty long what with all the different html tags and I didn't want to hard-code every single enum value string, I wrote the program which can be found here (or more accurately, it can be found at a link linked from there).
    and it was a snap in C#
    That may be...but I'm still in the process of learning C++, and so I can hardly switch to C# without first finishing my learning C++.
    but in c++, not so much. You should develop a "when in Rome..." attitude when going from one programming language to another. C++ can frustrate me to no end because many things are just easier to pull-off in other languages. Sometimes I feel like a complete idiot coding things in C++. Oh well, I do it because i think it's fun and challenging.
    Yeah, I think coding in C++ is fun and challenging too. I could have picked a different language to start learning but I picked C++ since I got advice from someone to start with C++ because it makes it easier to learn different languages later on. Don't know if that's true or not, but I intend to stick with C++ until I master all the basics of the language.
    Last edited by Programmer_P; 06-03-2010 at 09:57 PM.

  3. #18
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Well IMO C++ is not a web-friendly language, so if web development is your bag, then C++ may not be doing much for you, other than exercising your brain. Some people may disagree about C++ and the web, but I would say they're sadistic.

    But seriously, an enum of *all* html tags or attributes is a bit of a stretch. I think you would've found that implementing a map of string keys to whatever you're trying to do would probably be a lot less work and a lot prettier to look at.
    goto( comeFrom() );

  4. #19
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by StainedBlue View Post
    But seriously, an enum of *all* html tags or attributes is a bit of a stretch. I think you would've found that implementing a map of string keys to whatever you're trying to do would probably be a lot less work and a lot prettier to look at.
    Maybe so.
    No doubt I'll always discover better ways of doing stuff after already writing the code.
    The issue is though whether or not its really worth changing...

    Anyway, I'm not really all that familiar with maps, and I think enums are simpler for beginner C++ programmers.

  5. #20
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    What? But enums are only hard-coded enumerated constants. If you make it more than that, you're doing something totally different.
    I'm not making the enum different at all. I don't change the enums with my program.
    I merely retrieve the string names of the enum values. Two different things...
    If you want to use the constant, do that; if you want to save the stringified name, it's not hard to use a string array or whatever.
    It is if the enum is very long...
    In my web coder program, which I was writing before I decided to write "ConvertEnumToStrings", I encountered a problem where I needed a QStringList of the supported html attributes for a specific html element (each html element has its own corresponding enum value).
    Rather than hard-coding for every single enum value name, I came up with the brilliant idea that resulted in ConvertEnumToStrings.
    True, it might have ended up being more code to write, but at least now I know I have something which I can use with ANY enum in similiar situations, and I wont have to hard-code every single time.

    That is the purpose of the program.
    Last edited by Programmer_P; 06-03-2010 at 10:23 PM.

  6. #21
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by whiteflags View Post
    What? But enums are only hard-coded enumerated constants.
    I think Programmer_P is basically saying s/he wrote a program to autogenerate enums, or autogenerate code from an enum or something to do with autogenerating code. It's not clear to me that doing this needed to be done in C++, eg:

    My program gets an enum from file, writes it to a header (or source, if you so choose...) in a class, and provides a couple of member functions. One will retrieve a vector of strings containing all the enum values converted to strings, and the other will retrieve a specific enum value if you want to do it that way.
    sounds like something that could have been more easily done in a higher level language, but it really doesn't matter. I've read C or text into a perl script that spits out some different C (eg, auto generating enums or reverse string versions thereof), lots of people do this for various purposes I believe. You could write a C/C++ compiler in C/C++ or some other language, not that this is quite the same but it is does demonstrate the machine is an effective tool this way (or can be).

    Quote Originally Posted by StainedBlue View Post
    Well IMO C++ is not a web-friendly language, so if web development is your bag, then C++ may not be doing much for you, other than exercising your brain. Some people may disagree about C++ and the web, but I would say they're sadistic.
    A lot of server side stuff is written in C/C++ (eg, Apache itself), and of course the client side browser usually a C++ effort. Interpreters for (server side) web scripting languages are in C, interpreters for client side applications (such as flash or javascript) mostly C++.
    Last edited by MK27; 06-03-2010 at 10:14 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #22
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Ah, I meant the server-side languages themselves, not really what their interpreters were built with. But yes, in that respect, C/C++ have likely made significant contributions to the web.
    goto( comeFrom() );

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    Honestly, you should look up std::map. It sounds like you have a HTML element and a HTML tag and need to check if that element is valid inside that tag.
    A simple boolean value in a map could fix that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by StainedBlue View Post
    Ah, I meant the server-side languages themselves, not really what their interpreters were built with.
    My point was that it is totally normal and commonplace for server-side interfaces to be written in C/C++, and in fact these applications are integral to the operation of the web itself. You don't think Google is all just PHP or something, do you? I promise they have people working in C/C++ everyday all day, doing server side web development.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #25
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    Honestly, you should look up std::map. It sounds like you have a HTML element and a HTML tag and need to check if that element is valid inside that tag.
    A simple boolean value in a map could fix that.
    Not quite...
    More correctly, I have all standard html tags and all standard html attributes for all html elements. I then have a ChtmlElements class which provides several functions, such as one for retrieving a html element. The one I needed the enum to strings program for was a function to retrieve a QStringList of the supported attributes for a html element (which is specified in a function parameter that expects an enum value of an enum of the class which provides values for each html element).
    Do maps provide encapsulation and other class features?
    Is there anything classes have that maps don't?

    I will go read up on them now.

  11. #26
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Programmer_P View Post
    Not quite...
    More correctly, I have all standard html tags and all standard html attributes for all html elements. I then have a ChtmlElements class which provides several functions, such as one for retrieving a html element. The one I needed the enum to strings program for was a function to retrieve a QStringList of the supported attributes for a html element (which is specified in a function parameter that expects an enum value of an enum of the class which provides values for each html element).
    This is not a bad idea, it kind of depends on why you are using the enum in the first place instead of just strings. Certainly the enum might be more efficient but perhaps not, since html tags are strings to start with so you'd have to translate them anyway (back and forth, it sounds like...)

    Do maps provide encapsulation and other class features?
    Not in the sense that classes do, but a map could be a member of a class, or accessed/used by an object, just like an enum.

    Certainly, if you haven't used maps or something similar somewhere, you should start right away! They are very useful "general purpose" things.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    Why do you need the enum? Example of how you use it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Programmer_P View Post
    But it does not seem like the best idea to pass a string matching the string html element, like it sounds like you're trying to suggest...
    That is why I'm using an enum.
    Why not? You pass a reference or a pointer, not a copy, so there is no difference between this and an (enum) number. I would say that the advantage of the number is not having to do string comparisons, but you need to do those at some point anyway to form a correlation. Kind of depends on the frequency and nitty-gritty details.

    A lot of precise data processing is done with strings. They are essentially numbers anyway -- there's nothing "inappropriate" about using them as arguments and so on. You just need to take their properties into account where appropriate (eg, case).
    Last edited by MK27; 06-04-2010 at 10:44 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #29
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by MK27 View Post
    Why not? You pass a reference or a pointer, not a copy, so there is no difference between this and an (enum) number. I would say that the advantage of the number is not having to do string comparisons, but you need to do those at some point anyway to form a correlation. Kind of depends on the frequency and nitty-gritty details.

    A lot of precise data processing is done with strings. They are essentially numbers anyway -- there's nothing "inappropriate" about using them as arguments and so on. You just need to take their properties into account where appropriate (eg, case).
    Well, let's see...
    I have 3 major classes all which work together to form one unit:

    ChtmlTags
    ChtmlAttributes
    ChtmlElements

    ChtmlElements ties together ChtmlTags and ChtmlAttributes (i.e. it provides a method for retrieving the complete elements, i.e. the tags + a html attribute(s) QstringList variable which one can pass a QStringList containing the attributes he/she wants to retrieve in the element he's getting). There are other functions too, but I don't need to mention them all them here to describe my program and why I need to use an enum. I'll only state this:

    Basically, in ChtmlElements, I have three functions which need my ConvertEnumToStrings program. Those functions are:

    QString& getElement(ET_ELEMENT element, QStringList& attrs, QStringList& attrsValues, QString& elementContent);
    QStringList& getListOfSupportedAttrs(ET_ELEMENT element);
    QStringList& getSupportedAttrValues(ET_ELEMENT element, ChtmlAttributes::ET_ATTR attr);

    Ok, now those two names "ET_ELEMENT" and "ATTR" are both enums. The first is an enum of the ChtmlElements class. The last one is an enum of the ChtmlAttributes class.

    As already mentioned/hinted at, the ET_ELEMENT enum has an enum value corresponding to each html element. It will be used by all 3 functions to determine which html element to deal with. Similiarly, the ET_ATTR enum will be used to specify the html attribute we're operating on/with.
    Yes, i could pass a string name exactly the same as the string name of a corresponding element or attribute, but if I did that, I might as well just hard-code the string, and not use the function at all. Not to mention all the memory that would be used...
    That is why I'm using an enum in the first place.
    Now I'll explain the reasons why each one of the 3 functions need my ConvertEnumToStrings program:

    getElement() needs it because it seems to make sense to have a QStringList containing all the enum value names to pass to the function in the "QStringList& attrs" parameter.
    With my program that I just wrote, I will be able to call a "getEnumValues()" member function of the CET_ELEMENTToStrings class generated by the program to retrieve a vector of the string names of all the enum values (without hard-coding each one manually). Then I can just iterate through the vector in a function of the "Chtml" class (which ties the other 3 classes together, by inheriting from ChtmlElements, and adding the rest of the functions needed to make the whole interface work), and based on what attributes the user specified in the GUI he wished to add, I can add certain attributes to my QStringList which I then pass to getElement which can then operate on that information, and return the correct data (or more precisely, a reference to that data). Make sense?

    getListOfSupportedAttrs() needs it (i.e. ConvertEnumToStrings) because in order to return a QStringList reference to a QStringList of the supported attributes for a given element, it can make use of the easy getEnumValues() function generated in the header by the program to retrieve a vector of strings (or more accurately, a reference to the vector of strings) of the enum values of the ET_ELEMENT enum, and add certain enum values of the enum (or more precisely, their string names), while excluding others, to a QStringList which it then returns a reference to.

    getSupportedAttrValues() will return a reference to a QStringList of the supported attr values for a given element + attribute combination. Rather than hard-code for every single enum value of ET_ELEMENT and ET_ATTR, which would take a lot of code in a single function, I can run ConvertEnumToStrings and operate on the ChtmlElements.h and ChtmlAttributes.h headers, and get both of their enums written to separate files along with functions to retrieve that information (one of them being the getEnumValues() function which I already mentioned). I then just include the headers in ChtmlElements.cpp and in the getSupportedAttrValues() definition, I use the getEnumValues() function to retrieve a reference to the vector of strings containing the enum value names. I then iterate through that vector, comparing the strings to see if matches any particular string name of an enum value (which unfortunately, I'll have to hard-code, but I think it might be less code in getSupportedAttrValues...) or something, and if so, add the corresponding html attribute string to the element string...

    Anyway, I haven't got it all worked out yet, but at the time, my ConvertEnumToStrings program did seem necessary.
    As for passing a string to getListOfSupportedAttrs(), like you suggested twice already, the reason that is impractical is because I would need to manually write the string names to match the string names of the html attribute members (meaning I'll have to write the html attributes twice), and if I do that, then is what the point of using a class and functions to retrieve information at all? I might as well just hard-code everything...
    Or maybe you were suggesting writing the string names of the enum values, which is what I end up doing in getListOfSupportedAttrs(), which I think is a lot better than doing it in the caller function. At is now, I just have to pass a QStringList of enum value names which I've easily gotten from getEnumValues() generated by ConvertEnumToStrings, which is a lot less code than hard-coding every thing in the caller function.

    Do you see the difference now? I think my method is a lot more efficient...
    But I'm welcome to any further suggestions of course.

  15. #30
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    Why do you need the enum? Example of how you use it?
    Read the previous post.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Replies: 2
    Last Post: 04-22-2008, 01:07 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. private data member with public set member function
    By Mario in forum C++ Programming
    Replies: 2
    Last Post: 05-28-2002, 11:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21