标签: assignment-operator

这是错的还是我错过了什么(int count = 10,x;)

在我阅读的书中看到了这个例子,它对我来说根本没有意义,我可能错过了一些东西,但似乎你用值'10'分配计数,然后是值'x',这是不均匀的一个int.只是想知道这是否是有效的语法.

这本书说:

变量count和x以正常方式声明为整数变量.在下一行,变量intPtr被声明为"指向int的指针".请注意,这两行声明可以组合成一行:

int count = 10, x, *intPtr;
Run Code Online (Sandbox Code Playgroud)

这是程序取自:

#import <Foundation/Foundation.h>
int main (int argc, char *argv[ ])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    int count = 10, x;

    int *intPtr;

    intPtr = &count;

    x = *intPtr;

    NSLog (@"count = %i, x = %i", count, x);

    [pool drain];

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

c objective-c variable-assignment assignment-operator

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

编译时进行C++类型检查

所有.我是C++的新手,我正在用C++编写一个小型库(主要用于我自己的项目).在设计类型层次结构的过程中,我遇到了定义赋值运算符的问题.

我采用了本文最终达到的基本方法,即对于MyClass从类派生的层次结构中的每个类,Base您定义了两个赋值运算符,如下所示:

class MyClass: public Base {
public:
    MyClass& operator =(MyClass const& rhs);
    virtual MyClass& operator =(Base const& rhs);
};

// automatically gets defined, so we make it call the virtual function below
MyClass& MyClass::operator =(MyClass const& rhs);
{
    return (*this = static_cast<Base const&>(rhs));
}

