Thread: Functions which create objects.

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Functions which create objects.

    Hi,

    I am trying to do the following:

    1) Call a function
    2) Ask the user for the datamember values
    3) Create the object
    4) Return to main() a pointer to the object
    5) Push that pointer into the std::list

    But I'm having trouble.

    Here is what I've made:

    base.h:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Base
    {
    public:
    
    	//Member Functions:
    	Base * createABaseObject();//Create an object and return a pointer to it
    
    private:
    
    protected:
    
    	//Data Members common to all objects (abstract base class):
    	string _mystring;
    
    };
    base.cpp:
    Code:
    #pragma once
    
    #include <iostream>
    #include <string>
    #include "base.h"
    
    Base * Base::createABaseObject()
    {
    	//Build an object and return a pointer to the object. 
    	//Then within main(), the pointer can be pushed into the std:list
    
    	cout << "You chose to create an object" << endl;
    
    	string mystring;
    	cout << "Value? " << endl;
    	getline (cin, mystring);
    
    	//create the object
    	Base *ptr = new Aircraft(mystring);
    
    	//return a pointer to main()
    	return(ptr);
    }
    
    //constructor
    Base::Base(string mystring)
    {
    	_mystring = mystring;
    }
    main.cpp:
    Code:
    #include <iostream>
    #include <list>
    #include <string>
    #include "base.h"
    using namespace std;
    
    int main()
    {
    	typedef list<Base *>BaseList;
    
    	do{
    		char choice;
    		cout << "Choose one of the options below:" << endl;
    		cout << "a) Enter a base class pointer into the list" << endl;
    		cin >> choice;
    		cin.ignore();
    
    		switch(choice)
    		{
    		case 'a':
    			{
    				//Create an object and return a pointer to the object
    				createABaseObject();
    
    				//push the returned pointer onto the list
    				BaseList.push_back( ptr );
    
    				cout << "Object pushed onto list" << endl;
    				break;
    			}
    		default:
    			{
    				cout << "You made an invalid choice" << endl;
    				break;
    			}
    		}
    	}
    	while(true);
    
    	system("pause");
    }
    This isn't compiling, and was hoping someone could comment on where I'm going wrong.

    Any help is really appreciated.

    Thanks
    Last edited by Swerve; 01-27-2010 at 04:50 PM.

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    You need to do something like:
    Code:
    Base *ptrBase = createABaseObject();
    BaseList.push_back(ptrBase);
    IOW you are not taking what your own factory (createABaseObject()) is returning to you.. you are just discarding it...and leaking..
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There's hardly anything which is right

    Firstly, are you dealing with a class called Aircraft or a class called Base?

    It also seems that createABaseObject is a member function of Base (or Aircraft). As such you'll first need a Base object to call this function on - as it is, you first need a Base to ask it to create other Bases - which doesn't seem to make much sense here to begin with.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    Wont he need to declare a variable of BaseList type?
    Code:
    BaseList MyList;
    Then use Mylist
    Code:
    Mylist.pushback(createABaseObject());
    EDIT: oops, and like anon mentions, you'll need an instance of base to call createABaseObject, or not have the function as a member. You might be able to just do Base::createABaseObject(), but don't take my word for it.
    Last edited by Syneris; 01-28-2010 at 03:34 AM.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Syneris View Post
    Wont he need to declare a variable of BaseList type?
    Code:
    BaseList MyList;
    Yes

    You might be able to just do Base::createABaseObject(), but don't take my word for it.
    Declare createABaseObject() as a static method and you can do Base::createABaseObject();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  2. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  3. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  4. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM
  5. Expression Manipulator v0.2 (bug fixes, functions)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-26-2003, 04:52 PM