标签: assignment-operator

为什么这个向量分配不起作用?

类似问题:


#include <vector>
#include <iostream>

using namespace std;

int main() {
  vector<vector<int> > vvi;
  vvi.resize(1);
  vvi[0].reserve(1);
  vvi[0][0] = 1;

  vector<int> vi = vvi[0];

  cout << vi[0]; // cout << vvi[0][0]; works

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这给了我一个段错误,我不知道为什么.

c++ stdvector assignment-operator copy-assignment

1
推荐指数
1
解决办法
1475
查看次数

c ++中的运算符重载如何工作

我正在阅读一些c ++的赋值运算符理论.

让我们说吧

class MyClass {
private:
   T1 member1;
   T2 member2;
public:
   // The default copy assignment operator which assigns an object via memberwise copy
   MyClass & operator=(const MyClass & rhs) {
      member1 = rhs.member1;
      member2 = rhs.member2;
      return *this;
   }
......
}
Run Code Online (Sandbox Code Playgroud)

c7 = c6; //member wise copy assignment.
Run Code Online (Sandbox Code Playgroud)

这里我们在赋值操作期间返回对象的引用,并为其指定新对象c7.

但是,如果我的代码有点像这样:

    int a=12;
    int &b=a;
    int c=&b;        //error::invalid conversion from ‘int*’ to ‘int’
Run Code Online (Sandbox Code Playgroud)

为什么这与上述情况不同?

c++ assignment-operator

1
推荐指数
1
解决办法
77
查看次数

为什么在仍然通过时需要返回*this?

我写了下面的类,它重载了赋值运算符.如示例所示,我*this从赋值运算符返回.

class Sample
{
    int *p;
    int q;

public:

    Sample()
    {
        cout<<"Constructor called"<<endl;

        p = new int;
        q = 0;
    }


    Sample& Sample::operator =(const Sample &rhs)
    {
        cout<<"Assignment Operator"<<endl;

        if(this != &rhs)
        {
            delete p;

            p = new int;
            *p = *(rhs.p);
        }

        return *this;
    }

    void display()
    {

        cout<<"p = "<<p<<"     q = "<<q<<endl;
    }
};
Run Code Online (Sandbox Code Playgroud)

当我调用赋值运算符时a = b,就像,a.operator=(b);.

现在我正在调用一个运算符函数,这已经被传递,operator =然后为什么需要从赋值运算符返回它?

c++ class operator-overloading assignment-operator

1
推荐指数
1
解决办法
147
查看次数

为什么这个赋值操作导致函数调用不明确?

考虑以下程序编译并运行正常:

#include <iostream>
#include <string>
using std::string;
struct BB
{
 // generic cast
 template<typename T>
 operator T() const
 {   
   std::cout<<"Generic cast\n";
   return 0;
 }
 // string cast
 operator string() const
 { 
   std::cout<<"string cast\n";
   return string("hello");
 }
};
int main()
{
  BB b;
  string s = b; // copy constructor
}
Run Code Online (Sandbox Code Playgroud)

但如果我稍微更改main()函数的代码如下:

int main()
{
  BB b;
  string s;
  s = b;
} 
Run Code Online (Sandbox Code Playgroud)

编译器给出以下错误消息(请参阅此处的实时演示)

[Error] ambiguous overload for 'operator=' (operand types are 'std::string {aka std::basic_string<char>}' and 'BB') …
Run Code Online (Sandbox Code Playgroud)

c++ function-call ambiguous-call assignment-operator

1
推荐指数
1
解决办法
282
查看次数

如何声明与已知常量字符串长度相同的新字符串?

我一直在使用:

string letters = THESAMELENGTH; // Assign for allocation purposes.
Run Code Online (Sandbox Code Playgroud)

原因是,如果我:

string letters[THESAMELENGTH.length()];
Run Code Online (Sandbox Code Playgroud)

我收到一个非常量表达式投诉。

但如果我:

string letters[12];
Run Code Online (Sandbox Code Playgroud)

如果指南 const 字符串更改大小,我将面临需要更改每个实例的风险。

但是当我不使用这些条目时分配一个字符串似乎很愚蠢,我只希望我新分配的字符串与之前分配的 const 字符串长度相同,然后填充不同的值。

您如何建议我优雅且安全地执行此操作?

c++ string assignment-operator

1
推荐指数
2
解决办法
1万
查看次数

C++ 中的 a = 5、a(5)、a{5} 和 a[5] 有什么区别?

是什么的语句之间的差别a = 5a(5)a{5}以及a[5]在C ++?

我偶尔会看到它们用于诸如在 C++ 程序中为对象赋值之类的事情。它们之间的区别在哪里?

c++ syntax initialization variable-assignment assignment-operator

1
推荐指数
1
解决办法
242
查看次数

在使用 C++ 赋值时理解运算符“更少”或“更大”

我使用了大于和小于符号,它给出了输出!它是如何工作的?

int x = 2;
x >= 3;
cout << x;  // output is 2
Run Code Online (Sandbox Code Playgroud)

而且输出也是不同的

int x = 2;
x = x > 3;
cout << x;   // output is zero !! HOW ??
Run Code Online (Sandbox Code Playgroud)

c++ bitwise-operators assignment-operator comparison-operators relational-operators

1
推荐指数
1
解决办法
70
查看次数

当我们为类的对象分配整数值时,为什么会调用参数化构造函数?

代码是:

#include<iostream>
using namespace std;

class Integer
{
    int num;

    public:
        Integer()
        {
            num = 0;
            cout<<"1";
        }
        
        Integer(int arg)
        {
            cout<<"2";
            num = arg;
        }
        int getValue()
        {
            cout<<"3";
            return num;
        }

};

int main()
{
    Integer i;
    i = 10;  // calls parameterized constructor why??
    cout<<i.getValue();
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,语句i=10调用了参数化构造函数。你能解释一下吗。

c++ constructor assignment-operator parameterized-constructor

1
推荐指数
1
解决办法
88
查看次数

这个逻辑是如何工作的: k+=(x=5,y=x+2,z=x+y) ;

这个逻辑是如何工作的: k+=(x=5,y=x+2,z=x+y);

它将如何给出结果 k == 22。当我初始化 k = 10 的值时

#include <stdio.h>
int main()
{
    int x,y,z,k=10;
    k+=(x=5,y=x+2,z=x+y);
    printf("value of k %d ",k); //why it will show value of k =22
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c comma-operator assignment-operator

1
推荐指数
1
解决办法
101
查看次数

赋值运算符和 c

#include <stdio.h>

int main()
{
   int i,j;
   i=j=(22,23,24);
   printf("i:%d",i);
   printf("\nj:%d",j);
}
Run Code Online (Sandbox Code Playgroud)

这将 i,j 的输出都设为 24。

#include <stdio.h>

int main()
{
   int i,j;
   i=j=22,23,24;
   printf("i:%d",i);
   printf("\nj:%d",j);
}
Run Code Online (Sandbox Code Playgroud)

这使 i,j 都为 22。有人可以解释背后的术语吗。TIA

c assignment-operator

1
推荐指数
1
解决办法
49
查看次数