Thread: Pointer Question

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    Pointer Question

    Code:
    employee * create()
    {
    	employee * _newemployee;
    	string input1;
    	string input2;
    	cout << "Please enter the new employee's name: ";
    	getline(cin, input1);
    	cout << "Please enter the new employee's payrate: ";
    	getline(cin, input2);
    	_newemployee = new employee(input1, input2);
    	return _newemployee;
    }
    Is this correct? I didn't get any errors or exceptions in debug. I'm just wondering if this is correct usage pf pointers? I had to check a tutorial and I haven't really programmed much in a long time, any feedback would be appreciated!
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If the constructor is correct, then it could well be correct, though I might write:
    Code:
    employee* create()
    {
        cout << "Please enter the new employee's name: ";
        string name;
        getline(cin, name);
        cout << "Please enter the new employee's payrate: ";
        string payrate;
        getline(cin, payrate);
        return new employee(name, payrate);
    }
    That said, do you really need to use new here?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    I wanted to do new because eventually what I want to do is something like this:

    Code:
    int main()
    {
    	int selection;
    	string input;
    	bool goodinput = false;
        while (goodinput == false)
    	{
    		cout << "Welcome to the employee database, what would you like to do?" << endl		// prompt
    			 << "1. Add an employee" << endl << "2. Modify an employee" << endl;
    
    		getline(cin, input);	//get input
    		stringstream(input) >> selection;	//extract selection integer
    
    		if (selection == 1)
    		{
    			goodinput = true;
    			employees.push_back(create());
    		}
    		else if (selection == 2)
    		{
    			goodinput = false;
    		}
    		else
    		{
    			goodinput = false;	// invalid selection - continue loop
    			cout << "Please enter a valid selection, 1 or 2." << endl;
    		}
    	}
        return 0;
    }
    Also, if I explicitly call an instance of employee, like employee newemployee(name,payrate)...
    Doesn't that instance only last for the scope of the function?
    If not I don't need new at all, and I can just return a reference.
    Last edited by Shamino; 01-20-2012 at 11:52 AM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Shamino
    I wanted to do new because eventually what I want to do is something like this:
    You could, but then you will need to do manual memory management, e.g., to delete what you new.

    Quote Originally Posted by Shamino
    Also, if I explicitly call an instance of employee, like employee newemployee(name,payrate)...
    Doesn't that instance only last for the scope of the function?
    Yes, but you can return a copy, and if you are calling this function like:
    Code:
    employee x(create());
    This copying may even be optimised away.

    Quote Originally Posted by Shamino
    If not I don't need new at all, and I can just return a reference.
    You should not return a reference to a local variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Would deleting the contents of the vector before the program shuts down cover my butt if I use new?

    Or running a destructor for each employee held in the employees vector?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Every new needs a corresponding delete to prevent leaks, so this

    Or running a destructor for each employee held in the employees vector?
    Is what happens when you delete what you new.

  7. #7
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Oh good, then new is safe as long as I clean up after myself? I wanted to use pointers, because I know the stack is limited, and with a database potentially getting large I'd want to dynamically manage my memory eventually. I've done stuff like this before, it's just been so long I'm refamiliarizing myself with syntax.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually, vector uses heap memory, so you generally don't have to worry about how elements are stored until you run out, because heap memory is really big.

    The only reason you might want to do what you're doing is polymorphism. IOW, employee being a base class, and you want the vector to store any derived objects.

  9. #9
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Hmmm, it would save memory in the long haul to use polymorphism to differentiate between types of employees, for instance, salaried and hourly workers..

    Then I wouldn't have a single employee structure that could possibly have unused variables, does this make sense?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  10. #10
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Code:
    employee* get_employee(string _name)
    {
    	for (unsigned int i = 0; i < employees.size();)
    	{
    		if (_name.compare(employees[i]->name) == 1)
    		{
    			return employees[i];
    		}
    		else
    		{
    			i++;
    		}
    	}
    }
    
    void list_employees(vector<employee*> * _employees)
    {
    	for (unsigned int i = 0; i < _employees->size(); i++)
    	{
    		cout << employees[i]->name << endl;
    	}
    }
    Is this going to have the desired effect? My loop logic might be off.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would write it as:
    Code:
    employee* get_employee(string _name)
    {
        for (std::size_t i = 0; i < employees.size(); i++)
        {
            if (_name.compare(employees[i]->name) == 1)
                return employees[i];
        }
    }
     
    void list_employees(vector<employee*> & _employees)
    {
        for (std::size_t i = 0; i < _employees.size(); i++)
        {
            cout << employees[i].name << endl;
        }
    }
    That said, what is this?
    if (_name.compare(employees[i]->name) == 1)
    How does your employee struct/class look like? I am skeptical.

    Furthermore, if you intend to find an employee by name a lot, you may want to use a set, a map or a hash table for fast lookup.
    Also, if you really must use pointers, then use smart pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  2. pointer question.... pointer theory?
    By panfilero in forum C Programming
    Replies: 6
    Last Post: 11-12-2005, 02:29 AM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM
  5. Pointer to pointer question
    By caduardo21 in forum C Programming
    Replies: 7
    Last Post: 07-18-2005, 04:03 PM