Thread: problem with pointer to object and STL list

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    36

    problem with pointer to object and STL list

    Code:
    #include <iostream>
    #include <string>
    #include <list>
    using namespace std;
    
    class Member {
    	string name;
    	public:
    	Member(string name) {
    		this->name = name;
    	};
    	Member() {
    		string line;
    		cout << "Give name: ";
    		getline(cin, line);
    		name = line;
    	}
    	virtual void print() {
    		cout << name << endl;
    	}
    };
    
    class Employee:public Member {
    	string salary;
    	public:
    	Employee(string name, string salary):Member(name) {
    		this->salary = salary;
    	};
    	Employee():Member() {
    		string line;
    		cout << "Give salary: ";
    		getline(cin, line);
    		salary = line;
    	}
    	void print() {
    		Member::print();
    		cout << salary << endl;
    	};
    };
    
    
    int main ()
    {
    	
    	list<Member *> mlist;
    	
    	int ans;
    	string buf;
    	
    	cout << "What do you want to put?" << endl
    	<< "Member (1) - Employee (2) ? : ";
    	cin >> ans;
    	getline(cin,buf);
    	switch (ans) {
    		case 1 : {
    			Member memtest;
    			mlist.push_back(&memtest);
    			break;
    		}
    		case 2 : {
    			Employee emptest;
    			mlist.push_back(&emptest);
    			break;
    		}
    		
    		default :
    		cout << "Wrong Choice!" << endl;
    		break;
    	};
    	
    	list<Member *>::iterator iter;
    	for (iter = mlist.begin(); iter != mlist.end(); iter++)
    		(*iter)->print();
    
    	return 0;
    }
    I want to make a list of pointers to Member objects and then print the list using polymorphic function print. i want to use polymorphism - that's why i think i have to use pointer to object. The program crashes with segmentation fault.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Um, you really don't want to be assigning the address of local variables to your containers. Start thinking about dynamic memory and you'll have fewer troubles...kinda sorta.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    Code:
    int main ()
    {
    	list<Member *> mlist;
    	Member memtest;
    	Employee emptest;
    	int ans;
    	string buf;
    	
    	cout << "What do you want to put?" << endl
    	<< "Member (1) - Employee (2) ? : ";
    	cin >> ans;
    	getline(cin,buf);
    	switch (ans) {
    		case 1 : {
    			memtest = Member();
    			mlist.push_back(&memtest);
    			break;
    		}
    		case 2 : {
    			emptest = Employee();
    			mlist.push_back(&emptest);
    			break;
    		}
    		
    		default :
    		cout << "Wrong Choice!" << endl;
    		break;
    	};
    	
    	list<Member *>::iterator iter;
    	for (iter = mlist.begin(); iter != mlist.end(); iter++)
    		(*iter)->print();
    
    	return 0;
    }
    I placed the declarations in top level. Now it works - is there any way to avoid calling the construtor when i declare them? and i still don't get why the first thing didn't work. In the second program i still call the constructor emptest = Employee() inside the switch so still emptest points to some piece of memory allocated locally...

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    just forget the last post. i used "new" and member pointers and now it works smooothly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL List container with classes
    By Rune Hunter in forum C++ Programming
    Replies: 10
    Last Post: 10-25-2005, 02:13 PM
  2. the stl vector class in relation to pathfinding
    By DavidP in forum Game Programming
    Replies: 4
    Last Post: 05-14-2004, 03:03 PM
  3. Pointer validity check
    By Carlos in forum Windows Programming
    Replies: 6
    Last Post: 12-11-2003, 03:40 AM
  4. STL Map Object
    By nomes in forum C++ Programming
    Replies: 6
    Last Post: 09-11-2003, 01:51 PM
  5. Pointer Elements & STL Containers :: STL
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 08:13 PM