Stroustrup 5.9 exercise 9

Discussion in 'C++' started by arnuld, Nov 8, 2006.

  1. arnuld

    arnuld Guest

    i tried but i am not able to think of any solution of this:

    "find an example where it would make sense to use a name in its own
    initializer."
    arnuld, Nov 8, 2006
    #1
    1. Advertising

  2. Hello,

    arnuld wrote:

    > i tried but i am not able to think of any solution of this:
    >
    > "find an example where it would make sense to use a name in its own
    > initializer."


    Ordinary copy c'tor?

    C'tor with initializers from global object (kind of prototype object).

    But since it is so early in the book, and the chapter seems to be on
    pointers, somehow a value might depend on e.g. the address.

    extern long d;

    long ptrtolong (long *);

    long ptrtolong(long* lp)
    {
    if (lp == &d)
    return 1L;
    else
    return 0L;
    }

    long d (ptrtolong(&d));

    int main()
    {
    if (d!=0) throw d;
    }

    I hope for you there is something less arcane to it.

    Bernd Strieder
    Bernd Strieder, Nov 8, 2006
    #2
    1. Advertising

  3. * arnuld:
    > i tried but i am not able to think of any solution of this:
    >
    > "find an example where it would make sense to use a name in its own
    > initializer."


    A typical example is an OS API struct defined like

    struct OsThingaMajick
    {
    std::size_t byteSize;
    int something;
    char blah[502];
    void* dunno;
    double version2feature;
    bool version3feature;
    };

    Then OS API functions inspect the byteSize member to determine first of
    all whether you've remembered to initialize, and second which version
    this is, that is, which members at the end can be assumed to be there.

    A competent C++ programmer will define an OsThingaMajick object like

    OsThingaMajick aha = { sizeof(aha) };

    relying on implicit (and guaranteed) zero-initialization of the
    following members, and perhaps adding

    COMPILE_TIME_ASSERT( offsetof( OsThingaMajick, byteSize ) == 0 );

    to document the assumption that byteSize is the first member.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 8, 2006
    #3
  4. arnuld wrote:

    > i tried but i am not able to think of any solution of this:
    >
    > "find an example where it would make sense to use a name in its own
    > initializer."
    >


    It might depend of the meaning of "its own" in the above sentence. It is not
    immediately clear whether it is required that this name refers to the same
    object in both contexts (as initializer and as initializee).

    If there's no such requirement, then I can add the following example

    struct A {
    int i;
    A(int i) : i(i)
    {}
    };

    where a member named 'i' is initialized with the value of parameter named 'i'.
    These are two different objects, of course, but the name is the same.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 8, 2006
    #4
  5. arnuld

    arnuld Guest

    every reply here is "out of my head" :-(
    arnuld, Nov 9, 2006
    #5
  6. Hello,

    arnuld wrote:

    > every reply here is "out of my head" :-(


    That's probably not a problem. If you really need to think on that
    problem when programming, you will get it right, in a natural way, if
    you know enough C++.

    We had a selection of problems in this thread where that obscure thing
    of initializing something using its own name might happen. We don't
    know what the author had in mind, and why he expected somebody working
    through the book being able to give a reasonable answer, whether it was
    meant as tricky question or whatever.

    I first thought the OsThingaMajick answer by Alf P. Steinbach is closest
    to the intended, because it matches the content of the chapter best,
    but it does not use the name of an object in its initializer, but its
    type. So I think it is not precisely an answer to the question.

    But expanding on his, you might want a pointer to itself within a
    struct. E.g. a linked list, where the next pointer points to itself in
    the last node.

    struct intlistnode
    {
    int v;
    intlistnode * next;
    };

    extern intlistnode n1;
    extern intlistnode n2;
    extern intlistnode n3;

    intlistnode n1 = {2,&n2};
    intlistnode n2 = {3,&n3};
    intlistnode n3 = {4,&n3};

    int main()
    {
    if (n1.next->next != n1.next->next->next) throw n1;
    }

    Now you have a list of integers arranged by the compiler, starting with
    n1. And you can check you have reached the end node by testing whether
    next points to the node itself. Others might prefer using a zero
    pointer to signal this.

    Bernd Strieder
    Bernd Strieder, Nov 9, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. arnuld

    Stroustrup 5.9 exercise 6

    arnuld, Nov 8, 2006, in forum: C++
    Replies:
    7
    Views:
    364
    arnuld
    Nov 8, 2006
  2. arnuld
    Replies:
    5
    Views:
    338
    Alf P. Steinbach
    Nov 8, 2006
  3. arnuld
    Replies:
    2
    Views:
    288
    arnuld
    Nov 8, 2006
  4. arnuld
    Replies:
    6
    Views:
    378
    arnuld
    Nov 8, 2006
  5. arnuld

    Stroustrup 5.9 exercise 11

    arnuld, Nov 9, 2006, in forum: C++
    Replies:
    1
    Views:
    348
    Gary Wessle
    Nov 9, 2006
Loading...

Share This Page