dcsimg
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
    Posts
    27,449

    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.
    Code:
    	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.
    Code:
    	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.

    Regards,

    Paul McKenzie

  2. #17
    Join Date
    Jan 2008
    Posts
    60

    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...
    Code:
    class Job
    {
    public:
    	Job(string compName, string titl, double sal)
    		: companyName(compName), title(titl), salary(sal){};
    	Job(){};
    	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;};
    
    
    private:
    	string companyName;
    	string title;
    	double salary; 
    };
    //*********************************************************8
    class person
    {
    public:
    	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;};
    
    
    private:
    	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);
    RafaelB.setAge(22);
    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
    Posts
    38

    Re: pointers....

    to get your code compiled, you need to fix the following things..
    1. include the follwoing in the top
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    2. in your get2ndJob() you need to have
    Code:
    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
    Code:
    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
    Location
    M31
    Posts
    885

    Re: pointers....

    ASDKJGASDJKASDASD!!!!111
    main returns an int, not anything else.

  5. #20
    Join Date
    Jan 2008
    Posts
    60

    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.
    Code:
    	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)