Reference: TutorialPoints, GeekforGeeks

The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to:

  • Initialize one object from another of the same type.

  • Copy an object to pass it as an argument to a function.

  • Copy an object to return it from a function.

In C++, if a copy constructor is not defined in a class, the compiler itself defines one.

Java also supports copy constructor. But, unlike C++, Java doesn’t create a default copy constructor if you don’t write your own.

 class Complex {

     private double re, im;

     // A normal parametrized constructor
public Complex(double re, double im) {
this.re = re;
this.im = im;
} // copy constructor
Complex(Complex c) {
System.out.println("Copy constructor called");
re = c.re;
im = c.im;
} // Overriding the toString of Object class
@Override
public String toString() {
return "(" + re + " + " + im + "i)";
}
} public class Main { public static void main(String[] args) {
Complex c1 = new Complex(10, 15); // Following involves a copy constructor call
Complex c2 = new Complex(c1); // Note that following doesn't involve a copy constructor call as
// non-primitive variables are just references.
Complex c3 = c2; System.out.println(c2); // toString() of c2 is called here
}
}

Output:

Copy constructor

called (10.0 + 15.0i)

 class Complex {

     private double re, im;

     public Complex(double re, double im) {
this.re = re;
this.im = im;
}
} public class Main { public static void main(String[] args) {
Complex c1 = new Complex(10, 15);
Complex c2 = new Complex(c1); // compiler error here
}
}

Compile error

Copy Constructor in Java的更多相关文章

  1. C++-copy constructor、copy-assignment operator、destructor

    本文由@呆代待殆原创,转载请注明出处. 对于一个类来说,我们把copy constructor.copy-assignment operator.move constructor.move-assig ...

  2. 构造函数语义学之Copy Constructor构建操作(2)

    二.详述条件 3 和 4 那么好,我又要问大家了,条件1 和 2比较容易理解.因为member object或 base class 含有copy constructor.那么member objec ...

  3. 构造函数语义学之Copy Constructor构建操作(1)

    一.Copy Constructor的构建操作 就像 default constructor 一样,如果class没有申明一个 copy constructor,就会隐含的声明或隐含的定义一个.生成的 ...

  4. no copy constructor available or copy constructor is declared 'explicit'

    今天新写了一个类.然后对这个类使用STL中的vector,碰到错误: no copy constructor available or copy constructor is declared 'ex ...

  5. Copy Constructor的构造操作

    Copy Constructor的构造操作 有三种情况,会以一个object的内容作为另一个class object的初值: 1.  对一个object做显式的初始化操作 class X{…}; X ...

  6. Effective C++ 第0章 copy constructor和copy assignment operator

    拷贝构造函数(copy constructor)被用来以一个对象来初始化同类型的另一个对象,拷贝赋值运算符(copy assignment operator)被用来将一个对象中的值拷贝到同类型的另一个 ...

  7. copy constructor

    copy constructor也分为trivial和nontrivial两种 如果class展现出bitwise copy semantics(按位拷贝语义),则不会构造出 copy constru ...

  8. c++官方文档-copy constructor

    #include <iostream> using namespace std; class Example5 { string* ptr; public: Example5(const ...

  9. (C++)关于拷贝构造函数 Copy Constructor

    题目: In which of the following scenarios is a Copy Constructor called or invoked? A.    When no conve ...

随机推荐

  1. android面试题之六

    三十一.AIDL的全称是什么?如何工作?能处理哪些类型的数据? 英文全称:Android Interface Define Language(Android接口定义语言). 当A进程要去调用B进程中的 ...

  2. java 获取系统变量(环境变量和设置变量)

    前言 环境变量这个概念不陌生, 就是操作系统的环境变量. 系统变量就是java本身维护的变量. 通过 System.getProperty 的方式获取. 对于不同的操作系统来说, 环境变量的处理可能会 ...

  3. lucene 使用教程

    原文转自:http://cloudera.iteye.com/blog/656459 1 lucene简介  1.1 什么是lucene  Lucene是一个全文搜索框架,而不是应用产品.因此它并不像 ...

  4. loadrunner java 缺少必要的导入包报错

    loadrunner 运行从eclipse中做好的脚本,ctrl + A 复制到loadrunner中来, 添加参数化的的语句:verifyCode =    lr.eval_string (&quo ...

  5. maven install 报错Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin

    Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of ...

  6. MyBatis配置解析

    MyBatis配置文件解析(概要) 1.configuration:根元素 1.1 properties:定义配置外在化 1.2 settings:一些全局性的配置 1.3 typeAliases:为 ...

  7. web.xml配置DispatcherServlet

    1. org.springframework.web.servlet.DispatcherServlet 所在jar包: <dependency> <groupId>org.s ...

  8. jQuery----blur()方法

    当元素失去焦点时发生 blur 事件. blur() 函数触发 blur 事件,或者如果设置了 function 参数,该函数也可规定当发生 blur 事件时执行的代码. 提示:早前,blur 事件仅 ...

  9. Arcgis API for Android之GPS定位

    欢迎大家增加Arcgis API for Android的QQ交流群:337469080 先说说写这篇文章的原因吧,在群内讨论的过程中,有人提到了定位的问题,刚好,自己曾经在做相关工作的时候做过相关的 ...

  10. c++读取ccbi

    loader类文件: 需要定义CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(ButtonTestLayerLoader, loader); 这个宏定义是定义静态的l ...