-
Notifications
You must be signed in to change notification settings - Fork 15
My solutions for the exercises #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
de84d2a
e853c50
a48ce46
b0485d5
54d8715
74f597c
a73a856
a79b394
ca40cc0
e132108
319311d
fdfb4ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,18 +21,18 @@ class Student { | |
|
|
||
| Student* getStudent(){ | ||
| Student* myStudent = new Student(10801983,"Mary Wright"); | ||
| // ***** OPS! | ||
| return nullptr; | ||
| return myStudent; | ||
| } | ||
|
|
||
| void printStudent(Student* myStudent){ | ||
| std::cout << "-- Student's Data --\n"; | ||
| std::cout << "Student's ID is " << myStudent->getName() | ||
| << " and name is " << myStudent->getID() << std::cout; | ||
| std::cout << "Student's ID is " << myStudent->getID() | ||
| << " and name is " << myStudent->getName() << std::endl; | ||
| } | ||
|
|
||
| int main(){ | ||
| Student* myStudent = getStudent(); | ||
| printStudent(myStudent); | ||
| delete myStudent; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could have used a smart pointer here.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to do this without overwriting all standard pointer usage in the code? Initialising as a smart pointer type breaks
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may help: http://en.cppreference.com/w/cpp/memory/unique_ptr/get |
||
| return 1; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,17 @@ void indexLoop(std::vector<Tool>& tools){ | |
| } | ||
|
|
||
| void iteratorLoop(std::vector<Tool>& tools){ | ||
| for(std::vector<Tool>::const_iterator tool = tools.begin(), end = tools.end(); tool != end; ++tool){ | ||
| for(auto tool = tools.begin(), end = tools.end(); tool != end; ++tool){ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What exactly does How can
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What you get in hand is the object, not an iterator to it.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The iterator increment
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah. sorry. you are right. I was having a different example in my head :-) |
||
| tool->print(); | ||
| } | ||
| } | ||
|
|
||
| void rangeLoop(std::vector<Tool>& tools, const int beginning, const int ending){ | ||
| auto tool = tools.begin(), end = tools.end(); | ||
| std::advance(tool,beginning); | ||
| auto stop_point = std::distance(tools.begin(), end) - ending; | ||
|
|
||
| for( ; std::distance(tool, end) > stop_point; ++tool){ | ||
| tool->print(); | ||
| } | ||
| } | ||
|
|
@@ -42,6 +52,8 @@ int main(){ | |
| indexLoop(tools); | ||
| std::cout << "Start iterator looping" << std::endl; | ||
| iteratorLoop(tools); | ||
| std::cout << "Start range looping" << std::endl; | ||
| rangeLoop(tools, 2, 4); | ||
|
|
||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given this
newstatement and seeing no delete anywhere this code has a memory leak.