How to edit original object through object* temp that points to original object?

So if I have an Object called Friend with a member called name.
I do

1
2
3
4
5
6
7
8
9
Friend* f1;
Friend* temp;
for(int i =0; i<2; i++){
   f1->name = "BILL";
   if(i == 0)
     temp = f1;
   if(i == 1)
      temp->name = "LARRY";
}

When I iterate through my friend vector it still returns
BILL
BILL
instead of:
Larry
BILL

How can I make it return the above?
> When I iterate through my friend vector
¿what vector?
¿why do you post different code?

by the way, you never initialised `f1', so it's not pointing to a valid object and you can't dereference it.
Last edited on
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
You are doing everything fine in terms of editing the contents of an object that temp is pointing to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Friend *f1;
   Friend *temp;
   f1 = new Friend;
   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);
        cout<<"DEBUG "<<temp->name<<endl;
   } 


However, the real prolem is in this condition:
1
2
3
 if(n==1){
     temp->setName("ANOTHER NAME");
 }


On the second iteration temp->setName("ANOTHER NAME"); really sets the name of f1 and it prints
BILL
ANOTHER NAME
BILL


You want it to print
BILL
ANOTHER NAME
ANOTHER NAME


The reason third one gets changed is because at the start of the for loop you set this:
1
2
3
f1->setName("BILL");
f1->setAge(12);
f1->setHeight(6'12); 


And the condition in the if statement when n==2 doesn't get executed, so, the f1 keeps name as BILL:
1
2
3
if(n==1){
    temp->setName("ANOTHER NAME");
}



To achieve what you want, either change the condition inside if statement to:
if(n>=1){

Or put the setters above the for loop like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Friend *f1;
   Friend *temp;
   f1 = new Friend;
   
    f1->setName("BILL");
    f1->setAge(12);
    f1->setHeight(6'12);
    
   for (int n=0; n<3; n++)
   {   
        if(n==0){
            temp = f1;
        }
        if(n==1){
            temp->setName("ANOTHER NAME");
        }
               
        list.push_back(*f1);
        cout<<"DEBUG "<<temp->name<<endl;
   }
    
Last edited on
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)
using namespace std;

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);

Topic archived. No new replies allowed.