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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:55:06+00:00 2026-06-09T11:55:06+00:00

Consider the belo code snippet: class Base { public: Base() { cout<<Constructor<<endl; } Base(const

  • 0

Consider the belo code snippet:

class Base
{
    public:
    Base()
    {
            cout<<"Constructor"<<endl;
    }
    Base(const Base& rhs)
    {
            cout<<"Copy constructor"<<endl;
    }
    Base* Clone()
    {
            return new Base(*this); <--------------- 2
    }
    void* operator new(size_t size)
    {
            void* p=malloc(size);
            cout<<"Inside new"<<endl;
            return p;
    }
};

int main()
{
    Base b1; <------------ 1
    Base* ptr=b1.Clone();
    return 0;
}

I am getting the output as:

Constructor
Inside new
Copy constructor  

I kept hearing that first operator new allocates a chunk of void type & then new operator calls constructor to convert that chunk into the exact type as that on the LHS.
So, why the constructor is not getting called for statement 2?

I also want to know the exact series of actions undertaken by the C++ compiler for the statement 2.

  • 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-09T11:55:07+00:00Added an answer on June 9, 2026 at 11:55 am

    So, why the constructor is not getting called for statement 2?

    It is. Where do you think "Copy constructor" comes from?

    Base b1;
    

    Output: Constructor

    Base* ptr=b1.Clone();
    

    calls

    new Base(*this);
    

    which in turn calls your operator new and then your copy constructor.

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

Sidebar

Related Questions

Consider the following code : template<typename T> class Base { Base(); Base(const Base<T>& rhs);
Consider the following code, public class StartUp { public StartUp(String[] test){} public static void
Consider these classes. class Base { ... }; class Derived : public Base {
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following code: public interface A { public A another(); } public interface
Consider the following snippet of code: foreach (var setting in RequiredSettings) { try {
Consider the following situation: public class Storage { public T GetSingleByID<T>(long id) where T
Consider: Public MustInherit Class Column Public ReadOnly Property ReturnSomethingUseful() As Object Get 'return something
Consider the code below. I do not want to create multiple instances of class
Consider the following class: public class Score { private static readonly Guid _relationId =

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.