Created
March 8, 2020 16:08
-
-
Save elteammate/3bb16f9ddba9d96cd3ff1ab70ed2b5f7 to your computer and use it in GitHub Desktop.
C++ Variable initialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
struct A { | |
int x; | |
A(int x_ = 0) { x = x_; } | |
}; | |
struct B { | |
int x; | |
int* y; | |
A* a; | |
B(int x_, int* y_, A* a_) { x = x_; y = y_; a = a_; } | |
}; | |
int y_ptr = 10; | |
int* y_ptr_ = &y_ptr; | |
A* a_ptr = &A(11); | |
B* b_ptr = &B(12, y_ptr_, a_ptr); | |
int y_new = 20; | |
int* y_new_ = &y_new; | |
A* a_new = new A(21); | |
B* b_new = new B(22, y_new_, a_new); | |
int main() { | |
int y_loc = 30; | |
int* y_loc_ = &y_loc; | |
A* a_loc = &A(31); | |
B* b_loc = &B(32, y_loc_, a_loc); | |
int y_heap = 40; | |
int* y_heap_ = &y_heap; | |
A* a_heap = new A(41); | |
B* b_heap = new B(42, y_heap_, a_heap); | |
cout << b_heap->y << " -> " << *b_heap->y << " " << b_heap->a->x << endl; | |
cout << b_loc->y << " -> " << *b_loc->y << " " << b_loc->a->x << endl; | |
cout << b_new->y << " -> " << *b_new->y << " " << b_new->a->x << endl; | |
cout << b_ptr->y << " -> " << *b_ptr->y << " " << b_ptr->a->x << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment