GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Tech FAQs  >  OOPS
Go To First  |  Previous Question  |  Next Question 
 OOPS  |  Question 253 of 255    Print  
Why the copy constructor is having a (&) variable as parameter?

  
Total Answers and Comments: 2 Last Update: January 16, 2008     Asked by: preetha varma 
  
 Sponsored Links



 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
November 28, 2007 07:39:54   #1  
vedjai Member Since: November 2007   Contribution: 1    

RE: Why the copy constructor is having a (&) variable as parameter?
the work of the copy constructor is to create a new object of the same class and copy the value of the data members from the calling object into this new object data members. When we pass some variables as input parameters, the copy constructor gets called to create local copies of the objects. If the & is ignored then the Copy Constructor will keep calling itself and will go into an endless loop. To avoid this endless loop situation, we have to pass the parameter into the copy constructor as a reference.
 
Is this answer useful? Yes | No
January 16, 2008 21:18:29   #2  
irfan.omair Member Since: January 2008   Contribution: 2    

RE: Why the copy constructor is having a (&) variable as parameter?

It can be better explained in this way also.

Thumb Rule When a copy constructor is called ? : when any object is created and it is being initialized with otherObject of same type Copy constructor  is called. Blv me this is is only one way how copy constructor is called.

Now lets understand one thing with an example .

int foo ( int age) { } and this function is called from main () { int m=10; foo(m); }
now here in function foo(int age) , the varible age is being created and initialiezed with another value which is being send from main with name "m" .
similarly in the case of copyConstructor

class A { ..... A(A&a){}..}
if A(A&a) would be  A(A a) , then here in A(A a) an object a is being created and initialiezed with anoher object . and it will again call copy constructor as per the thumb Rule above. then again same story and this will continue untill the applicationsl stack is not overflowed. and program will fianaly crash.


 
Is this answer useful? Yes | No

 Related Questions

Latest Answer : In C++, assignment and copy construction are different because the copy constructor initializes uninitialized memory, whereas assignment starts with an existing initialized object. If your class contains instances of other classes as data members, the ...

Latest Answer : In C++, variable declared as static it is shared by all the objects of the class. It can be accessed only by a static function. ...

Latest Answer : Yes . e.g. scanf ("%10s", str ); only 10 chars would be taken in ...

Which header file should you include if you are to develop a function which can accept variable number of arguments?

Can there be at least some solution to determine the number of arguments passed to a variable argument list function?

Latest Answer : A constructor is executed when an object of a class is getting initialized (constructed) ...

What is the difference between function pointer and pointer to function?why we use static variable as a global instead of defining it a local?
Read Answers (2) | Asked by : vimal gupta
Tags : Pointer

Can we call constructor of superclass directly without using super()method in subclass? if yes,explain,if no,why?

How could I, in C++ declare a class point. The class point has two private data members x and y of type float. The class point has a parameterized constructor to initialize both the data members i.e. x and y. The class point has a member function display() that display the value of x and y. Create two objects p1 and p2 with your desired data and display the values of x and y of p1 and p2. Now create a third object p3 by using the expression p3=p1+p2 and display the values of x and y of p3.
Read Answers (1) | Asked by : attique

Latest Answer : The answer is when instantiating an object compiler needs a correct object type. If the constructor is declared virtual then the the object type can not be resolved at compile time, and that's why it is not possible to declare a constructor as virtual. ...
Read Answers (4) | Asked by : gaurav


 Sponsored Links



 
Related Articles

OOPS Tutorials

Object oriented programming OOP is a computer science term used to characterize a programming language that began development in the 1960’ s The term object oriented programming’ was originally coined by Xerox PARC to designate a computer application that describes the methodol
 

What happens when a variable is not initialized in main function?

When a variable is not initialized in main function it contains garbage value. This can be well seen from the example below main() { int x; printf(“%d”,x); z= sample() } sample() { printf(“Testing program”); } Output is   &n
 

What happens when a variable is not declared in function definition?

Generally in C program the function definition and calling takes the form as given below: main() { int x,y,z; z=sample(x,y); printf(“%d”,z); } sample(x1,y1) int x1,y1; { int z1; z1= x1 - y1; return(z1); } Here what happens is the values x, y gets passed to x1,y1
 

Variable, Constants and Data types in C++

Variable Constants and Data types in C In this C tutorial you will learn about variable constants and data types in C rules for defining variable name short int int long int float double long double char bool declaring variables and constants mosgoogle Variables A variable is the storage location in
 

The Inheritance Concept In OOPs

The Inheritance Concept In OOPs In object oriented programming objects will be characterised by classes It is possible to learn a lot about an object based on the class it belongs to Even if you are not familiar with the name Maybach If I told you it is a car you would immediately know that it has f
 

Understanding The Message Concept In OOPs

Understanding The Message Concept In OOPs To understand object oriented programming you will need to become familiar with messages As the name implies a message is a process in which software objects will communicate with one another Because of this having one object is not enough mosgoogle An objec
 

Basic concepts of OOPS and Structure of C++ program

Basic concepts of OOPS and Structure of C program In this tutorial you will learn about Objects Classes Inheritance Data Abstraction Data Encapsulation Polymorphism Overloading Reusability Before starting to learn C it is essential that one must have a basic knowledge of the concepts of Object orie
 

Sponsored Links

 
Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape