在我阅读的书中看到了这个例子,它对我来说根本没有意义,我可能错过了一些东西,但似乎你用值'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++的新手,我正在用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) 请考虑以下代码:
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) 我以为语法:
var a, b, c = {};
Run Code Online (Sandbox Code Playgroud)
意味着这三个变量是分开的,而不是对同一个{}的引用.
这是因为{}是一个对象,这是标准行为吗?
所以,如果我这样做:
var a, b, c = 0;
Run Code Online (Sandbox Code Playgroud)
三者确实是分开的而不是参考?
谢谢,韦斯利
标题几乎说明了一切,我几乎肯定它是复制构造函数或赋值运算符,我很确定它是后者.这是一个非常短的课程,所以我会发布整个事情,任何关于如何处理它的建议都会很好.老实说我在这里也有点过头了,所以任何指向一些可靠阅读的人都会非常感激.
#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) 我有以下代码片段:
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)
我不知道我理解的第一个值怎么printf来0.当值k与之比较时35,理想情况下它应该返回(并因此打印)1,但它如何打印为零?产生的其他两个值 - 50并且0都是正确的,因为在第二个值中,k的值被视为50,而对于第三个值,k的值(即35)被比较40.因为35 < 40,所以它打印0.
任何帮助将不胜感激,谢谢.
**更新**
在研究了关于这个主题的更多内容之后undefined behavior,我在一本关于C的书中看到了这一点,最后给出了源代码.
调用约定 调用约定表示在遇到函数调用时参数被传递给函数的顺序.这里有两种可能性:
C语言遵循第二顺序.
考虑以下函数调用:
fun (a, b, c, d ) ;
Run Code Online (Sandbox Code Playgroud)
在这个调用中,参数是从左向右还是从右向左传递并不重要.但是,在某些函数调用中,传递参数的顺序成为一个重要的考虑因素.例如:
int a = 1 ;
printf …Run Code Online (Sandbox Code Playgroud) 这是代码
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*1是1,为什么?使用赋值运算符或修改内部值会printf()导致undefined behaviour?
我有点困惑,因为我确信这应该有所不同.看看这个代码示例:
#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#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开发),所以虽然很容易理解一些东西,但这样的东西让我感到困惑.
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.
c++ ×4
c ×3
printf ×2
.net ×1
boost ×1
c# ×1
deep-copy ×1
equality ×1
gcc ×1
java-8 ×1
javascript ×1
memory-leaks ×1
object ×1
objective-c ×1
polymorphism ×1
reference ×1
shared-ptr ×1
typechecking ×1