answersLogoWhite

0


Best Answer

A copy constructor gets called any time an object needs to be copied. Unlike in some of the newer languages like Java, you can chose to pass objects either by reference or by value. When you pass by reference, only the address of the function is copied. However, if you pass by value, the whole object must be copied. In order to copy the object, the copy constructor will get called.

If the copy constructor's parameter is not a reference, then the object will get passed by value. When the object gets passed by value, it needs to get copied so it will call the same copy constructor. Because the object is still being passed by value it'll call itself again to create a copy of itself. The copy constructor will continue to call itself until the stack overflows.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why not sending arefrence to copy constructor will cause an infinite loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are multiple constructors in c?

A constructor is a special method that is created when the object is created or defined. This particular method holds the same name as that of the object and it initializes the instance of the object whenever that object is created. The constructor also usually holds the initializations of the different declared member variables of its object. Unlike some of the other methods, the constructor does not return a value, not even void.When you create an object, if you do not declare a constructor, the compiler would create one for your program; this is useful because it lets all other objects and functions of the program know that this object exists. This compiler created constructor is called the default constructor. If you want to declare your own constructor, simply add a method with the same name as the object in the public section of the object. When you declare an instance of an object, whether you use that object or not, a constructor for the object is created and signals itself.A constructor is declared without a return value, that also excludes void.Therefore, when implemented, do not return a value:Constructor Exampleclass rectangle { // A simple class int height; int width; public: rectangle(void); // with a constuctor, ~rectangle(void); // and a destructor }; rectangle::rectangle(void) // constuctor { height = 6; width = 6; } sagar sainath samant. sybsc (computer science)


How do you fix an infinite loop?

It comes from its name: it doesn't terminate, the user have to interrupt the program-run (in the worst case: power off the computer).The infinite loop is also used to program loops with non-easily-deterministically end-of-loop conditions.You write an infinite loop, such as for (;;) {statements}, and break out of the loop with the break statement when ready to terminate.


What is a bit copy?

A bit copy of an object is an exact, bit-by-bit, copy of that object. The default copy constructor generated by the compiler makes a bit copy. This is potentially a problem if the object contains pointers to other objects... A bit copy of a pointer copies the pointer, but not its data. This means that you have two pointers pointing at the same object in memory. If you delete one of them, the other becomes invalid, and this can (usually does) cause corruption. If an object contains a pointer, the object's copy constructor should provide for proper allocation and copying of any pointed to objects within that object.


How are an objects instance variables initialized of a class has only a default constructor?

You have to explicitly initialise all instance variables, otherwise their values will be initialised to whatever happens to be in memory at the time they are instantiated. Consider the following class which has no explicit initialisation: #include <iostream> class object { public: object(){} public: const int getData( void ) const { return( m_data ); } private: int m_data; }; int main() { object O; printf( "O.m_data = %d\n", O.getData() ); return( 0 ); } Example output: O.m_data = -858993460 This is not particularly useful; we must ensure m_data is initialised before we access its value. One way to initialise object::m_data is from within the body of the constructor: object(){ m_data = 0; } // inefficient initialisation However this is quite inefficient because object::m_data will have already be instantiated by the time we got to the constructor body. This is akin to the following C-style initialisation: int x; // instantiate x x = 0; // initialise x When what we really want to do is: int x = 0; Or use the more formal construction semantics: int x(0); Both achieve the same thing. They both initialise the variable x at the point of instantiation, not after instantiation. Fortunately, C++ allows us to initialise all instance variables at the point of instantiation, via the constructor's initialisation section: object():m_data(0){} // efficient initialisation While initialising a single primitive data type in the constructor body isn't going to cause major problems in terms of efficiency, with more complex data types the inefficiencies can very quickly add up. Thus it is important to use the initialisation section as much as possible and to only use the body of the constructor when there is no option. The initialisation section is particularly important when dealing with derived classes. Consider the following: #include <iostream> class base { public: base():m_int(0){} base(const base& object):m_int(object.m_int){} public: const int getInt( void ) const { return( m_int ); } void setInt( const int data ) { m_int = data; } private: int m_int; }; class derived : public base { public: derived():m_float(0.0){} derived(const derived& object):m_float(object.m_float){} public: const float getFloat( void ) const { return( m_float ); } void setFloat( const float data ) { m_float = data; } private: float m_float; }; int main() { derived d; d.setInt( 1 ); d.setFloat( 2.0 ); printf( "d.getInt() = %d, d.getFloat() = %f\n", d.getInt(), d.getFloat() ); derived c(d); // call copy constructor. printf( "c.getInt() = %d, c.getFloat() = %f\n", c.getInt(), c.getFloat() ); return( 0 ); } Example output: d.getInt() = 1, d.getFloat() = 2.000000 c.getInt() = 0, c.getFloat() = 2.000000 Note that c should be an exact copy of d, but there's clearly a difference in the integer variables inherited from the base class. This is because the derived class copy constructor called the base class default constructor, not the base class copy constructor as you might have expected. To resolve this we must explicitly call the base class copy constructor and the only way to do so is via the initialisation section of the derived class: derived(const derived& object):base(object),m_float(object.m_float){} Note that the derived class' copy constructor now includes a call to base(object) in the initialisation section. This ensures the base class copy constructor is called. Although object is actually an instance of derived, because it derived from base it is also a kind of base, thus the call is legitimate. Now if we run the code we'll get the expected result: d.getInt() = 1, d.getFloat() = 2.000000 c.getInt() = 1, c.getFloat() = 2.000000 As your classes become more and more complex you will inevitably find yourself creating overloaded constructors to provide a wide variety of initialisation methods. But keep in mind that it costs absolutely nothing to use the initialisation sections even if several constructors end up using the exact same initialisations. And while it is tempting to move all of the common initialisation code into a private method of the class and have each constructor call that method (known as a construction helper function), this simply adds yet more inefficiency by introducing yet another unnecessary function call. Helper functions such as these are undoubtedly useful during the initial development of a class but as soon as the class is finalised, get rid of the helper function and move all that functionality into the initialisation sections where they belong. In the case of derived classes, base class constructors will be called whether you like it or not, so it makes sense to call the most appropriate base class constructor from within the initialisation section of each of your derived class constructors. Remember that if you do not specify a base class constructor, its default constructor will be called implicitly, which may or may not be the most appropriate constructor in all cases (not least in the case of the copy constructor).


Will maximum current flow through 0 ohms of resistance?

In theory, you would get infinite current. But, of course, in practise, this cannot happen, as any large current would cause severe damage to the source supplying that current, not to mention the conductors involved. Protection against such currents would be provided by a fuse or circuit breaker.