Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8465239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:59:06+00:00 2026-06-10T14:59:06+00:00

Learning C++ with help of "Thinking in C++" by Bruce Eckel,stuck in exercise 32,

  • 0

Learning C++ with help of "Thinking in C++" by Bruce Eckel,stuck in exercise 32, chapter 10.
The question is how to change link order, that Mirror::test() called for object m5 return false.
Here is my code.

mirror.h:

#ifndef MIRROR_H_
#define MIRROR_H_

class Mirror {
 public:
  Mirror() {logic_ = true; self_ = 0;};
  Mirror(Mirror *ptr) {self_ = ptr; logic_ = false;};
  bool test() {
    if (self_ != 0) {
      return self_->test();
    } else {
      return logic_;
    }
  };

 private:
  bool logic_;
  Mirror *self_;
};


#endif // MIRROR_H_

task

one.cpp

#include "mirror.h"
Mirror m1;

two.cpp

#include "mirror.h"
extern Mirror m1;
Mirror m2 (&m1);

three.cpp

#include "mirror.h"
extern Mirror m2;
Mirror m3 (&m2);

and so on. Finally,

five.cpp

#include "mirror.h"

#include <iostream>

extern Mirror m4;
Mirror m5 (&m4);

int main(int argc, char* argv[]) {
  std::cout << m5.test() << std::endl;
}

m5.test() returns true. The task says, that I should change linking order, that m5.test() returns false. I have tried to use:

init_priority (priority)

In Standard C++, objects defined at namespace scope are guaranteed to be initialized in an order in strict accordance with that of their
definitions in a given translation unit. No guarantee is made for
initializations across translation units. However, GNU C++ allows
users to control the order of initialization of objects defined at
namespace scope with the init_priority attribute by specifying a
relative priority, a constant integral expression currently bounded
between 101 and 65535 inclusive. Lower numbers indicate a higher
priority.

but no luck.

Full exercise text:

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:59:07+00:00Added an answer on June 10, 2026 at 2:59 pm

    You’ll need to change the order of the object files when passing them to the linker. This works reasonable for the toplevel code although different compilers use different approaches, i.e., it isn’t portable. Also, for libraries you generally can’t control the order in which the objects are included. For example, if you have

    // file1.cpp
    int main() {
    }
    
    // file2.cpp
    #include <iostream>
    static bool value = std::cout << "file2.cpp\n";
    
    // file3.cpp
    #include <iostream>
    static bool value = std::cout << "file3.cpp\n";
    

    … and you link two programs like this:

    g++ -o tst1 file1.cpp file2.cpp file3.cpp
    g++ -o tst2 file1.cpp file3.cpp file2.cpp
    

    you will get different output for tst1 and tst2, e.g.:

    $ ./tst1
    file2.cpp
    file3.cpp
    $ ./tst2
    file3.cpp
    file2.cpp
    

    The overall moral is: don’t do it. That is: don’t use global objects. If you feel you absolutely need to use global objects, encapsulate them into functions, e.g.:

    Type& global_value() {
        static Type value; // possibly with constructor arguments
        return value;
    }
    

    This way, value is initialized the first time it is accessed and there is no way to access it while it isn’t constructed, yet. If you encapsulate all objects like this, you can guarantee that they are constructed in an appropriate order (unless you have a cyclic dependency in which case it can’t be made to work and you should seriously rethink your design). The above approach encapsulating objects into function is, unfortunately, not thread-safe in C++ 2003. It is thread-safe in C++ 2011, however. Still, use of global variable is generally problematic and you definitely want to minimize their use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Ruby right now and I got stuck, maybe you guys can help
Is there any fun, educational game (maybe a flash game, etc.) that help learning
Still learning WPF....thanks for any help. Is there any way to Refactor this: <ListBox
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I am in a learning way of mysql and want help to know where
So I'm just learning Forth and was curious if anyone could help me understand
I'm still learning how to design databases for my applications and need some help
I'm learning Java and find myself sending methods around while asking for help but
Hi thanks in advance for your help. I'm just getting started learning Python and
A friend of mine wanted help learning to program, so he gave me all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.