C/C++编程笔记:高级C++知识 |虚拟副本构造器

在虚拟构造函数中,我们看到了构造对象的方法,该对象的类型直到运行时才确定。是否可以在不知道其确切类类型的情况下创建对象?该虚拟拷贝构造函数解决这个问题。

有时我们可能需要从另一个现有对象构造一个对象。精确地,复制构造函数执行相同的操作。新对象的初始状态将基于另一个现有对象状态。从另一个对象实例化一个对象时,编译器将调用复制构造函数。但是,编译器需要具体的类型信息来调用适当的副本构造函数。

#include <iostream>

using namespace std;

class Base

{

public:

//

};

class Derived : public Base

{

public:

Derived()

{

cout << "Derived created" << endl;

}

Derived(const Derived &rhs)

{

cout << "Derived created by deep copy" << endl;

}

~Derived()

{

cout << "Derived destroyed" << endl;

}

};

int main()

{

Derived s1;

Derived s2 = s1; // Compiler invokes "copy constructor"

// Type of s1 and s2 are concrete to compiler

// How can we create Derived1 or Derived2 object

// from pointer (reference) to Base class pointing Derived object?

return 0;

}

如果我们不能决定要从哪种对象类型进行复制,该怎么办?例如,虚拟构造函数根据某些输入在运行时创建类层次结构的对象。当我们想从虚拟构造函数创建的另一个对象中复制构造一个对象时,我们不能使用通常的复制构造函数。我们需要一个特殊的克隆函数,该函数可以在运行时复制对象。

例如,考虑一个绘图应用程序。您可以选择一个已经在画布上绘制的对象,然后粘贴同一对象的另一个实例。从程序员的角度来看,我们无法确定哪个对象将被复制粘贴,因为它是运行时决策。我们需要虚拟副本构造函数来提供帮助。

同样,考虑剪贴板应用。剪贴板可以容纳不同类型的对象,并从现有对象复制对象,然后将其粘贴到应用程序画布上。同样,要复制的对象类型是运行时决策。虚拟副本构造函数填补了这里的空白。请参见下面的示例:

#include <iostream>

using namespace std;

 LIBRARY SRART

class Base

{

public:

Base() { }

virtual // Ensures to invoke actual object destructor

~Base() { }

virtual void ChangeAttributes() = 0;

// The "Virtual Constructor"

static Base *Create(int id);

// The "Virtual Copy Constructor"

virtual Base *Clone() = 0;

};

class Derived1 : public Base

{

public:

Derived1()

{

cout << "Derived1 created" << endl;

}

Derived1(const Derived1& rhs)

{

cout << "Derived1 created by deep copy" << endl;

}

~Derived1()

{

cout << "~Derived1 destroyed" << endl;

}

void ChangeAttributes()

{

cout << "Derived1 Attributes Changed" << endl;

}

Base *Clone()

{

return new Derived1(*this);

}

};

class Derived2 : public Base

{

public:

Derived2()

{

cout << "Derived2 created" << endl;

}

Derived2(const Derived2& rhs)

{

cout << "Derived2 created by deep copy" << endl;

}

~Derived2()

{

cout << "~Derived2 destroyed" << endl;

}

void ChangeAttributes()

{

cout << "Derived2 Attributes Changed" << endl;

}

Base *Clone()

{

return new Derived2(*this);

}

};

class Derived3 : public Base

{

public:

Derived3()

{

cout << "Derived3 created" << endl;

}

Derived3(const Derived3& rhs)

{

cout << "Derived3 created by deep copy" << endl;

}

~Derived3()

{

cout << "~Derived3 destroyed" << endl;

}

void ChangeAttributes()

{

cout << "Derived3 Attributes Changed" << endl;

}

Base *Clone()

{

return new Derived3(*this);

}

};

// We can also declare "Create" outside Base.

// But is more relevant to limit it's scope to Base

Base *Base::Create(int id)

{

// Just expand the if-else ladder, if new Derived class is created

// User need not be recompiled to create newly added class objects

if( id == 1 )

{

return new Derived1;

}

else if( id == 2 )

{

return new Derived2;

}

else

{

return new Derived3;

}

}

 LIBRARY END

 UTILITY SRART

class User

{

public:

User() : pBase(0)

{

// Creates any object of Base heirarchey at runtime

int input;

cout << "Enter ID (1, 2 or 3): ";

cin >> input;

while( (input != 1) && (input != 2) && (input != 3) )

{

cout << "Enter ID (1, 2 or 3 only): ";

cin >> input;

}

// Create objects via the "Virtual Constructor"

pBase = Base::Create(input);

}

~User()

{

if( pBase )

{

delete pBase;

pBase = 0;

}

}

void Action()

{

// Duplicate current object

Base *pNewBase = pBase->Clone();

// Change its attributes

pNewBase->ChangeAttributes();

// Dispose the created object

delete pNewBase;

}

private:

Base *pBase;

};

 UTILITY END

 Consumer of User (UTILITY) class

int main()

{

User *user = new User();

user->Action();

delete user;

}

用户类借助虚拟构造函数创建对象。要创建的对象基于用户输入。Action() 正在复制正在创建的对象并修改其属性。借助Clone()虚拟函数创建的重复对象,该虚拟函数也被视为虚拟副本构造函数Clone()方法背后的概念是原型模式的基础

以上就是今天的全部内容了。每日分享小知识,希望对你有帮助~

另外如果你想更好的提升你的编程能力,学好C语言C++编程!弯道超车,快人一步!笔者这里或许可以帮到你~

C语言C++编程学习交流圈子,QQ群:765803539点击进入】微信公众号:C语言编程学习基地

分享(源码、项目实战视频、项目笔记,基础入门教程)

欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!

编程学习视频分享:

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值