Copy Constructor Call When Function Returns a Reference to Class Object – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#7473    siteicon   siteicon  

Copy Constructor Call When Function Returns a Reference to Class Object

Understanding copy constructor call when a function is returning reference to a class object.

Learning Objectives

  • Viewing how copy constructor is called when a function returns a reference to an object.

Source Code

TC++ #7473

 

Source Code

Run Output

10
20

Code Understanding

int x,y;  //Global variables declared so that this demo allows them to use from different functions.
class C { //Class C declared
public:
C(){ x=10;}  //Default Constructor sets x as 10.
C(C& D) { x=20;} //Copy Constructor sets x as 20.
};

C E;               //An object E of class type C has been declared. This has been declared globally so that the returned reference can be used by other functions or objects.

C& func() {//This functions is returning a reference to Class type C
return E;      //A reference to object E is returned.
}

int main(){
C c1;    //c1 is an object based on C, so just default constructor would be called.
cout<<x<<endl; //This prints 10 as default constructor has set x as 10.
C c2=func();    //Here another object c2 is being made based on object reference returned by function func()
cout<<x<<endl; //This would print 20 as copy constructor has now been called.
return 0;}

Notes

  • Copying copy constructor can be called in many other situations like
    – Creating copy of an object.
    – When Object is called by value to a function.
    – When Object is returned from a function.
    – When temporary objects are created.

Common Errors

  • If function doesn’t return object by reference like this C func() then compilation error would occur.
  • If the the object  C E; is declared inside the function the returned reference may not be available to other functions and error/warning may be returned by the compiler.


Suggested Filename(s): objreturnedasreference.cpp,obretref.cpp



Share

CSKC| Created: 26-Apr-2019 | Updated: 26-Apr-2019|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back