I am passing object as pointer and then I want to use passed objects values to assign them to other method.
This is how I pass object:
Author *pAuthor = new Author("John", "Doe");
Publisher *pPublisher = new Publisher("Wrox", "10475 Crosspoint Blvd.", "Indianapolis");
Book *pBook = new Book("Memory Management", pAuthor, pPublisher, 39.99);
cout << pBook->getBookInfo() << endl;
Now, In other class I must read those passed variables from object and assign them somewhere else. In this code I am using only passed values and no object because I’m not sure how to deal with them.
Book::Book(string title, Author *pAuthor, Publisher *pPublisher, double price)
{
this->title = title;
this->price = price;
}
How do I read passed values from passed object?
Edit, There might be some confusion in my question. The below code must access the variables and insert then into methods which will later use them.
Each variable I try to insert look like this. I don’t want to access the author’s private variables because no way I can acces when they are set private, and yet they are still empty.
The code I have right now is:
Book::Book(string title, Author *pAuthor, Publisher *pPublisher, double price)
{
this->title = title;
this->price = price;
author.setFirstName();
author.setLastName();
publisher.setName();
publisher.setAddress();
publisher.setCity();
}
Use this syntax, supposing you have these variables and they are public.
And for other class.
The
'->'operator dereferences pointer to the object, or in other words, takes you to your desired variables.It is better if you use these data members as private members. If you do so, you cannot access them directly. You would have to use getter and setter functions to read and write their values.
They would look like this.
Lastly it is better if you dont pass by value these object. Pass them by reference and use
constbefore them.