How does C++ linking work in practice?

This question already has an answer here: What do linkers do? 4 answers EDIT : I have moved this answer to the duplicate: https://stackoverflow.com/a/33690144/895245 This answer focuses on address relocation , which is one of the crucial functions of linking. A minimal example will be used to clarify the concept. 0) Introduction Summary: relocation edits the .text section of object f

C ++链接如何在实践中工作?

这个问题在这里已经有了答案: 连接器有什么作用? 4个答案 编辑 :我已经将这个答案移动到重复:https://stackoverflow.com/a/33690144/895245 这个答案着重于地址重定位 ,这是链接的关键功能之一。 一个最小的例子将被用来澄清这个概念。 0)介绍 简介:重定位编辑要翻译的对象文件的.text部分: 目标文件地址 进入可执行文件的最终地址 这必须由链接器完成,因为编译器一次只能看到一个输入文件,但我们

Unsequenced value computations (a.k.a sequence points)

Sorry for opening this topic again, but thinking about this topic itself has started giving me an Undefined Behavior. Want to move into the zone of well-defined behavior. Given int i = 0; int v[10]; i = ++i; //Expr1 i = i++; //Expr2 ++ ++i; //Expr3 i = v[i++]; //Expr4 I think of the above expressions (in that order) as operator=(i, operator++(i)) ; //Expr1 equivalent operat

非序列值计算(aka序列点)

对不起再次打开这个话题,但想到这个话题本身已经开始给我一个未定义的行为。 想要进入明确定义的行为区域。 特定 int i = 0; int v[10]; i = ++i; //Expr1 i = i++; //Expr2 ++ ++i; //Expr3 i = v[i++]; //Expr4 我认为上述表达式(按此顺序)为 operator=(i, operator++(i)) ; //Expr1 equivalent operator=(i, operator++(i, 0)) ; //Expr2 equivalent operator++(operator++(i)) ; //Expr3 equi

Multiple preincrement operations on a variable in C++(C ?)

Why does the following compile in C++? int phew = 53; ++++++++++phew ; The same code fails in C, why? That is because in C++ pre-increment operator returns an lvalue and it requires its operand to be an lvalue . ++++++++++phew ; in interpreted as ++(++(++(++(++phew)))) However your code invokes Undefined Behaviour because you are trying to modify the value of phew more than once between t

对C ++中的变量进行多次预增量操作(C?)

为什么在C ++中编译? int phew = 53; ++++++++++phew ; 为什么C代码失败? 这是因为在C++ pre-increment操作符返回一个lvalue并且它的操作数是一个lvalue 。 ++++++++++phew ; 在解释为++(++(++(++(++phew)))) 但是,您的代码会调用Undefined Behaviour因为您试图在两个序列点之间多次修改phew的值。 在C ,预增值运算符返回一个rvalue并要求其操作数为lvalue 。 所以你的代码不能在C模式下编译。 注意:两个缺陷

What belongs in an educational tool to demonstrate the unwarranted assumptions people make in C/C++?

I'd like to prepare a little educational tool for SO which should help beginners (and intermediate) programmers to recognize and challenge their unwarranted assumptions in C, C++ and their platforms. Examples: "integers wrap around" "everyone has ASCII" "I can store a function pointer in a void*" I figured that a small test program could be run on vario

什么属于教育工具来演示人们在C / C ++中做出的无理假设?

我想为SO编写一个小小的教育工具,它可以帮助初学者(和中级)程序员认识并挑战他们在C,C ++及其平台上的无理假设。 例子: “整数环绕” “每个人都有ASCII” “我可以将函数指针存储在void *中” 我想,一个小的测试程序可以在各种平台上,运行于“似是而非”的假设它们是,从我们的经验,所以,平时很多没有经验/ semiexperienced主流开发商提出并记录他们在不同的机器坏了的方式运行。 这样做的目的不是证明它是“安全”

Undefined behavior and sequence points reloaded

Consider this topic a sequel of the following topic: Previous installment Undefined behavior and sequence points Let's revisit this funny and convoluted expression (the italicized phrases are taken from the above topic *smile* ): i += ++i; We say this invokes undefined-behavior. I presume that when say this, we implicitly assume that type of i is one of the built-in types. What if

未定义的行为和序列点重新加载

