A Couple of C++ Quickies


I wrote two fairly trivial libraries to support my development of the timezone class that I’ve mentioned before.

1.  Reading Directories in C++ describes a class that loops through directories.  It’s portable to both POSIX and Windows so that you don’t have to do that in two very different ways.

2.  Getting Zoneinfo Data on Windows describes a simple way to do what the title says along with a couple of functions that let you create and delete symbolic links on Windows.

Both of those are really old news, and neither has any great ideas of my own, so all that code is in the public domain.

The timezone class is finished, but I still need to do some testing before I release the code to the world and, perhaps, embarrass myself. 😎  I hope to have that done later today.

Comments

  1. Peter B says

    This is OT but… I’ve noticed in C/C++ that the number 6 can be used in a variety of ways.
    x+=6;
    x>>6
    fpValue*6 // here I assume this is okay syntax
    Case 6:
    Yet when defining an object that uses 6 for some reason it must become a static part of the object and take real space in the object. Define as int six=6 in object definition and possibly embed 6 into an instruction without having to fetch the value from some address.
    This just seems silly.

  2. billseymour says

    Peter B, I’m not sure what you’re getting at; but you don’t seem to be describing anything about either C or C++ (which are two different languages, by the way…there’s no language called “C/C++”).

    In the example you give:

    int six = 6;
    

    what’s “taking up space” is the thing that you yourself have given the name, “six”.  The “6” could indeed be an immediate operand in some instruction (assuming that the CPU has an immediate addressing mode).

    Can you explain more clearly what you’re talking about?

  3. Peter B says

    In an object definition, “const int six=6” should be legal and not have to create a storage location containing the value 6. If I wanted to always have six=6 my object init would have to say const int six=6. And I found that annoying. At least some years ago that was true. (Lately, I tend to write mostly assembly.) I’m wondering why it still is or was that way.
    .
    To most people, C++ is an extension of C because that’s what it looks like. I tend to use “C/C++” meaning either of both.

  4. billseymour says

    Peter B:  by “an object definition” I’m guessing that you mean “a class definition” (“object” has a technical meaning in both C and C++ that’s different from what it means in OOP-only languages…an object in C is a region of memory, an object in C++ is a region of memory that has a type).

    Your “const int six=6” (assuming that there’s a semicolon after it) declares a non-static data member named “six” so, yes, it “takes up space” in all instances of the class.

    You might be thinking that const (short for “constant”) means more than it does.  It does not mean that the object can’t be modified; it means that you can’t change it when you call it “six”.  (constness is a property of the identifier, not of the object.)

    Starting with C++11, there’s the keyword, constexpr (short for “constant expression”, which means something computed at compile time, not at run time).

    static constexpr int six = 6;
    

    probably does what you want.

    To most people, C++ is an extension of C …

    Most people are wrong.  The two languages share a good bit of syntax (statement syntax inherited from a language called Algol, for example), but neither is a strict subset of the other, and they’re defined by totally different ISO standards.

  5. beholder says

    @1,3 Peter B

    This just seems silly.

    You declared a region of memory in your executable was a variable, gave it the name “six” and assigned the value 6 to it. Were you expecting something else to happen? Did you have it confused with preprocessor definitions?


    #ifndef SIX
    #define SIX 6
    #endif

    The above define shouldn’t take up any “real space” in the machine code. It’s an instruction for the compiler.

  6. billseymour says

    beholder:  yeah, but I deliberatly avoided mentioning macros because (IMO) they’re ugly; and simple ones like your example aren’t really needed given C++11’s constexpr keyword.  Also, I wanted to explain in more detail what was going on and to mention something about how C++ jargon differs from what you often hear in reference to other languages.  Maybe I was being too pedantic.

Leave a Reply

Your email address will not be published. Required fields are marked *