Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Stroustrup 5.9 exercise 9

Reply
Thread Tools

Stroustrup 5.9 exercise 9

 
 
arnuld
Guest
Posts: n/a
 
      11-08-2006
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."

 
Reply With Quote
 
 
 
 
Bernd Strieder
Guest
Posts: n/a
 
      11-08-2006
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-08-2006
* 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?
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-08-2006
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
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      11-09-2006
every reply here is "out of my head"

 
Reply With Quote
 
Bernd Strieder
Guest
Posts: n/a
 
      11-09-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stroustrup 5.9 exercise 11 arnuld C++ 1 11-09-2006 06:38 AM
Stroustrup 5.9 exercise 6 (char& as argument) arnuld C++ 5 11-08-2006 06:03 PM
Stroustrup exercise 7 section 5.9 (using struct) arnuld C++ 6 11-08-2006 04:40 PM
Stroustrup exercise 7 section 5.9 (using arrays) arnuld C++ 2 11-08-2006 03:18 PM
Stroustrup 5.9 exercise 6 arnuld C++ 7 11-08-2006 01:44 PM



Advertisments