请考虑以下主题的续集: 前一期 未定义的行为和顺序点 让我们重温这个有趣而复杂的表达方式(斜体短语取自上述主题* smile *): i += ++i; 我们说这调用了未定义的行为。 我假设说这个时,我们隐含地认为这种类型的i是内置类型之一。 如果i的类型是用户定义类型会怎么样? 说它的类型是在这篇文章后面定义的Index (见下文)。 它会不会调用未定义的行为? 如果是,为什么? 它不等同于编写i.operator+=(i.oper

Undefined, unspecified and implementation

C和C ++中未定义,未指定和实现定义的行为之间有什么区别? Undefined behavior is one of those aspects of the C and C++ language that can be surprising to programmers coming from other languages (other languages try to hide it better). Basically, it is possible to write C++ programs that do not behave in a predictable way, even though many C++ compilers will not report any errors in the program!

未定义,未指定和实现

C和C ++中未定义,未指定和实现定义的行为之间有什么区别? 未定义的行为是C和C ++语言的其中一个方面,对于来自其他语言的程序员来说可能会令人惊讶(其他语言试图更好地隐藏它)。 基本上,即使许多C ++编译器不会在程序中报告任何错误,也可以编写不可预测的C ++程序! 我们来看一个典型的例子: #include <iostream> int main() { char* p = "hello!n"; // yes I know, deprecated conversion p[0] = '

What does a colon following a C++ constructor name do?

This question already has an answer here: What is this weird colon-member (“ : ”) syntax in the constructor? 12 answers This is an initialization list , and is part of the constructor's implementation. The constructor's signature is: MyClass(); This means that the constructor can be called with no parameters. This makes it a default constructor, ie, one which will be called by d

C ++构造函数名称后面的冒号是做什么的?

这个问题在这里已经有了答案: 构造函数中这个奇怪的冒号成员(“:”)语法是什么? 12个答案 这是一个初始化列表 ,是构造函数实现的一部分。 构造函数的签名是: MyClass(); 这意味着可以不带参数调用构造函数。 这使得它成为默认的构造函数,也就是说,当你编写MyClass someObject;时候默认调用它MyClass someObject; 。 部分: m_classID(-1), m_userdata(0)被称为初始化列表 。 这是一种用你选择的值初始化你的

c++

So, after watching this wonderful lecture on rvalue references, I thought that every class would benefit of such a "move constructor", template<class T> MyClass(T&& other) edit and of course a "move assignment operator", template<class T> MyClass& operator=(T&& other) as Philipp points out in his answer, if it has dynamically allocated members,

C ++

因此,观看右值引用这个美好的演讲结束后,我认为每一个类将有利于这样的“移动构造”的, template<class T> MyClass(T&& other) 的编辑 ,当然是一个“移动赋值运算符”, template<class T> MyClass& operator=(T&& other)正如Philipp在他的回答中指出的那样,如果它有动态分配的成员,或者通常存储指针。 就像你应该有一个copy-ctor,赋值运算符和析构函数,如果前面提到的点适用。 思考?

Operator overloading "float" and "<<"

This question already has an answer here: What are the basic rules and idioms for operator overloading? 7 answers Your output operator<< should accept the stream as the first argument, and the fraction as the second argument. Right now, it does the opposite (the fraction is the first argument, and the stream is the second). This means the operator isn't called, instead the fracti

运算符重载“float”和“<<”

这个问题在这里已经有了答案: 运算符重载的基本规则和习惯用法是什么? 7个答案 您的输出operator<<应接受流作为第一个参数,并将分数作为第二个参数。 现在,它做了相反的事情(分数是第一个参数,流是第二个参数)。 这意味着操作员没有被调用,而是将分数转换为float ,然后显示浮点数。 你写的操作符可以用a << cout来调用,这显然是错误的。 您不应将其作为成员运算符,而应该将此运算符作为(朋友

Operator overloading inside vs outside class

This question already has an answer here: Operator overloading outside class [duplicate] 3 answers What are the basic rules and idioms for operator overloading? 7 answers First consider this example using your class (reformatted to a style I prefer): class A { public: auto operator==( A const* p ) -> bool { return true; // Whatever. } }; auto main()

内部操作符与外部类重载

这个问题在这里已经有了答案: 运算符在类外部重载[重复] 3个答案 运算符重载的基本规则和习惯用法是什么? 7个答案 首先考虑使用你的类的这个例子(重新格式化我喜欢的风格): class A { public: auto operator==( A const* p ) -> bool { return true; // Whatever. } }; auto main() -> int { A u{}, v{}; A const c{}; bool const r1 = (u == &v);