MyClass& MyClass::operator =(Base const& rhs);
{
    assert(typeid(rhs) == typeid(*this)); // assigning to different types is a logical error
    MyClass const& casted_rhs = dynamic_cast<MyClass const&>(rhs);
    try {
        // allocate new …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism boost typechecking assignment-operator

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

C++赋值运算符解析

请考虑以下代码:

struct A
{
    void foo( const char * ) { cout << __PRETTY_FUNCTION__ << endl; }
    A & operator = ( const A &  ) { cout << __PRETTY_FUNCTION__ << endl; return * this; }
};


struct B : public A
{
    void foo( const char * ) { cout << __PRETTY_FUNCTION__ << endl; }
    A & operator = ( const A & other ) { cout << __PRETTY_FUNCTION__ << endl; return * this; }
};
Run Code Online (Sandbox Code Playgroud)

然后我们称这个成员为:

B b; …
Run Code Online (Sandbox Code Playgroud)

c++ assignment-operator member-hiding

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

var a,b,c = {}

我以为语法:

var a, b, c = {};
Run Code Online (Sandbox Code Playgroud)

意味着这三个变量是分开的,而不是对同一个{}的引用.

这是因为{}是一个对象,这是标准行为吗?

所以,如果我这样做:

var a, b, c = 0;
Run Code Online (Sandbox Code Playgroud)

三者确实是分开的而不是参考?

谢谢,韦斯利

javascript reference object assignment-operator

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

在共享指针中捕获内存泄漏?

标题几乎说明了一切,我几乎肯定它是复制构造函数或赋值运算符,我很确定它是后者.这是一个非常短的课程,所以我会发布整个事情,任何关于如何处理它的建议都会很好.老实说我在这里也有点过头了,所以任何指向一些可靠阅读的人都会非常感激.

#pragma once

//for non-learning purposes, boost has a good smart pointer
template <class type>
class sPtr
{
private:
    type *p;
    int r; //referenceCount

    void add()
    {
        r++;
    }
    int release()
    {
        return --r;
    }
public:
    sPtr(): p(NULL), r(1) {}
    sPtr(type *pValue): p(pValue)
    {
        add();
    }
    sPtr(const sPtr<type> & sp): p(sp.p), r(sp.r)
    {
        add();
    }
    ~sPtr()
    {
        if(release() == 0)
        {
            delete p;
        }
    }

    type* get()
    {
        return p;
    }

    type& operator*()
    {
        return *p;
    }
    type* operator->()
    { …
Run Code Online (Sandbox Code Playgroud)

c++ memory-leaks shared-ptr assignment-operator

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

如何在printf占位符中使用等于表达式?

我有以下代码片段:

main( )
{
int k = 35 ;
printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ;
}
Run Code Online (Sandbox Code Playgroud)

产生以下输出

0 50 0
Run Code Online (Sandbox Code Playgroud)

我不知道我理解的第一个值怎么printf0.当值k与之比较时35,理想情况下它应该返回(并因此打印)1,但它如何打印为零?产生的其他两个值 - 50并且0都是正确的,因为在第二个值中,k的值被视为50,而对于第三个值,k的值(即35)被比较40.因为35 < 40,所以它打印0.

任何帮助将不胜感激,谢谢.

**更新**

在研究了关于这个主题的更多内容之后undefined behavior,我在一本关于C的书中看到了这一点,最后给出了源代码.

调用约定 调用约定表示在遇到函数调用时参数被传递给函数的顺序.这里有两种可能性:

  1. 参数可能从左向右传递.
  2. 参数可能从右到左传递.

C语言遵循第二顺序.

考虑以下函数调用:

fun (a, b, c, d ) ;
Run Code Online (Sandbox Code Playgroud)

在这个调用中,参数是从左向右还是从右向左传递并不重要.但是,在某些函数调用中,传递参数的顺序成为一个重要的考虑因素.例如:

int a = 1 ;
printf …
Run Code Online (Sandbox Code Playgroud)

c printf equality undefined-behavior assignment-operator

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

printf()中赋值运算符的问题

这是代码

int main()
{
  int x=15;
  printf("%d %d %d %d",x=1,x<20,x*1,x>10);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是 1 1 1 1

我期待1 1 15 1作为输出,

x*1等于,15但这里x*11,为什么?使用赋值运算符或修改内部值会printf()导致undefined behaviour

c printf gcc assignment-operator

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

在分配新内容之前未删除对象

我有点困惑,因为我确信这应该有所不同.看看这个代码示例:

#include <iostream>
#include <string>

using namespace std;

class base
{
    public:
    virtual ~base() = default;
};

class derived : public base
{
    private:
    int a = 0;
    int *b = nullptr;
    std::string lol;

    public:
    derived(std::string s) : b(new int(6)), lol{s} { cout << "ctor " << lol << endl; }
    derived(derived const& d) : lol{d.lol + " copy"} {cout << "copy " << lol << endl; }

    virtual ~derived() { cout << "dtor " << lol << endl; delete …
Run Code Online (Sandbox Code Playgroud)

c++ deep-copy copy-constructor assignment-operator

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

为什么"=>"赋值在这种情况下有效而不是"="?

我在我的工作中继承了一个C#MVC Web应用程序,并且在控制器类中直接有一个如下所示的赋值:

public class FooController : Controller
{
    private IAuthenticationManager AuthenticationManager => HttpContext.GetOwinContext().Authentication;
}
Run Code Online (Sandbox Code Playgroud)

Visual Studio突出显示错误,类似于"; expected".但它编译并运行得很好.如果我将"=>"更改为一个简单的赋值"=",它会突出显示HttpContext,并显示错误"非静态字段bla bla bla需要一个对象引用..."它将无法编译.

所以这是我的问题.为什么使用"=>"运算符编译并正常工作?我是C#的新手(来自Android/iOS开发),所以虽然很容易理解一些东西,但这样的东西让我感到困惑.

.net c# assignment-operator

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

如何在java 8中评估第2行?

int f = 1;
f = f++;
System.out.println(f);
Run Code Online (Sandbox Code Playgroud)

post increment operator的优先级高于赋值运算符,所以我想f的值(即1)用于赋值,f增加,然后输出为2(因为f的值现在为2),但输出为1 ,但是怎么样?我哪里错了?

我的解释在下面的代码中得到了正确的答案

int f = 1;
int g = f++;
System.out.println(f);
Run Code Online (Sandbox Code Playgroud)

在这种情况下输出为2.

assignment-operator post-increment java-8

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