Stroustrup 5.9 exercise 9

A

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."
 
B

Bernd Strieder

Hello,
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
 
A

Alf P. Steinbach

* 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

Andrey Tarasevich

arnuld said:
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.
 
B

Bernd Strieder

Hello,
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top