标签: assignment-operator

作业有两个等号

以下代码我们总是使用,没关系,

while(int c=getchar() != 'q');
Run Code Online (Sandbox Code Playgroud)

但如果将int更改为如下所示的指针,则编译将出现错误.

#include <stdio.h>
int main(){
  int* c=0;
  if(int* a =c !=0)
    printf("ok");
}

error: cannot convert `bool' to `int*' in assignment
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?似乎优先事项已经改变.任何人都可以给我一个提示.如果我改变它,它会工作.

#include <stdio.h>
int main(){
  int* c=0;
  int* a;
  if((a =c) !=0)
    printf("ok");
}
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors assignment-operator

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

重载C++赋值运算符

我想扩展std::string一些功能,所以我从中推导String出来.为了使代码像String str = stdStr;工作一样,我试图重载赋值运算符,但我的代码由于某种原因没有被调用.我该如何解决?

#include <string>

class String
    :
        public std::string
{

    public:
        /*
        I do know that this constructor will solve the problem, but is it possible to use the operator?

        String ( const std::string& stdString )
        {

            ...

        }
        */

        String& operator= ( const std::string& stdString )
        {
            ...
            return *this;
        }

};

int main()
{

    std::string foo = "foo";
    String bar = foo;

    return 1;

}
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-overloading variable-assignment assignment-operator

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

为什么RVO不会发生在赋值运算符中?(C++)

例:

A myfunction() { return A(); }
A a = myfunction(); // default ctor only (return value optimization)
a = myfunction(); // default ctor and operator=
Run Code Online (Sandbox Code Playgroud)

为什么编译器不能将新对象写入现有对象?我相信一个类的所有实例占用相同数量的(非动态)内存,所以我不明白为什么这会是一个问题.

c++ assignment-operator rvo

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

为什么在java中不允许赋值和没有括号的布尔运算符

对不起,对于奇怪的问题表述.如果有人知道如何让它变得更好,我会很高兴.以为我们有3个布尔变量:

boolean a = false;
boolean b = false;
boolean c = false;
Run Code Online (Sandbox Code Playgroud)

Java允许编写以下内容:

System.out.println(a=b);
System.out.println(a==b & a==c);
Run Code Online (Sandbox Code Playgroud)

从这两个陈述中我预计以下内容也是合法的.

System.out.println(a=b & a=c);
Run Code Online (Sandbox Code Playgroud)

我的问题是:为什么在第二种情况下不允许,在第一种情况下允许它?在第二种情况下,两个赋值都以布尔值解析,而且对我来说看起来合法.

java variable-assignment boolean-expression assignment-operator assign

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

在数组上调用赋值运算符

我想知道这段代码是如何工作的:

struct my_array
{
    int r[1000];
};

int main()
{
    my_array foo, bar;
    foo = bar;
}
Run Code Online (Sandbox Code Playgroud)

因为foo = bar调用将调用operator=为类提供的构造函数,它将懒惰地将它应用于每个成员.但是数组没有实现operator=,证明是,这段代码无法编译:

int main()
{
    int a[1000], b[1000];
    a = b;
}
Run Code Online (Sandbox Code Playgroud)

那我的第一个代码怎么编译呢?

c++ arrays assignment-operator

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

为什么在赋值发生时调用了Paramaterized构造函数?

我的问题是最后的陈述,即之前的陈述 return 0;

当我们尝试将int值分配给对象时,为什么要调用参数化构造函数.

我的代码:

#include<iostream>
using namespace std;
class Test {
private:
    int i;
    public:
    Test(int s=0):i(s) {
            cout<<"param ctor: "<<i<<endl;
    }
};

int main()
{
    Test a;         //param ctor called
    Test b(5);      //param ctor called
    //b(a);         //error as we have not implemented copy ctor
    b=a;            //compiler provided assignment opr. called
    b=100;          //why param ctor called for this.??
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

  param ctor: 0
  param ctor: 5
  param ctor: 100
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor assignment-operator

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

将一个字符数组分配给另一个给出错误.为什么?

数组的名称是指向第一个元素的指针.那么为什么一个字符数组不能分配另一个数组呢?

#include<stdio.h>
int main()
{
  char str1[]="Hello";
  char str2[10];
  char *s="Good Morning";
  char *q;
  str2=str1;  /* Error */
  q=s; /* Works */
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c arrays string pointers assignment-operator

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

在c中使用malloc进行多个一行分配

假设以下声明:

int *numbers, *inverse;
numbers = inverse = (int *)malloc(n * sizeof(int));
Run Code Online (Sandbox Code Playgroud)

我很想知道这里发生了什么 - 我知道它是正确的,所以首先inverse要分配内存.然后我设置numbers等于inverse,这是否意味着内存位置numbers将是相同的inverse?或者它的位置分配相同的内存量&numbers&inverse

例如,如果我做的inverse[i] = 5意思是numbers[i] == 5什么意思?

c memory variable-assignment assignment-operator

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

编译器报告'已删除'operator =,但它就在那里

我遇到了一个讨厌的问题,编译器声称它operator=被删除了,但它就在那里.经过几个小时的尝试,我制作了一个最小的解决方案来重现这个问题.我正在使用MSVC Community Edition 2017 15.7.5(截至今天的最新版本,2018-07-20),并将其设置为'C++ 17'

代码不是微不足道的; 概念是模板类TT用于强制foo在一组类中存在静态成员函数Fn.这与工厂模式非常相似,只是此解决方案不会创建类实例,而是报告有关类的静态详细信息.

在最后一行的赋值中报告错误,并读取(底部的完整错误列表):

"错误C2280:'C&C :: operator =(const C&)':尝试引用已删除的函数"

但第5行定义了这个运算符,右边是那些装饰器?

失败的赋值尝试将返回的值分配给const std::vector<C>&类成员变量.
我认为这const是产生问题,但删除const每个函数中的所有函数没有任何区别; 同一行中的错误相同.

问题:为什么编译器报告这个,以及可能的修复方法是什么?
我认为这一定是我想念的傻事,但我找不到它.

#include <vector>

class C
{
public:
  C(int ii) : i(ii) {}
  C& operator=(const C&) = default;    /// HERE is the assignment operator
  const int i;
};
typedef std::vector<C> CVec;   // shorthand for a vector of C's

template <class T>   // this template forces classes F1, …
Run Code Online (Sandbox Code Playgroud)

c++ templates factory-method assignment-operator c++17

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

如何解决“ *或-&gt;运算符必须应用于指针”的问题?

我正在使用Visual Studio 2019研究C#中的赋值运算符,但由于错误代码CS0193,该脚本未运行。

首先,像{c = + a}一样,更改了“ =”等号后的“ +”加号的位置,但仅适用于add和sub。我想使用{c * = a}但出现以下错误。

            int a = 21;

            int c =+ a;

            Console.WriteLine("1. Value of c is {0}", c );

            Console.WriteLine("2. Binery of c is {0}", Convert.ToString(c, 2));

            Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

根据我的书很好,它应该像(x-= y),(x + = y),(x * = y)一样工作...但是在Visual Studio 2019中,它的工作原理像(x =-y),(x = + y ),错误,错误…

错误代码:CS0193

说明:*或->运算符必须应用于指针。

c# assignment-operator visual-studio-2019

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