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. Jquery让图片根据浏览器窗口大小自动缩放

    (function($){ $.fn.resizeimage = function(){ var imgLoad = function (url, callback) { var img = new ...

  2. 栈和托管堆/值类型和引用类型/强制类型转换/装箱和拆箱[C#]

    原文地址:http://www.cnblogs.com/xy8.cn/articles/1227228.html 一.栈和托管堆      通用类型系统(CTS)区分两种基本类型:值类型和引用类型.它 ...

  3. 好用的QT连接

    QT属性控件项目https://github.com/lexxmark/QtnProperty比特币交易软件https://github.com/JulyIGHOR/QtBitcoinTrader导航 ...

  4. Go程序GC优化经验分享

    http://1234n.com/?post/yzsrwa 最近一段时间对<仙侠道>的服务端进行了一系列针对GC的调优,这里跟各位分享一下调优的经验. 游戏第一次上线的时候,大部分精力都投 ...

  5. [Redux] Extracting Presentational Components -- TodoApp

    Finally, I just noticed that the to-do app component doesn't actually have to be a class. I can turn ...

  6. python之面向对象(一)

    python编程分为三个阶段: 面向过程编程:根据业务逻辑从上到下垒 函数式编程:将某功能进行函数封装,使用时调用函数即可,减少代码重复量 面向对象编程:对函数进行分类和封装 理论上我们是比较鄙视面向 ...

  7. .net学习体会

    莫名其妙学了IT,在课堂上学了C,C++,自学了C#,也做了一些网站项目,学习过程,写了厚厚的几本笔记本,却没写博文的习惯,前几天,有同学问学习.net的建议.其实我懂的也不多,也给了一些个人见解,主 ...

  8. 22个CSS黑魔法

    原链接:http://www.ido321.com/1665.html Hey there! Today we are going to talk about some useful tricks i ...

  9. encodeURI与encodeURIComponent的区别

    webservice输出时选择的格式与Content-Type报文头有关 encodeURI与encodeURIComponent的区别:后者会将URI进行编码(包括"://")

  10. utf-8的mysql表笔记

    链接数据库指定编码集jdbc:mysql://192.168.2.33:3306/mybase?useUnicode=true&characterEncoding=UTF-8 mysql默认链 ...