I was reading the answers to this question C++ pros and cons and got this doubt while reading the comments. programmers frequently find it confusing that "this" is a pointer but not a reference. another confusion is why "hello" is not of type std::string but evaluates to a char const* (pointer) (after array to pointer conversion) – Johannes Schaub - litb Dec 22 '08 at 1
我正在阅读这个问题的答案C ++的优点和缺点,并在阅读评论时产生了疑问。 程序员们经常发现它让人困惑,“this”是一个指针,但不是一个参考。 另一个困惑是为什么“hello”不是std :: string类型,而是求值为char const *(pointer)(在数组到指针转换之后) - Johannes Schaub - litb 08年8月22日在1:56 这只表明它不使用与其他(以后)语言相同的约定。 - le dorfier 08年12月22日在3:35 尽管如此,我将这个“这个”
I have a solid understanding of most OO theory but the one thing that confuses me a lot is virtual destructors. I thought that the destructor always gets called no matter what and for every object in the chain. When are you meant to make them virtual and why? Virtual destructors are useful when you can delete an instance of a derived class through a pointer to base class: class Base {
我对大多数面向对象理论有了深刻的理解,但让我困惑的是虚拟析构函数。 我认为,无论对于链中的每个对象而言,析构函数都会被调用。 你什么时候想让它们变成虚拟的,为什么? 当您可以通过指向基类的指针删除派生类的实例时,虚拟析构函数非常有用: class Base { // some virtual methods }; class Derived : public Base { ~Derived() { // Do some important cleanup } }; 在这里,你会注
What would be better practice when giving a function the original variable to work with: unsigned long x = 4; void func1(unsigned long& val) { val = 5; } func1(x); or: void func2(unsigned long* val) { *val = 5; } func2(&x); IOW: Is there any reason to pick one over another? My rule of thumb is: Use pointers if you want to do pointer arithmetic with them (eg
给一个函数使用原始变量时,更好的做法是: unsigned long x = 4; void func1(unsigned long& val) { val = 5; } func1(x); 要么: void func2(unsigned long* val) { *val = 5; } func2(&x); IOW:有什么理由选择一个吗? 我的经验法则是: 如果你想对它们进行指针运算,就使用指针(例如,增加指针地址以逐步通过一个数组),或者你必须传递一个NULL指针。 否则请使用参考。 我真
Once upon a time, I assumed that code like this would fail: const MyClass& obj = MyClass(); obj.DoSomething(); because the MyClass object would be destroyed at the end of its full-expression, leaving obj as a dangling reference. However, I learned (here) that this isn't true; the standard actually has a special provision that allows const references to keep temporaries alive until sai
曾几何时,我认为这样的代码会失败: const MyClass& obj = MyClass(); obj.DoSomething(); 因为MyClass对象在其全表达式结束时会被销毁,所以将obj留作悬挂引用。 但是,我在这里了解到这不是真的; 该标准实际上有一个特殊的规定,允许const引用使临时对象保持活动状态,直到所有引用被自己销毁。 但是,有人强调,只有const引用才具有这种力量。 今天,我将VS2012中的代码作为实验运行。 struct Foo { Foo() { s
If I have a function that needs to work with a shared_ptr , wouldn't it be more efficient to pass it a reference to it (so to avoid copying the shared_ptr object)? What are the possible bad side effects? I envision two possible cases: 1) inside the function a copy is made of the argument, like in ClassA::take_copy_of_sp(boost::shared_ptr<foo> &sp) { ... m_sp_mem
如果我有一个需要使用shared_ptr的函数,将它的引用传递给它会不会更高效(所以为了避免复制shared_ptr对象)? 什么是可能的不良副作用? 我设想了两种可能的情况: 1)函数内部的副本由参数组成,如in ClassA::take_copy_of_sp(boost::shared_ptr<foo> &sp) { ... m_sp_member=sp; //This will copy the object, incrementing refcount ... } 2)在函数内部只使用参数,就像 Class
This question already has an answer here: What is a smart pointer and when should I use one? 14 answers Are you asking for example code? Hopefully this helps... class Mammal { public: virtual void Speak() const = 0; }; class Dog : public Mammal { public: void Speak() const { cout << "Woof Woof" << endl; } }; Mammal* getDog() { return new Dog(); } int
这个问题在这里已经有了答案: 什么是智能指针,我应该什么时候使用它? 14个答案 你是否要求示例代码? 希望这有助于... class Mammal { public: virtual void Speak() const = 0; }; class Dog : public Mammal { public: void Speak() const { cout << "Woof Woof" << endl; } }; Mammal* getDog() { return new Dog(); } int main() { Mammal* m = getDog(); m->S
I started studying smart pointers of C++11 and I don't see any useful use of std::weak_ptr . Can someone tell me when std::weak_ptr is useful/necessary? A good example would be a cache. For recently accessed objects, you want to keep them in memory, so you hold a strong pointer to them. Periodically, you scan the cache and decide which objects have not been accessed recently. You don
我开始研究C ++ 11的智能指针,我没有看到std::weak_ptr任何有用的用法。 有人可以告诉我什么时候std::weak_ptr是有用/必要的? 一个很好的例子就是缓存。 对于最近访问的对象,你想保留它们在内存中,所以你持有一个强大的指针。 定期扫描缓存并确定哪些对象最近未被访问过。 你不需要把它们留在记忆中,所以你可以摆脱强壮的指针。 但是如果该对象正在使用中,并且其他一些代码拥有强大的指针呢? 如果缓存摆脱了它
C++ is all about memory ownership Aka " Ownership Semantics " It is the responsibility of the owner of a chunk of dynamically allocated memory to release that memory. So the question really becomes who owns the memory. In C++ ownership is documented by the type a RAW pointer is wrapped inside thus in a good (IMO) C++ program it is very rare [RARE not NEVER] to see RAW pointers pa
C ++全是关于内存所有权的 又名“ 所有权语义 ” 一块动态分配的内存的所有者负责释放该内存。 所以这个问题真的变成谁拥有记忆。 在C ++中,所有权由一个RAW指针包装在内部的类型记录,因此在一个好的(IMO)C ++程序中,很少见到RAW RAW指针传递过来(因为RAW指针没有推断的所有权,所以我们不能告诉谁拥有记忆,因此如果没有仔细阅读文件,你不能分辨谁对所有权负责)。 相反,很少见到存储在类中的RAW指针,每个RAW指
I'm trying to start thread using a shared_ptr from class Test , and I get this error: /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/functional:559:2: note: no known conversion for argument 1 from 'std::shared_ptr<Test>' to 'std::shared_ptr<Test>&' Example Code: std::shared_ptr<Test> test = std::make_shared<Test>(); std::thread t
我想从类Test的shared_ptr开始线程,并且我得到这个错误: /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/functional:559:2: note: no known conversion for argument 1 from 'std::shared_ptr<Test>' to 'std::shared_ptr<Test>&' 示例代码: std::shared_ptr<Test> test = std::make_shared<Test>(); std::thread th(&Test::run, test); // Compiler e
This question already has an answer here: What are the differences between a pointer variable and a reference variable in C++? 32 answers yes, it's essentially the same as a pointer but a nicer and safer version as the pointer address cant be changed or set to null. i see it mainly as a means to have nice function interfaces without requiring the user of the interface to worry about poi
这个问题在这里已经有了答案: C ++中的指针变量和引用变量之间有什么区别? 32个答案 是的,它基本上与指针相同,但是更好更安全的版本,因为指针地址不能被更改或设置为空。 我认为它主要是作为一种手段来获得很好的函数接口,而不需要接口的用户担心指针 - 即函数保持简单但不复制对象,纯粹是它的地址。 类拷贝构造函数是引用至关重要的主要例子。 一个引用是一个有一些特殊限制的指针。 绝对不能对参考进行初始化