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 8715841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:56:34+00:00 2026-06-13T05:56:34+00:00

Learning C++ with help of Bruce Eckel Thinking in C++. Stuck in exercise 30.

  • 0

Learning C++ with help of Bruce Eckel “Thinking in C++”. Stuck in exercise 30. Here it is:

If function calls to an object passed by value weren’t early-bound, a
virtual call might access parts that didn’t exist. Is this possible?
Write some code to force a virtual call, and see if this causes a
crash. To explain the behavior, examine what happens when you pass an
object by value.

I can understand result of calling virtual function for object, but I can not understand how to force compiler to do it, without proper constructors called.

Is there a way to treat one object as another without calling proper constructors or operators (for type conversion)?

  • 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-13T05:56:35+00:00Added an answer on June 13, 2026 at 5:56 am

    Bruce is trying to illustrate object slicing, a situation when a polymorphic object is passed by value.

    Here is how you can do it:

    #include <iostream>
    using namespace std;
    struct hello {
        virtual void say() { cout << "hello" << endl; }
    };
    struct world : public hello {
        virtual void say() { cout << "world" << endl; }
    };
    void force(hello h) {
        h.say();
    }
    int main() {
        world w;
        w.say();
        force(w);
        return 0;
    }
    

    This code outputs (link to ideone)

    world
    hello
    

    even though you’d expect an object of type world to “say” world, not hello. C++ compiler is smart in noticing that w is passed to hello by value, so it adjusts the vtable to avoid calls of methods in the derived class.


    Bonus exercise to test if you understand passing by reference: can you modify my code so that it prints world world? You are allowed to insert a single character.

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

Sidebar

Related Questions

Learning C++ with help of "Thinking in C++" by Bruce Eckel,stuck in exercise 32,
I'm learning C++ right now using Bruce Eckel's Thinking in C++ and I'm in
I'm learning Ruby right now and I got stuck, maybe you guys can help
I'm coding a small function to help with learning how to use python and
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

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.