Manipulating a class member outside a class

Hi,
I want to change value of an object that is a member of another object.
Although i used as a reference parameter in a function, i could not achieve this. Suppose that created a Product object and add it to newly created Storage object. After i change value of Product object, i want root item in Storage object also changes. But i could not achieve this.
Am i missing something in code below?
Thanks in advance,

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
#include <cstdlib>
#include <iostream>
using namespace std;

class Product {
private:
    string name;
public:
        Product(string newName) : name(newName) {} //constructor
        void setName(string newName) {name = newName;}
        string getName() {return name;}
};

struct ProductNode {
    Product data;
    ProductNode* left;
    ProductNode* right;
    ProductNode(Product &newData, ProductNode* newLeft, ProductNode * newRight)
    : data(newData), left(newLeft), right(newRight) {}
};

class Storage {
public:
        ProductNode* root; //root of a product tree
        Storage() { root = NULL; }
        void addNewProduct(Product &item) {
                if (root == NULL) root = new ProductNode(item, NULL, NULL);
                else {
                //..etc...
                }
        }
};

int main() {

    Product pr("dell");
    Storage st;
    st.addNewProduct(pr);
    cout << pr.getName() << ":" << st.root->data.getName() << endl;
    pr.setName("HP");
    cout << pr.getName() << ":" << st.root->data.getName() << endl;
    //i want this output to be: HP:HP
    return 0;
}
root = new ProductNode(item,NULL,NULL);

This line calls ProductNode's constructor, which has this in the member initializer list:

data(newData)

This calls Product's copy constructor, and since you have not written one yourself, the default one created by the compiler makes a shallow copy of the product you've passed to it.

If you want to reference an object that was created outside the container, you have to make a pointer to it.
there is no point to calling private in a class because all (unless public or protected is called) is private by default
There is a point to indicating private in a class. First it is more explicit, and second, if you were to add public or protected sections above it, you won't accidentally expose members you intended to be private but forgot to add the label. And if for some reason you wanted to change it from class to struct, you could just change it without worrying about adding in necessary labels.
Last edited on
thats why you add public and protected below. where is the advantage of changing from class to struct?
closed account (S6k9GNh0)
struct is visually more associated with packed data where as classes are associated with functional objects. There is no actual advantage outside of that though.

kayseri: In order to do what you're wanting, you'll have to change product name either through the storage class or you'll have to have each product keep up with the parent storage instance.

For instance, you could have "addNewProduct" return an index of the given product that was added so a syntax like as follows can be used:

1
2
int index = myStorage.addNewProduct(myProduct);
myStorage.changeProduct(index, "HP"); 


This way, the storage class would determine when it's root product changes which makes more sense. Although, I'm honestly not getting the whole root product thing anyways...

Also, it seems that I'm rather integrated with C as of late. Any more C++ savvy solutions would be great.
Last edited on
Thank you all,
@uberwulu, @Aramil of Elixia : the code i gave above was just a sample :) Restricting class members is not the case here :) Of course in real life applications i care such situations.
@computerquip : There is already such a function as you wrote in Storage class. I just did not write above. I just wondered a way to change outside Storage class.

Changing Product data; to Product *data; in ProductNode solved my problem here. But still looking for new ways to achieve my goal.
Thanks,

1
2
3
4
5
6
7
struct ProductNode {
    Product *data;
    ProductNode* left;
    ProductNode* right;
    ProductNode(Product &newData, ProductNode* newLeft, ProductNode * newRight)
    : data(newData), left(newLeft), right(newRight) {}
};
Last edited on
Topic archived. No new replies allowed.