0

I am doing an assignment in which we are supposed to code a very basic scenario of a company to manage their products. So when a new product is entered we are supposed to check if it already exists (which we are supposed to do by comparing the names of the products with the name of the product entered; empty products have an empty string as their name), if we do not find it, then we are supposed to add it to the list of products of the company(there can be a maximum of 3 product only so I am checking each one of them individually).

class Store : public Product {
public:
        ...
        Product& return_product_1() {return product1;}
        Product& return_product_2() {return product2;}
        Product& return_product_3() {return product3;}

private:
        Product product1;
        Product product2;
        Product product3;
        ...
        
};

So I thought that I can use a function which returns a reference to an object which is currently empty (Since all the three Product objects are individually stored in the class, so adding or removing an object is simply asking for its details from the user or setting the data members to NULL respectively), so I created the following function to find a particular Product object. I pass a string n to check whether any Product object has a same name as n and I also pass an empty Product object so that i can make p synonymous with the returned Product since both of them are references. ( I did this because i wanted to return both the correct Product object as well as a boolean. I could have used std::pair to return both of them, but i did not wanted to do that.

bool StarberksInterface::search(string n, Product& p) {     

        if(store.return_product_1().product_name() == n) {
                p = store.return_product_1();
                return true;
        }
        else if(store.return_product_2().product_name() == n){
                p = store.return_product_2();
                return true;
        }
        else if(store.return_product_3().product_name() == n) {
                p = store.return_product_3();
                return true;
        }
        else return false;    // The function returns true when a Product object which the same name as the first parameter is found
}

product_name is defined as

std::string product_name() const {return name;}

where name is a data member of type std::string of Product class.

The function search(string, Product&) is called using the function call search("", p); where i pass an empty string to check for a currently empty product and p is declared as Product p;.

I declare another Product object Product temp = input();, where input is declared as Product input(); and it asks the user for the information about the product and returns a Product object. I have overloaded the assignment operator for Product class.

I then write

p = temp;

Now I was thinking was that since return_product_1 would have returned a reference, and p was assigned the value of return_product_1, therefore p and product1 should be synonymous ( Assuming that the search function initially finds that product1 indeed has an empty string as a name). Any changes made to p should be reflected in product1, but it seems that is not the case. I wanted that the details of temp should also be assigned to product1 since i assigned temp to p. Is there something wrong with my code or perhaps my logic? Any help is appreciated. Thank You.

CeNiEi
  • 97
  • 1
  • 5
  • References are not reassignable. When you create a reference to object, it will always point to that particular object and nothing else. `p = store.return_product_1();` calls copy assignment operator - you still have two separate objects, but now `p` will have values copied from `store.product1`. – Yksisarvinen Oct 08 '20 at 23:03
  • You could instead have `Product* StarberksInterface::search(const std::string& name)`. Also, any time you find yourself defining a list of variables like `var1`, `var2`, etc. all with the same type, consider changing to a `std::array` or `std::vector` instead, so that it takes much less code to use them. – aschepler Oct 08 '20 at 23:14
  • Usually, when you are trying to bend the rules of a language, there is a problem with your design. – paddy Oct 08 '20 at 23:38
  • @aschepler I completely get the use of the pointer, but isn't it safe to assume that C++ references behave as pointers.. I know the differences between references and pointers.. But as @Yksisarvinen stated, since ```p = store.return_product_1``` calls copy assignment operator but, ```p``` is a reference to a ```Product``` and ```store.return_product_1``` also returns a reference, so i was thinking that the changes which I do in ```p``` should also be reflected back in ```product1``` – CeNiEi Oct 09 '20 at 05:57
  • @CeNiEi You still have two different `Product` objects: the member of the `Store` object, and the object named `temp`. There's no way to combine them to be just one object. – aschepler Oct 09 '20 at 12:45

0 Answers0