By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Stroustrup 5.9 exercise 9

P: n/a
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."

Nov 8 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Nov 8 '06 #2

P: n/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."
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?
Nov 8 '06 #3

P: n/a
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
Nov 8 '06 #4

P: n/a
every reply here is "out of my head" :-(

Nov 9 '06 #5

P: n/a
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

Nov 9 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.