473,396 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Stroustrup 5.9 exercise 9

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
5 1814
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
* 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
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
every reply here is "out of my head" :-(

Nov 9 '06 #5
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Oplec | last post by:
Hi, I am learning standard C++ as a hobby. The C++ Programming Language : Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I...
7
by: arnuld | last post by:
problem: define functions F(char), g(char&) & h(const char&). call them with arguments 'a', 49, 3300, c, uc & sc where c is a char, uc is unsigned char & sc is signed char. whihc calls are legal?...
0
by: arnuld | last post by:
Stroustrup has a table in section 4.9 of declarations and definitions. he asks to write a similar table but in opposite sense: char ch; // declaration with definition he asks to do the...
0
by: arnuld | last post by:
this programme runs without any trouble. it took 45 minutes of typing. i posted it here so that people can save their precious time: // Stroustrup special edition // chapter 4 exercise 2 //...
2
by: arnuld | last post by:
MAX and MIN values of CHAR could not be displayed. Why ? BTW, any advice on improvement ? (please remember i have covered chapter 4 only) ------------- PROGRAMME -------------- /*...
16
by: arnuld | last post by:
i did what i could do at Best to solve this exercise and this i what i have come up with: ----------- PROGRAMME -------------- /* Stroustrup 5.9, exercise 3 STATEMENT: Use typedef to define...
11
by: arnuld | last post by:
/* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a table of the name sof months o fyear and the number of days in each month. write out that table. Do this twice: 1.) using ar array of char...
6
by: arnuld | last post by:
this one was much easier and works fine. as usual, i put code here for any further comments/views/advice: --------- PROGRAMME ------------ /* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a...
14
by: arnuld | last post by:
there is no "compile-time error". after i enter input and hit ENTER i get a run-time error. here is the code: ---------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 11 STATEMENT:...
27
by: arnuld | last post by:
it works fine without any trouble. i want to have advice on improving the code from any angle like readability, maintenance etc: ---------- PROGRAMME ------------ /* Stroustrup, 5.9, exercise 11...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.