Isn't it a mistake?

Hi, I've seen this problem already on other forums, but I still haven't found concrete answer or it's sollution. This is exercise 32, chapter 10, from "Thinking in C++" by Bruce Eckel:

In a header file, create a class Mirror that contains two data members: a pointer to a Mirror object and a bool. Give it two constructors: the default constructor initializes the bool to true and the Mirror pointer to zero. The second constructor takes as an argument a pointer to a Mirror object, which it assigns to the object’s internal pointer; it sets the bool to false. Add a member function test( ): if the object’s pointer is nonzero, it returns the value of test( ) called through the pointer. If the pointer is zero, it returns the bool. Now create five cpp files, each of which includes the Mirror header. The first cpp file defines a global Mirror object using the default constructor. The second file declares the object in the first file as extern, and defines a global Mirror object using the second constructor, with a pointer to the first object. Keep doing this until you reach the last file, which will also contain a global object definition. In that file, main( ) should call the test( ) function and report the result. If the result is true, find out how to change the linking order for your linker and change it until the result is false.


Here is my code...

Mirror.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef MIRROR_H
#define MIRROR_H

class Mirror
{
	Mirror* selfPtr;
	bool end;
public:
	Mirror(): selfPtr(0), end(true) {}
	Mirror(Mirror* ptr): selfPtr(ptr), end(false) {}
	bool test()
	{
		if(selfPtr)
		{
			return selfPtr->test();
		}
		return end;
	}
};

#endif //MIRROR_H 

one.cpp
1
2
3
#include"Mirror.h"

Mirror mir1;

two.cpp
1
2
3
4
#include"Mirror.h"

extern Mirror mir1;
Mirror mir2(&mir1);

three.cpp
1
2
3
4
#include"Mirror.h"

extern Mirror mir2;
Mirror mir3(&mir2);

four.cpp
1
2
3
4
#include"Mirror.h"

extern Mirror mir3;
Mirror mir4(&mir3);

five.cpp
1
2
3
4
5
6
7
8
9
10
#include"Mirror.h"
#include<iostream>

extern Mirror mir4;
Mirror mir5(&mir4);

int main()
{
    std::cout << std::boolalpha << mir5.test() << std::endl;	
}


So the question is, how should I change linking order?? And if the linking order would be cahnged is it really possible that this function would return false??
I mean, as for me, no matter in what order this objects would be initialized, cause while initializing they don't need the value of the object, they just need the address. The point is that before main() all of them would be initialized, and the result can't be false, is it?
Thanks in advance:)
Last edited on
I agree, it can never be false. I don't even know what on earth he means by suggesting that the linking order would affect program behavior.
The point of the exercise was to expose the "Static Initialization Fiasco".
http://www.parashift.com/c++-faq/static-init-order.html
But that's not the case, here. The constructor for each object doesn't require any other object to be initialized. The addresses are static, so no undefined behavior could possibly be triggered.
I agree. The exercise is flawed.
Thank you guys, I was trapped with this exercise, but no I can go on reading...
Topic archived. No new replies allowed.