C++ 内联函数设置为等于一个数字
我有两个文件。一个是测试文件,一个头文件上有链表类的声明。这个类有一些内联方法。这个密码不是我的 我不明白的是测试文件中有一行写着C++ 内联函数设置为等于一个数字,c++,list,linked-list,inline,C++,List,Linked List,Inline,我有两个文件。一个是测试文件,一个头文件上有链表类的声明。这个类有一些内联方法。这个密码不是我的 我不明白的是测试文件中有一行写着x.info()=1515。我们怎么能打这样的电话?如果我能够访问这个方法的定义,我就可以弄清楚这一点,但这里的情况并非如此。如果有人能帮助我了解这里发生的事情,我将不胜感激 代码如下: listint.hpp #ifndef _LISTEINT_H_ #define _LISTEINT_H_ #include <exception> #include
x.info()=1515代码>。我们怎么能打这样的电话?如果我能够访问这个方法的定义,我就可以弄清楚这一点,但这里的情况并非如此。如果有人能帮助我了解这里发生的事情,我将不胜感激
代码如下:
listint.hpp
#ifndef _LISTEINT_H_
#define _LISTEINT_H_
#include <exception>
#include <stdexcept>
class ListeInt {
public:
inline constexpr bool vide () const noexcept;
inline int info () const;
inline const ListeInt& suite () const;
}; // ListeInt
#include <ostream>
std::ostream& operator<< (std::ostream&, const ListeInt&);
#endif // _LISTEINT_H_
#include "ListeInt.hpp"
#include <utility> // swap, move, forward...
#include <exception>
#include <iostream> // cin, cout, clog, cerr...
int main () {
std::cout << std::boolalpha << "<<<" << std::endl;
{
std::cout << "***" << std::endl;
int i = 42;
ListeInt x;
std::cout << "sizeof(x) = " << sizeof(x)
<< " cf. " << sizeof(nullptr) << std::endl;
std::cout << "x = " << x << std::endl;
std::cout << "x.vide = " << x.vide() << std::endl;
x = ListeInt(i);
std::cout << "x = " << x << std::endl;
std::cout << "x.vide = " << x.vide() << std::endl;
std::cout << "x.info = " << x.info() << std::endl;
std::cout << "x.suite = " << x.suite() << std::endl;
x.info() = 1515;
std::cout << "x = " << x << std::endl;
std::cout << "x.info = " << x.info() << std::endl;
}
std::cout << ">>>" << std::endl;
return 0;
}
\ifndef\u listint\u H_
#定义_listin_H_
#包括
#包括
类列表{
公众:
内联constexpr bool vide()const noexcept;
内联int info()常量;
内联常量listint&suite()常量;
}; // 李斯特
#包括
std::ostream&operator如果代码根本没有编译(由于几个原因,它没有编译),则语句x.info()=1515代码>真的什么都做不了
调用x.info()。当<代码>代码>已到达。x
的状态将不受此分配的影响
我之所以说“会”,是因为该语句是代码不能正确编译的原因之一:
error: lvalue required as left operand of assignment
x.info() = 1515;
^~~~
错误:赋值的左操作数需要左值
x、 info()=1515;
^~~~
如果x.info()。但事实并非如此
因此,要么编写此代码的人不知道他们在做什么,要么您使用了错误版本的listint.hpp
x.info()=1515
ifinfo
确实返回一个int
。您确定它不会返回int&
?您使用的编译器及其版本是什么?代码无论如何都不会编译listint()
没有将int
作为输入的构造函数,因此x=listint(i)代码>也无效。但不允许您分配给临时,因此代码甚至不应编译。clang-cl给出错误:表达式不正确assignable@NathanOliver在这种情况下可能是这样,但并非所有情况下都是这样。例如: