English 中文(简体)
是否能够“处理”违约复印件的构件?
原标题:Is it possible to "add" to the default copy constructor?

Is it possible to "add" to the default copy constructor?

Eg. For this class:

class A
{
    public:
        int a;
        int* b;
};

I want to just write

A::A(const A& rvalue):
    a(rvalue.a),
    b(new int(*(rvalue.b)))
{}

<代码>a(r Value.a) part。

(Ignore the bad/ugly code and possible memory leaks)

最佳回答

What you ask for is impossible. Once you declare your own copy constructor the compiler will not generate a copy constructor for you. That means you won t be able to simply add or augment the default copy constructor because it won t exist. It s all or nothing, so to speak.

问题回答

it is impossible. However, if you want to reduce redundant code for a large number of "default copied" fields, it may be achieved with intermdiate inheritance:

struct A1  {
  int a1;
  int a2;
  // ....
  int aN;
};

struct A:public A1
{
  int* b;
  A(const A& rhs):  A1(rhs), b(new int(*(rhs.b))) {}
};

你们想要做的事情并不得到C++的全力支持。 你们不可能有半个违约建筑。

但是,你想要实现的目标可以通过以下一点来完成:

please note this small demo below have a lot defects(memory leak etc), so it s ONLY for demostration the tentative solution only:

//class A hasa large number date members(but all can take advantage of default 
//copy constructor
struct A{
    A(int i):a(i){}
    int a;
    //much more data memberS can use default copy constructor all in class A
};

//class B is simply wrapper for class A 
//so class B can use the default constructor of A
//while just write copy constructor for a raw pointer in it s copy constructor
//I think this is what OP want ?
struct B
{
    B(int i,int j):m_a(i),m_b(new int(j)){}

    B(const B & rval):
    m_a(rval.m_a),
    m_b(new int(*rval.m_b))
    {
    }
    A     m_a;
    int * m_b;
};

int main()
{
    B c(2,3); // a=2, *m_b=3
    B d(c);   //after copy constructor, a=2, *m_b=3
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签