举个例子:
class Base {
Base (const Base & copyFrom) { globalRegister (* this); }
}
class Derived {
Derived (const Derived & copyFrom) : Base (copyFrom) {}
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了建议,在Baseived的初始化列表中包含Base的复制构造函数,以便复制Base的属性(如示例中所示).
但是,我有Base的复制构造函数将自身(*this)传递给其他对象(要向该对象注册).这是否真的必须在Derived的复制构造函数的初始化列表中使用(隐式或显式)Base(默认)构造函数,并且只在Derived的复制构造函数的主体中调用Base的复制构造函数,当实际存在对象时可以通过Base的复制构造函数附加吗?否则 - (*this)是一个有效的对象?
我对遗传的一些概念有疑问,我说的是我所知道的,如果我错了,请纠正我.
基类的私有成员由派生类继承,但派生类无法通过任何方式访问它们.
基类的受保护成员由派生类继承,但派生类不能直接访问它,而是通过其某些成员函数的帮助.
现在在以下代码中:
class A
{
protected:
A(const A&){}
A operator=(const A &){}
int t;
public:
A(int r) {t=r;}
A(){t=0;}
};
class B : public A
{
A a;
public:
void make(void)
{
//A b(a); //LINE 1 -------COPY CONSTRUCTOR BEING CALLED ---protected member of base class
cout<<t; //LINE 2 -------protected member of base class
}
};
int main()
{
B b;
b.make();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么LINE 1出现错误?
为什么我们不能为A的对象调用copy-constructor?
提前许多不止于此
我实现了这里描述的复制构造函数.但问题仍然是当我更新时route_copy,则应用相同的更新route.所以,我不明白我的代码有什么问题?
public class Route implements Comparable<Route> {
private List<Site> sites;
public Route()
{
sites = new ArrayList<Site>();
}
public Route(List<Site> sites)
{
this.sites = sites;
}
/**
* Copy constructor
*/
public Route(Route r) {
this(r.sites);
}
public void deleteSite(Site s) {
this.sites.remove(s);
}
}
public processData(Route route)
{
Route route_copy = new Route(route);
Site s = selectSite(route_copy);
route_copy.deleteSite(s); // !!! now 'route' does not contain an element 's'
}
Run Code Online (Sandbox Code Playgroud) class td{
int roll;
float per;
String name;
td(int r,float p,String n){
roll=r;
name=n;
per=p;
}
td(){
roll=0;
name="sachin";
per=0;
}
void get(){
try{
DataInputStream x=new DataInputStream(System.in);
roll=Integer.parseInt(x.readLine());
per=Float.parseFloat(x.readLine());
name=x.readLine();
}
catch(Exception e){
System.out.println("the error is" +e);
}
}
void disp(){
System.out.println(roll);
System.out.println(name);
System.out.println(per);
}
}
class std{
public static void main(String ar[]){
td c1 = new td();
td c2 = new td(3,5.600,"ddd");
td c3 = new td();
c1.disp();
c2.disp();
c3.disp();
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我得到了这个:
C:\j2sdk1.4.0\bin\std.java:48: cannot resolve symbol
symbol : …Run Code Online (Sandbox Code Playgroud) 我有一个实现接口形状的基类Polygon,以及另一个扩展Polygon的类Triangle,现在在Triangle复制构造函数中我需要检查给定的另一个三角形是不是空指针但是我不能这样做,因为我必须使用super()以初始化我的点数组.
这是我的代码:Polygon - 抽象类:
public abstract class Polygon implements Shape {
private Point[] points;
/**
* Build a Polygon that hold a set of Points.
*
* @param points
* (Point[])
*/
public Polygon(Point[] points) {
this.points = points;
}
Run Code Online (Sandbox Code Playgroud)
三角形子类:
public class Triangle extends Polygon {
/**
* Constructor.
* Build a Triangle from 3 Point's.
* @param p1
* @param p2
* @param p3
*/
public Triangle(Point p1, Point p2, Point p3) {
super(new Point[] { p1, p2, …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#include <iostream>
class foo_class {
std::string value;
public:
foo_class(const foo_class& v) : value{v.value} {
std::cout << "copy constructor" << std::endl;
}
foo_class(foo_class&& v) : value{std::move(v.value)} {
std::cout << "move constructor" << std::endl;
}
~foo_class() {
std::cout << "destructor" << std::endl;
}
foo_class(std::string v) : value{std::move(v)} {
std::cout << "type constructor" << std::endl;
}
};
struct Data {
foo_class a;
foo_class b;
};
int main() {
std::string c = "3";
Data x{c,c+"3"};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
重要的是,我使用GCC和Clang(分别为4.8.2和3.4)和标志-fno-elide-constructors进行编译,因此我们不会忽略复制/移动构造函数.
执行结果如下: …
在工作的时候,我遇到了一段奇怪/令人困惑的代码,我觉得这些代码与匿名 对象生命周期概念有关.以下是示例代码:
#include<iostream>
#include<string>
class A {
private:
int i;
std::string s;
public:
A(int ii, std::string ss = "Hello") { i = ii; s = ss; }
void Display() { std::cout<<i<<"\n"; }
~A() { std::cout<<"A::~A()"<<"\n";}
};
void function()
{
A a = 1;
//A a = A(1);
a.Display();
}
int main()
{
function();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
VS2010中的输出1(如果A a = 1)
1
A::~A()
Run Code Online (Sandbox Code Playgroud)
VS2010中的输出2(如果A a = A(1))
A::~A()
1
A::~A()
Run Code Online (Sandbox Code Playgroud)
当析构函数被调用两次(包括匿名)对象时,output2完全有意义.
但是输出1让我困惑,无法理解为什么析构函数 …
我正在学习Java,最近经历了Copy Constructor教程.我尝试编写Copy Constructor代码,但它会产生意外的输出.
问题是:
为什么第一个输出显示0和null值?
这是Copy Constructor的对象:
class student6 {
int id;
String name;
int i;
String n;
student6(int a, String b) {
id = a;
name = b;
}
student6(student6 s) {
i = s.id;
n = s.name;
}
void display() {
System.out.println(i + "..." + n);
}
public static void main(String args[]) {
student6 s1 = new student6(11, "Suresh");
student6 s2 = new student6(s1);
s1.display();
s2.display();
}
}
Run Code Online (Sandbox Code Playgroud)
产量
0...null
11...Suresh
我正在查看以下有关移动构造函数/赋值的示例:https: //msdn.microsoft.com/en-us/library/dd293665.aspx
我通过添加交换函数来修改它,以简化移动构造函数/赋值和复制赋值:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MemoryBlock
{
public:
// Simple constructor that initializes the resource.
explicit MemoryBlock(size_t length)
: _length(length)
, _data(new int[length])
{
std::cout << "In MemoryBlock(size_t). length = "
<< _length << "." << std::endl;
}
// Destructor.
~MemoryBlock()
{
std::cout << "In ~MemoryBlock(). length = "
<< _length << ".";
if (_data != nullptr)
{
std::cout << " Deleting resource.";
// Delete the resource.
delete[] _data;
} …Run Code Online (Sandbox Code Playgroud) 我正在使用pimpl成语const std::unique_ptr来保存类实现.我的班级需要支持复制构建和复制分配.我想要做的是手动调用impl类中的类的复制构造函数unique_ptr.但是,我没有看到如何.
#include <memory>
struct potato {
potato();
~potato();
potato(const potato& p);
private:
struct impl;
const std::unique_ptr<impl> _pimpl;
};
struct potato::impl {
int carbs = 42;
};
potato::potato()
: _pimpl(std::make_unique<impl>()) {
}
potato::~potato() = default;
potato::potato(const potato& p) {
// Try to call the copy constructor of impl, stored in unique_ptr, not the
// unique_ptr copy-constructor (which doesn't exist).
_pimpl.get()->impl(p._pimpl); // This doesn't work.
}
Run Code Online (Sandbox Code Playgroud)
我已经检查了另一个关于在对象上显式调用copy-constructor的问题.一个答案建议使用placement new.
Object dstObject;
new(&dstObject) Object(&anotherObject);
Run Code Online (Sandbox Code Playgroud)
我可以在我的拷贝构造函数中使用它吗?如果是这样,怎么样?我真的不明白那里发生了什么.谢谢.
copy-constructor ×10
c++ ×6
c++11 ×4
java ×4
oop ×2
anonymous ×1
clone ×1
constructor ×1
derived ×1
inheritance ×1
inherited ×1
null ×1
pimpl-idiom ×1
unique-ptr ×1