here is a sample code: http://cpp.sh/5top3 if you scroll all the way to bottom it shows what I want it to output |
Was it that hard to copy it inside this thread?
1) To achieve what you want, your code could be far simpler:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
#include <string>
#include <vector>
class Friend
{
public:
std::string name;
Friend() = default;
~Friend(){}
void setName(std::string friend_name) { name = friend_name; }
void setAge(int friend_age) { age = friend_age; }
void setHeight(int friend_height) { height = friend_height; }
void printFriendInfo() const;
private:
int age {};
double height {};
};
void Friend:: printFriendInfo() const
{
std::cout << "Name : " << name << '\n';
std::cout << "Age : " << age << '\n';
std::cout << "Height : " << height << "\n\n";
}
int main()
{
std::vector<Friend> list;
Friend f1;
f1.setAge(12);
f1.setHeight(6.12);
for (int n = 0; n < 3; ++n)
{
f1.setName( 0 < n ? "ANOTHER NAME" : "BILL" );
list.push_back(f1);
}
for ( auto it = list.begin(); it != list.end(); ++it ) {
it->printFriendInfo();
}
return 0;
}
| |
2) Anyway, assuming you’re just playing around with pointers, you should consider keeping your code more readable:
a)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
Friend *temp;
for (int n=0; n<3; n++)
{
f1->setName("BILL");
f1->setAge(12);
f1->setHeight(6'12);
if(n==0){
temp = f1;
}
if(n==1){
temp->setName("ANOTHER NAME");
}
list.push_back(*f1);
| |
When n is equal to 0, temp is assigned a valid address; since n becomes 0 at least once, your code is apparently fine.
Anyway, that’s not as simple to read as it could be and it doesn’t look so beneficial: you just have two pointers which modifies the same memory area at every iteration, and one of them is not initialized at definition. Every tiny code modification could bring hard to debug bugs along.
That simply works because later that memory area is
copied inside the std::vector.
If I’d been requested of an opinion about the above code, I’d say the standard library just saves your ass.
b)
You forgot to delete you pointers and to set them to nullptr:
1 2 3
|
delete f1;
f1 = nullptr;
temp = nullptr;
| |
c)
You declare your variable at the beginning of main() without initializing them.
In general, don’t do that. Do declare your variables only when you have a valid value to assign them:
http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#nr1-dont-all-declarations-should-be-at-the-top-of-a-function
http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es21-dont-introduce-a-variable-or-constant-before-you-need-to-use-it
d)
That’s for experts only; don’t use it:
http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#sf6-use-using-namespace-directives-for-transition-for-foundation-libraries-such-as-std-or-within-a-local-scope-only
http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#sf7-dont-write-using-namespace-at-global-scope-in-a-header-file
Once you’ve introduced the entire namespace std inside your code, you use a name (‘list’), which is part of the standard library, to name a std::vector... That’s at least confusing.
e)
In general, prefer double to float.
f)
Trust you compiler: it’s pretty good at optimization, very often better than programmers themselves - definitely better than me :-)
For example, I think this code:
1 2 3 4 5 6 7 8
|
for (int n = 0; n < 3; ++n)
{
Friend f1;
f1.setAge(12);
f1.setHeight(6.12);
f1.setName( 0 < n ? "ANOTHER NAME" : "BILL" );
list.push_back(f1);
}
| |
is better than the one I presented above:
1 2 3 4 5 6 7 8
|
Friend f1;
f1.setAge(12);
f1.setHeight(6.12);
for (int n = 0; n < 3; ++n)
{
f1.setName( 0 < n ? "ANOTHER NAME" : "BILL" );
list.push_back(f1);
}
| |
simply because ‘f1’ will disappear at the end of the for-loop.
g)
Probably a typo:
f1->setHeight(6'12); should be
f1->setHeight(6.12);