Does std::list::remove method call destructor of each removed element?

std::list<Node *> lst; //.... Node * node = /* get from somewhere pointer on my node */; lst.remove(node); Does std::list::remove method call destructor(and free memory) of each removed element? If yes, how I can avoid it? Yes, removing a Foo* from a container destroys the Foo* , but it will not release the Foo . Destroying a raw pointer is always a no-op. It cannot be any other way!

std :: list :: remove方法调用每个被删除元素的析构函数吗?

std::list<Node *> lst; //.... Node * node = /* get from somewhere pointer on my node */; lst.remove(node); std :: list :: remove方法调用每个被删除元素的析构函数(和可用内存)吗? 如果是,我该如何避免它? 是的,从容器中移除Foo*破坏了Foo* ,但它不会释放Foo 。 销毁原始指针始终是无操作的。 它不能以任何其他方式! 让我给你几个原因。 存储类 只有在指针被实际动态分配的时候删除一个指针才有

Understanding the meaning of the term and the concept

Could you C++ developers please give us a good description of what RAII is, why it is important, and whether or not it might have any relevance to other languages? I do know a little bit. I believe it stands for "Resource Acquisition is Initialization". However, that name doesn't jive with my (possibly incorrect) understanding of what RAII is: I get the impression that RAII is a

了解术语和概念的含义

您能否让C ++开发人员详细描述RAII是什么,为什么它很重要,以及它是否与其他语言有任何关联? 我确实知道一点。 我相信它代表“资源获取是初始化”。 然而,这个名字并不与我对RAII的理解(可能是不正确的)有所联系:我认为RAI​​I是一种在堆栈上初始化对象的方式,以便当这些变量超出范围时,析构函数会自动被称为导致资源被清理。 那么,为什么不叫“使用栈来触发清理”(UTSTTC :)呢? 你如何从那里到达“RAII”? 你怎么

When do we have to use copy constructors?

