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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:40:27+00:00 2026-05-26T07:40:27+00:00

This is almost a duplicate question, but I really didn’t understand the answer given

  • 0

This is almost a duplicate question, but I really didn’t understand the answer given for the other one, so I am going to try again:

I am learning C++ and I an trying to understand the various options for creating and using constructors. So my first question is what is the difference between these two object creations:

 class Example{
      Example(int x){myX = x} ;
      private:
         int myX;
 }

Then In my main method:

 Example example1 = new Example(5);
 Example example2 = Example(5);
 Example example3(5);

I know that using new will give me a dynamically allocated object, that I will later need to delete. And that the example2 will be allocated on the stack and shouldn’t need to be deleted. But I don’t really understand when, or why, to use the constructor style of example3. any help involving minimal jargon would be very greatly appreciated because thats why I can’t seem to understand this elsewhere. Thanks very much in advance for any light you guys might be able to shed on this for me.

  • 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-05-26T07:40:28+00:00Added an answer on May 26, 2026 at 7:40 am

    The two declarations

    Example example2 = Example(5);
    Example example3(5);
    

    are equivalent. Although the first one looks like it might create an object and then invoke the copy constructor, most compilers will simply create the example2 object in place.

    The decision about when to choose which of the above styles to use is largely a matter of taste.

    Here’s a complete example program to demonstrate:

    #include <iostream>
    
    using namespace std;
    
    class Test {
    public:
        Test(int x): X(x) {
            cout << "constructor " << X << endl;
        }
        Test(const Test &rhs): X(rhs.X) {
            cout << "copy " << X << endl;
        }
        Test &operator=(const Test &rhs) {
            X = rhs.X;
            cout << "assign " << X << endl;
            return *this;
        }
    private:
        int X;
    };
    
    int main()
    {
        Test t1 = Test(1);
        Test t2(2);
        t2 = t1;
    }
    

    and the output (gcc 4.2.1, OS X Lion):

    constructor 1
    constructor 2
    assign 1
    

    Notice how the assignment operator is called only for t2 = t1 (as expected), but the copy constructor is not called at all. (However, as Dennis Zickefoose notes in the comments, a copy constructor must be accessible. Try making the copy constructor private in the above example, and the compiler should refuse to compile it.)

    EDIT: Note that gcc actually has an option that controls this behaviour:

        -fno-elide-constructors
            The C++ standard allows an implementation to omit creating a
            temporary which is only used to initialize another object of the
            same type.  Specifying this option disables that optimization, and
            forces G++ to call the copy constructor in all cases.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies, this is almost certainly a duplicate of this question but as that one
This is almost similar question to this one: - Dealing with timezones in PHP
This is almost certainly a very silly question, but for some reason I'm having
So this is almost a duplicate of this question , except that I do
I'm sorry if it's a duplicate of some question but this is specific on
I try to get this code running. I am almost there but I got
I apologize if this question is a duplicate. I have read many other mysqli
Sorry, this is almost certainly a duplicate, but I've been searching StackOverflow and haven't
Duplicate post, see: When do you use the "this" keyword? On almost every project
Possible Duplicate: Why there are no ++ and — operators in Python? This question

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.