pointers

Incrementing pointer not working

ε祈祈猫儿з 提交于 2020-11-29 10:35:18
问题 I’m trying to increment pointer. I was sure that it is similar to do i+=1 , but I’m getting adress. #include "stdafx.h" #include <iostream> using namespace std; int main() { int i = 42; int *a = &i; *a++; cout << *a; cin.get(); return 0; } Can anybody explain why ? 回答1: ++ has a higher operator precedence than the pointer dereference operator * . So what *a++ does is to return the value of i (the old value of *a ) before incrementing the pointer value. After that expression has been evaluated

Incrementing pointer not working

半城伤御伤魂 提交于 2020-11-29 10:33:27
问题 I’m trying to increment pointer. I was sure that it is similar to do i+=1 , but I’m getting adress. #include "stdafx.h" #include <iostream> using namespace std; int main() { int i = 42; int *a = &i; *a++; cout << *a; cin.get(); return 0; } Can anybody explain why ? 回答1: ++ has a higher operator precedence than the pointer dereference operator * . So what *a++ does is to return the value of i (the old value of *a ) before incrementing the pointer value. After that expression has been evaluated

C++ : Covariant return type without pointer

本小妞迷上赌 提交于 2020-11-28 15:20:43
问题 I create two simple classes by inheritance, and I add a virtual function and the override in the child class. class Parent { public: virtual Parent foo(); }; class Child : public Parent { public: Child foo() override; }; In this case, my overridden function get an error : error C2555: 'Child::foo': overriding virtual function return type differs and is not covariant from 'Parent::foo' If I change return types with pointer : class Parent { public: virtual Parent* foo(); }; class Child : public

C++ : Covariant return type without pointer

…衆ロ難τιáo~ 提交于 2020-11-28 15:16:05
问题 I create two simple classes by inheritance, and I add a virtual function and the override in the child class. class Parent { public: virtual Parent foo(); }; class Child : public Parent { public: Child foo() override; }; In this case, my overridden function get an error : error C2555: 'Child::foo': overriding virtual function return type differs and is not covariant from 'Parent::foo' If I change return types with pointer : class Parent { public: virtual Parent* foo(); }; class Child : public

C++ : Covariant return type without pointer

扶醉桌前 提交于 2020-11-28 15:12:55
问题 I create two simple classes by inheritance, and I add a virtual function and the override in the child class. class Parent { public: virtual Parent foo(); }; class Child : public Parent { public: Child foo() override; }; In this case, my overridden function get an error : error C2555: 'Child::foo': overriding virtual function return type differs and is not covariant from 'Parent::foo' If I change return types with pointer : class Parent { public: virtual Parent* foo(); }; class Child : public

C++ : Covariant return type without pointer

拥有回忆 提交于 2020-11-28 15:12:41
问题 I create two simple classes by inheritance, and I add a virtual function and the override in the child class. class Parent { public: virtual Parent foo(); }; class Child : public Parent { public: Child foo() override; }; In this case, my overridden function get an error : error C2555: 'Child::foo': overriding virtual function return type differs and is not covariant from 'Parent::foo' If I change return types with pointer : class Parent { public: virtual Parent* foo(); }; class Child : public

C++ Linked list using smart pointers

偶尔善良 提交于 2020-11-26 04:49:20
问题 I have only been using raw pointers for linked list with templates. For example, the member data, Node<T>* head; and when I am inserting a node one of the lines would be head = new Node<T>(data); . However, now I need to use a smart pointer and I am not sure how I would change it to use smart pointers. Would the member data be changed to shared_ptr<Node<T>> head; and the other line would change to head = shared_ptr<Node<T>>( new <Node<T>>(data) ); ? 回答1: You do not "need" to use a smart

C++ Linked list using smart pointers

大憨熊 提交于 2020-11-26 04:48:42
问题 I have only been using raw pointers for linked list with templates. For example, the member data, Node<T>* head; and when I am inserting a node one of the lines would be head = new Node<T>(data); . However, now I need to use a smart pointer and I am not sure how I would change it to use smart pointers. Would the member data be changed to shared_ptr<Node<T>> head; and the other line would change to head = shared_ptr<Node<T>>( new <Node<T>>(data) ); ? 回答1: You do not "need" to use a smart

C++ Linked list using smart pointers

对着背影说爱祢 提交于 2020-11-26 04:48:41
问题 I have only been using raw pointers for linked list with templates. For example, the member data, Node<T>* head; and when I am inserting a node one of the lines would be head = new Node<T>(data); . However, now I need to use a smart pointer and I am not sure how I would change it to use smart pointers. Would the member data be changed to shared_ptr<Node<T>> head; and the other line would change to head = shared_ptr<Node<T>>( new <Node<T>>(data) ); ? 回答1: You do not "need" to use a smart