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

  • Home
  • SEARCH
  • 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 9248919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:03:58+00:00 2026-06-18T10:03:58+00:00

I’m having trouble understanding what’s really happening with the code in a book I’m

  • 0

I’m having trouble understanding what’s really happening with the code in a book I’m using to learn C++. Here’s the code:

class Base
{
    public:
        Base() {};
        virtual ~Base() {};

        virtual Base* Clone() {return new Base(*this);}
};

class Derived
{
    public:
        Derived() {};
        virtual ~Derived() {};

        virtual Base* Clone() {return new Derived(*this);}
};

So in this Clone() function I understand that the function returns a pointer to a Base class object. What I don’t understand is what’s happening within that function. When I’ve previously used new as in int *pInt = new int, I was under the impression that new essentially allocates enough memory on the free store for an integer, then returns that address, applying the address to the pointer pInt. With that same logic I’m trying to understand the new Derived(*this) portion of the code. So, I think it is allocating enough memory on the free store for a Derived class object, and returning the address, which is then returned by the function Clone().

Why, though, does it pass *this through the constructor, if that is a constructor? I understand *this means its passing the address of whatever object is being cloned, but I don’t understand the syntax of class_name(address_of_an_object) in the context of the new function.

Could someone please explain what’s happening in that portion?

Thanks in advance.

  • 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-18T10:03:59+00:00Added an answer on June 18, 2026 at 10:03 am

    The misunderstanding is here:

    *this means its passing the address of whatever object is being cloned

    In reality, this is the address of the object that is being cloned, but *this (note the asterisk) is the result of dereferencing that address. So *this is of type Derived &, it’s a reference to the object being cloned, not its address.

    Therefore, calling new Derived(*this) means that after dynamically allocating space (which is what new does), the new space is initialised by the copy constructor Derived(const Derived &), which in this case hasn’t actually been user-defined, so the (compiler-generated) default version of the copy constructor is used.


    To clarify the semantics of new: If C is a class, then

    new C;
    

    allocates enough space for an object of type C and then calls the constructor of C to initialise that space. This is part of the semantics of new: It always calls the constructor to initialise the newly allocated space.

    When you call

    new C(a,b,c);
    

    with some arguments a, b and c, then new will call a constructor of C that takes these three arguments. If no such constructor has been defined, you’ll get a compiler error.

    Now in the special case where you call

    new C(a);
    

    with an argument a that is itself of type C&, new will, as always, call the appropriate constructor. The appropriate constructor is either C(C &) (if defined), or C(const C&) (copy-constructor auto-defined by the compiler).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am reading a book about Javascript and jQuery and using one of the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a

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.