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. 布局神器:Flexbox

    最近的工作内容大多是移动端网页的开发,百分比布局,Media Queries,Bootstrap等常规的响应式/自适应的开发技术皆一一试过,但觉以上都不够灵活,所以,一直再尝试寻求更加灵活的精确的移动 ...

  2. 具体解说Android的图片下载框架UniversialImageLoader之磁盘缓存的扩展(二)

    相对于第一篇来讲,这里讲的是磁盘缓存的延续.在这里我们主要是关注四个类.各自是DiskLruCache.LruDiskCache.StrictLineReader以及工具类Util. 接下来逐一的对它 ...

  3. OpenWrt sscanf问题之于MT7620N与AR9341

    在MT7620N平台做好了wifidog的相关调试工作,除了eth驱动.wireless性能问题,其余的都能够基本正常. 依据实际须要要对已完毕的工作在AR9341平台上实现. 事实上也简单.基本功能 ...

  4. ubuntu16.04 server安装小记

    由于本人有一台闲置的thinkpad电脑,所以打算在上边安装一个ubuntu16.04 server版本,其中遇到主要问题,做一下记录: 安装过程中出现“ubuntu16.04 server64 bu ...

  5. [转载]C#开源项目(国外的还是很多)

    C#开源项目(国外的还是很多)一.Ajax框架Ajax.NET Professional (AjaxPro)是最先把AJAX技术在微软.NET环境下的实现的AJAX框架之一.它在客户端脚本之上创建代理 ...

  6. ASP.NET批量下载服务器端指定目录文件

    //遍历服务器指定文件夹下的所有文件 string path = "uploads/Image/"; string serverPath = Server.MapPath(path ...

  7. Activity-在ListFragment中为ListView增加空白视图

     有两种方法可以实现为ListView添加空白视图.但是原理都一样: 第一种方法是XML+代码添加: 1.定义emptyView视图 2.调用AdapterView的setEmptyView(empt ...

  8. C#创建Windows服务的几个注意事项

    1.服务安装后的自动启动:服务的StartType即使配置成Automatic,在首次安装成功之后还是要在服务列表中找到并手工启动.此外,可以通过在ProjectInstaller中添加AfterIn ...

  9. 5 HBase 常用Shell命令

    进入hbase shell console $HBASE_HOME/bin/hbase shell 如果有kerberos认证,需要事先使用相应的keytab进行一下认证(使用kinit命令),认证成 ...

  10. show,hide与fadeIn、fadeOu的区别

    show,hide——>是通过改变height,width,opacity来实现动画的显示与隐藏的 fadeIn.fadeOut——>只通过opacity来实现动画的显示与隐藏的 它们两个 ...