This code is a simplified test for what I'm trying to do elsewhere. I have a function that takes a ref-to-ptr argument and modifies it to return a pointer from a list of pointers.
#include <iostream> #include <list> using namespace std; typedef int* intp; typedef std::list<intp> intplist; intplist myList; void func(intp &arg) // (1) { intplist::const_iterator it = myList.begin(); std::advance(it, 2); arg = *it; } int main() { myList.push_back(new int(1)); myList.push_back(new int(2)); myList.push_back(new int(3)); int* ip = NULL; // (2) func(ip); if (ip) cout << "ip = " << *ip << endl; else cout << "ip is null!" << endl; for (intplist::const_iterator it = myList.begin(); it != myList.end(); ++it) delete *it; return 0; }
It works and prints ip = 3 , as expected, only it bothers me that it can cause undefined behavior or otherwise lead to problems, because I remove the iterator constant, assigning the result of its dereference to Argument. I tried adding const to (1) and (2) but did not create one.
Am I right to worry? If so, why am I not getting a warning from g ++ (4.9.2)?
source share