English 中文(简体)
C++版图
原标题:Stucture in C++

I encountered this code but I could not understand the functionality of this code. It would be a great help if someone could explain it .

struct A{
   int i,j;
   A(int ii,int jj) : i(ii),j(ii){}

   A(const A&a){
           }
   A& operator =(const A& a){
               i=a.i;j=a.j;
   }
};

int main()
{
int i;
A a(1,2);
A b(2,3);
A z = (a=b);
cout<<z.i<<" "<<z.j<<endl;

system("pause");
return 0;
}
问题回答

解释:

struct A{
   int i,j;//members i and j

   A(int ii,int jj) : i(ii),j(ii){} //A constructor. Short form of A(int ii,int jj){i = ii;j = jj;} Original code is wrong too. Should be j(jj) instead of j(ii)

   A(const A&a){}//Another constructor. It is missing the assignment

   A& operator =(const A& a){
               i=a.i;j=a.j;
   }//Equal operator overload definition for A = another A. It copies the data from another A and assign to this new one
};

完整的工作法典:

#include <iostream>
using namespace std;

struct A{
   int i,j;

   A(int ii,int jj) : i(ii),j(jj){}

   A(const A&a){i=a.i;j=a.j;}

   A& operator =(const A& a){i=a.i;j=a.j;}
};

int main()
{
    int i;
    A a(1,2);
    A b(2,3);
    A z = (a=b);
    cout<<z.i<<" "<<z.j<<endl;

    return 0;
}

你的问题是:

A z = (a=b);

它最后援引了您的<代码>operator=方法和您的制版主。 <代码>a = b 执行时,使用<代码>operator= 方法:a 现在已经存在,随后还提到了<代码>a。 页: 1

A z = ......执行时,它实际上使用了制版指南,而不是operator=。 由于<代码>z尚未存在。 由于z是由该复印机和ij创建的,在你试图将其印出时,你在为<代码>i和保留的记忆中找到了任何座标。

看待此行的另一个方式:

A z = (a=b);

确实如此:

A z(a.operator=(b));

这方面的一个充分例子:

int main()
{
    A a(1,2);
    A b(2,3);

    a = b; //calls A& operator=(const A& a)

    A z = a; //calls A(const A& a)
}

最后,规定要做到这一点:

A(const A& a)
   {
        i = a.i;
        j = a.j;
   }

三个错误:

1.A(int ii,int jj) : i(ii),j(ii)/* jj here? 页: 1

2. 结 论 影印机应首先对成员进行:A(const A&a): i(a.i), j(a.j){}

3.You should Add return *this in operator=:

A& operator =(const A& a){
    i=a.i;j=a.j;
    return *this;
}

The OP has asked the operator overloadin part. if we take a as const how can we edit it.

运营商超负荷部分是:

A& operator =(const A& a){
           i=a.i;j=a.j;

iii

当你写<条码>a=b时,你只希望<条码>a更改而不是<条码>。 这一点在职能论证定义中由最有投机者执行。 这种投机者与平等标志左侧出现的任何事情毫无关系。 只是说平等标志右侧不会改变。





相关问题
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?

热门标签