CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Thread: pointers....

  1. #16
    Join Date
    Apr 1999

    Re: pointers....

    Quote Originally Posted by armen_shlang
    Ok I'm sorry but the answers didn't digest quite well. Even though I'm sure an average person would've got it by now, but I didn't.
    	string& getCompanyName() {return companyName;};
    	string& getTitle() {return title;};
    What practical purpose would you have of returning a reference to a string? Just return the string.
    	string getCompanyName() {return companyName;};
    	string getTitle() {return title;};
    What if I want to get the company name, and in some other non-related function, write that name to a file, long after the object is gone? You can't do that with the code you posted, since that reference is tied to the object.

    By returning a string, that string can last beyond the life the object.


    Paul McKenzie

  2. #17
    Join Date
    Jan 2008

    Re: pointers....

    skanthaverl, I understand what you mean and why to make the firstjob a reference and the secondjob a pointer. However, I'm having problems trying to code it....I'm a kinda newbie tackling really hard. This is what i got so far, and it's completly wrong, how can i fix this according to what you said...
    class Job
    	Job(string compName, string titl, double sal)
    		: companyName(compName), title(titl), salary(sal){};
    	string& getCompanyName() {return companyName;};
    	string& getTitle()		 {return title;};
    	double	getSalary()		 {return salary;};
    	void setCompanyName(string name) {companyName = name;};
    	void setTitleName(string name) {title = name;};
    	void setSalery(double name) {salary = name;};
    	string companyName;
    	string title;
    	double salary; 
    class person
    	person(string name, string last, string compName, string title, double sal)
    	: m_name(name), m_last(last), m_firstJob(compName,title,sal)
    	int getAge(){return m_age;};
    	string getName(){return m_name;};
    	string getLast(){return m_last;};
    	void setAge(int age) {m_age = age;};
    	Job& get1stJob(){ return m_firstJob;};
    	Job* get2ndJob(){ return &m_secondJob;};
    	string	m_name;
    	string	m_last;
    	int	m_age;
    	Job&	m_firstJob;
    	Job*	m_secondJob;
    void main()
    person RafaelB("Rafael","BakerMan", "Starbucks", "maneger", 8.50);
    cout << RafaelB.getName() << " --" << RafaelB.getLast() << " --" << RafaelB.getAge() << endl;
    cout << RafaelB.get1stJob().getCompanyName() << " --" << 
    RafaelB.get1stJob().getTitle() << " --" << RafaelB.get1stJob().getSalary() << endl;
    Last edited by armen_shlang; March 26th, 2008 at 07:38 AM.

  3. #18
    Join Date
    Jul 2007

    Re: pointers....

    to get your code compiled, you need to fix the following things..
    1. include the follwoing in the top
    #include <string>
    #include <iostream>
    using namespace std;
    2. in your get2ndJob() you need to have
    Job* get2ndJob(){ return m_secondJob;};
    not return &m_secondJob; because, m_secondJob itself is a Job* type, and your get2ndJob() expect a Job* as it's return type. so simply return m_secondJob. if you say &m_secondJob, you are returning a pointer to pointer to Job, ie Job** type.

    3. In your initialisation list, to init m_firstJob, you need to pass an instance/reference of a Job object; not the Job's constructor parameters.
    it will be like this
    person(string name, string last, string compName, string title, double sal, Job&    job): m_name(name), m_last(last), m_firstJob(job){};
    note that I've changed the signature of the constructor person(.......). do necessary changes in the main() as well.

    now your code should be OK.

  4. #19
    Join Date
    Jun 2006

    Re: pointers....

    main returns an int, not anything else.

  5. #20
    Join Date
    Jan 2008

    Re: pointers....

    Thank you,
    I didn't know How to have the m_firstJob() to take only one argument also I didn't know about the constructor and how to write the member initialization list.

    One last thing...
    How do declare it in the main function...what should i have in the last argument.
    	person RafaelB("Rafael","BakerMan", "Starbucks", "maneger", 8.50, ???);
    also how can I initialize the m_secondJob inside the main.

    Thank you very much again.
    i stayed up all night for this concept.
    Last edited by armen_shlang; March 26th, 2008 at 08:42 AM.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)