I know that C++ compiler creates a copy constructor for a class. In which case do we have to write a user-defined copy constructor? Can you give some examples? The copy constructor generated by the compiler does member-wise copying. Sometimes that is not sufficient. For example: class Class { public: Class( const char* str ); ~Class(); private: char* stored; }; Class::Class( c

我们何时必须使用复制构造函数?

我知道C ++编译器为类创建一个拷贝构造函数。 在这种情况下,我们必须编写一个用户定义的拷贝构造函数吗? 你能举一些例子吗? 编译器生成的复制构造函数执行成员智能复制。 有时候这还不够。 例如: class Class { public: Class( const char* str ); ~Class(); private: char* stored; }; Class::Class( const char* str ) { stored = new char[srtlen( str ) + 1 ]; strcpy( stored, str ); } Cl

Move assignment operator and `if (this != &rhs)`

In the assignment operator of a class, you usually need to check if the object being assigned is the invoking object so you don't screw things up: Class& Class::operator=(const Class& rhs) { if (this != &rhs) { // do the assignment } return *this; } Do you need the same thing for the move assignment operator? Is there ever a situation where this == &rhs

移动赋值运算符和`if(this!=&rhs)`

在一个类的赋值操作符中,通常需要检查被赋值的对象是否是调用对象,这样就不会搞砸了: Class& Class::operator=(const Class& rhs) { if (this != &rhs) { // do the assignment } return *this; } 移动赋值操作符需要相同的东西吗? 有没有this == &rhs会是真的吗? ? Class::operator=(Class&& rhs) { ? } 哇,这里要清理这么多... 首先,复制和交换并不总是实现复

How to "return an object" in C++?

I know the title sounds familiar as there are many similar questions, but I'm asking for a different aspect of the problem (I know the difference between having things on the stack and putting them on the heap). In Java I can always return references to "local" objects public Thing calculateThing() { Thing thing = new Thing(); // do calculations and modify thing retur

如何在C ++中“返回一个对象”?

我知道标题听起来很熟悉,因为有很多类似的问题,但我要求的是问题的不同方面(我知道在堆栈中放置事物并将其放在堆上的区别)。 在Java中,我总是可以返回对“本地”对象的引用 public Thing calculateThing() { Thing thing = new Thing(); // do calculations and modify thing return thing; } 在C ++中,为了做类似的事情,我有两个选项 (1)每当我需要“返回”一个对象时,我都可以使用引用 void calculate

What is the correct way of using C++11's range

What is the correct way of using C++11's range-based for ? What syntax should be used? for (auto elem : container) , or for (auto& elem : container) or for (const auto& elem : container) ? Or some other? Let's start differentiating between observing the elements in the continer vs. modifying them in place. Observing the elements Let's consider a simple example: vect

什么是使用C ++ 11范围的正确方法

什么是使用基于范围的C ++ 11的正确方式for ? 应该使用什么语法? for (auto elem : container)还是for (auto& elem : container)或for (const auto& elem : container) ? 还是其他的? 让我们开始区分大小写元素和修改它们之间的区别。 观察元素 我们来看一个简单的例子: vector<int> v = {1, 3, 5, 7, 9}; for (auto x : v) cout << x << ' '; 上面的代码在vector打印元素( in

Which C++ Standard Library wrapper functions do you use?

This question, asked this morning, made me wonder which features you think are missing from the C++ Standard Library, and how you have gone about filling the gaps with wrapper functions. For example, my own utility library has this function for vector append: template <class T> std::vector<T> & operator += ( std::vector<T> & v1, const std

您使用哪种C ++标准库包装函数?

今天早上提出的这个问题使我想知道C ++标准库中缺少哪些功能,以及如何利用包装函数填补空白。 例如,我自己的实用程序库具有此向量附加功能: template <class T> std::vector<T> & operator += ( std::vector<T> & v1, const std::vector <T> & v2 ) { v1.insert( v1.end(), v2.begin(), v2.end() ); return v1; } 和这个清除(或多或少)任何

What's the point of const pointers?

I'm not talking about pointers to const values, but const pointers themselves. I'm learning C and C++ beyond the very basic stuff and just until today I realized that pointers are passed by value to functions, which makes sense. This means that inside a function I can make the copied pointer point to some other value without affecting the original pointer from the caller. So what'

什么是const指针的要点?

我不是在谈论const指针的指针,而是指向const指针本身。 我正在学习C和C ++,超越了基本的东西,直到今天,我意识到指针会被传递给函数,这很有道理。 这意味着在一个函数中,我可以使复制的指针指向其他一些值,而不影响来自调用者的原始指针。 那么有一个功能标题说什么? void foo(int* const ptr); 在这样的函数内部,你不能让ptr指向别的东西,因为它是const的,你不希望它被修改,但是像这样的函数: void foo(int*

Bug in glibc `div()` code?

As quoted in "Integer division rounding with negatives in C++", in C before C99 (ie in C89) and in C++ before C++11 (ie in C++98 and C++03), for an integer division computation where either operand is negative the sign of the remainder (or equivalently, the rounding direction of the quotient) is implementation-defined. Then comes the standard function std::div which is specified to tr

glibc`div()`代码中的错误?

如C99之前的C(即C89)和C ++ 11之前的C ++(即C ++ 98和C ++ 03)中的C中的“整数除法与C ++中的负数四舍五入”所引用的那样,或者操作数是负的,其余的符号(或者等价地,商的舍入方向)是实现定义的。 然后是标准函数std::div ,它被指定为将商截短为零(即,余数与分数(分子)具有相同的符号)(例如,参见“div()库函数的用途?“)。 这里是glibc的代码为div() (源)(也引用“是div函数有用(stdlib.h)?”): (注

ADL and container functions (begin, end, etc)

C++11 and later define free functions begin , end , empty , etc in namespace std. For most containers these functions invoke the corresponding member function. But for some containers (like valarray ) these free functions are overloaded (initializer_list does not have a member begin()). So to iterate over any container free functions should be used and to find functions for container from name

ADL和容器功能(开始,结束等)

C ++ 11及更高版本在命名空间标准中定义了自由函数begin , end , empty等。 对于大多数容器来说,这些函数调用相应的成员函数。 但对于一些容器(比如valarray ),这些自由函数被重载(initializer_list没有成员begin())。 因此,要迭代任何容器free函数应使用从比其他命名空间的集装箱功能std ADL应使用: template<typename C> void foo(C c) { using std::begin; using std::end; using std::empt