Question

How can the compiler determine if a constructor is a copy constructor?

How can the compiler determine if a constructor is a copy constructor?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer)

Whenever you initialize an object, C++ will make the copy by invoking the class's copy constructor, a constructor that accepts another object of the same type as a parameter.

Copy constructors in the compiler are invoked whenever:

1. A newly-created object is initialized to the value of an existing object.

Example :-

MyClass1 apple;

MyClass1 orange = apple;

MyClass1 banana = orange;

Here, since orange and banana are being initialized to the values of apple and orange, respectively, C++ will invoke the copy constructors to initialize their values. Although it looks like you're assigning values to two and three using the = operator, since they're newly-created objects, the = indicates initialization, not assignment.

In fact, the above code is equivalent to the more explicit initialization code below:

MyClass1 apple;

MyClass1 orange(apple); // Equivalent to the above code.

MyClass1 banana(orange); // Same here

2. An object is passed to a function as a non-reference parameter.

consider the following function:

void myFunction1 (MyClass1 parameter1)

{ // ... Work with parameter1 ... }

If we write MyClass1 ob1;

myFunction1(ob1);

Then the variable parameter1 inside of myFunction1 will be initialized to a copy of ob1 using the copy constructor.

3. An object is returned from a function.

MyClass1 myFunction1()

{

MyClass1 ob1;

return ob1;

}

If we call myFunction1, then C++ will create a new myClass1 object that's initialized to ob1 when myFunction1 returns. Thus while your code might act like it's transparently moving the object from inside of myFunction1 to the rest of your code, it's actually making a temporary copy.

Add a comment
Know the answer?
Add Answer to:
How can the compiler determine if a constructor is a copy constructor?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A copy constructor is used to. A/ move an object to another location. B/ copy a...

    A copy constructor is used to. A/ move an object to another location. B/ copy a field into another field. C/ create a copy of an existing object. D/ rename an existing object

  • Copy the the code below into your IDE or an online compiler and test an additional...

    Copy the the code below into your IDE or an online compiler and test an additional type with the generic class. Submit your code and execution display. IN JAVA // A Simple Java program to show working of user defined // Generic classes     // We use < > to specify Parameter type class Test<T> {     // An object of type T is declared     T obj;     Test(T obj) {  this.obj = obj;  }  // constructor     public T getObject()  { return this.obj; } } //...

  • How can a constructor be identified in a class file? The name of the constructor is...

    How can a constructor be identified in a class file? The name of the constructor is the same as the name of the class. The name of the constructor is the same as the name of the file (without .java). The constructor looks like a method, but it has no return type (not even void). A. I only B. II only C. III only D. I and III only E. I, II, and III

  • C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object...

    C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object from another of the same type. II. Copy an object to pass it as argument to a function. III. Copy an object to return it from a function. Read the following program and answer questions (20 points) #include <iostream> using namespace std; class Line public: int getLength( void); Line( int len // simple constructor Line( const Line &obj) 1/ copy constructor Line (); //...

  • Yes or No? 1) When a variable is declared in a program, the memory is allocated...

    Yes or No? 1) When a variable is declared in a program, the memory is allocated immediately for the variable? 2) If a class does not define a default constructor, the compiler will always provide a default constructor? 3) If a class does not define a copy constructor, the compiler will always provide a copy constructor? 4) A constructor can be used as a type conversion function only if it has exactly one parameter? 5) Only operators provided by C++...

  • Why are we required to provide a copy constructor and a destructor in C++ when they...

    Why are we required to provide a copy constructor and a destructor in C++ when they are not required in Java. Explain in detail.

  • There are three times when a copy constructor is called. Which of the following is NOT...

    There are three times when a copy constructor is called. Which of the following is NOT one of them.    when the right side of an assignment is an rvlaue     when an object is initialized from an object of the same class     when an object is returned using a return statement from a function     when an object is passed by value to a function

  • C++ LAB 8: More Classes Fill-in-the-Blank Questions Briefly describe what is meant by memberwise assignment _____________________________________________________________....

    C++ LAB 8: More Classes Fill-in-the-Blank Questions Briefly describe what is meant by memberwise assignment _____________________________________________________________. Describe two instances when memberwise assignment occurs. _____________________________________________________________. Describe a situation in which memberwise assignment should not be used. _____________________________________________________________. When is a copy constructor called? _____________________________________________________________. How does the compiler know that a member function is a copy constructor? _____________________________________________________________. What action is performed by a class’s default copy constructor? _____________________________________________________________.

  • 1. Using the bootstrapping concept, how do you build a F# compiler that runs on a...

    1. Using the bootstrapping concept, how do you build a F# compiler that runs on a given computer whose machine language is called M if we consider three subsets S1 and S2 and S3 of the F# language and assuming that we already build the compiler for F1 that runs on the computer whose machine language is M (assuming S1 is a subset of S2 and S2 is subset of S3). Show step by step for this process. You must...

  • Lab 2 (ADTs) 1) LockADT – Show the interface and all abstract methods LockDataStructureClass – Show the following methods: default constructor, overloaded constructor, copy constructor, setX, setY, s...

    Lab 2 (ADTs) 1) LockADT – Show the interface and all abstract methods LockDataStructureClass – Show the following methods: default constructor, overloaded constructor, copy constructor, setX, setY, setZ, alter (change the lock’s combination to the numbers passed) turn (use for loops to show the dial turning), close (locks the lock), attempt (tries to unlock the lock – calls turn( ), inquire (locked or unlocked), current (returns the number the dial is pointing to), toString LockClientDemoClass – You should have a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT