CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Mar 2008
    Posts
    31

    Creating objects with functions.

    Hi,

    I am making a console program which has a menu, for example:

    Enter your choice and press Enter:
    1: Add a new vehicle
    2: Delete a vehicle
    3: Search for a vehicle
    I then have a switch in main, if the user selects 1, I call a function and create a new object.

    I'm wondering, once the object is created within the function, how best to enter in into a vector.

    Can/should I return the object back to main, and then push it onto the vector? Or can I push it onto the vector straight from within the function?

    Also I have a few different classes in the program, types of cars and motorbikes.

    I'm new to vectors (this program will have to use a doubly linkedlist eventually, but thought I'm using vectors first to just get the program working. I hope it wont be an issue converting the container types, perhaps it will just mean adding another pointer to each vector?

    But should I have one vector for each type of class? I cannot use one sigle vector for all the different class types, can I? The different classes hold differently named member variables.

    Sorry for the 20 questions.

    Thank you very much!

  2. #2
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: Creating objects with functions.

    Hmm, I have been using C++ for 25 years, and I don't know what a "vector" is. Must be a pretty obscure part of the language.

    You seem to be making things way more complicated than they need to be. KISS.

    Create a common vehicle class, derive motorcycles, cars, trucks, SUVs, etc classes from it, do a new on whichever object it is then call common members.

    -Erik

  3. #3
    Join Date
    Mar 2008
    Posts
    31

    Re: Creating objects with functions.

    I have come a little further along now and was hoping someone could spot where my mistake is.

    After calling a constructor inside a global function, I tried to return this newly existing object to main, where I will enter it into a vector.

    But I have two problems.

    The program will not compile, error says "always get 'tmp' : undeclared identifier"

    I am unable to return the object to main.

    This is a sample of the header file:

    Header file:

    Code:
    class Cars: public Vehicles
    {
    public:
    
    	int MPG;
    
    private:
    
    protected:
    
    };

    From part of the function in main.cpp:

    Code:
    switch ( choice )
    {
    
    case 1:
    	cout << "Before creating an object of type car" << endl;
    	int x = 1;
    	if(x >= 1)
    	{	
    
    				
    		cars temp;    //construct an object called 'temp' of type cars
    
    
    
    		cout << "Enter the MPG" << endl;
    		cin >> temp.MPG;
    
    
    	}
    
    	cout << "After creating an object of type car" << endl;
    
            return temp;//always get 'tmp' : undeclared identifier, but I have called the constructor earlier
    
            //other ways I have tried to return the object back to main, always the same error 
    	//return *temp;
    	//return &temp;
    	//return *temp;	
    
    		--x;
    }
    And I want to return the newly created object back to main, so that I can then enter it into a vector. As it is a global function, I cannot use *this to return the object say's the compiler.

    Much thanks to anyone who can point me in the wrong direction

  4. #4
    Join Date
    Mar 2008
    Posts
    31

    Re: Creating objects with functions.

    Hmm, I have been using C++ for 25 years, and I don't know what a "vector" is. Must be a pretty obscure part of the language.
    Yikes!

    I mean the container type, a 'variable length array' .

    Plan to hold all of the car objects within their own vector. Reading about the STL says I can use a type T template, and then when objects are created, replace the T with the class type.


    EDIT -

    Going to try a globally declared function which will overload the >> operator so that I could use 'temp' to construct any class type.

    Need to find out how to declare a globally overloaded operator.

    I would try to just return a reference or pointer to the object, but want to also hold the object's data with in text file, so will return the new object to main, them both push a copy onto the vector, and also send a copy to the text file.

    I wonder that if I just return a reference or a pointer, the object will be destroyed, so stopping me from deleting/modyfing them as the user wishes.
    Last edited by Swerve; March 20th, 2009 at 11:04 AM.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Creating objects with functions.

    And I want to return the newly created object back to main, so that I can then enter it into a vector. As it is a global function, I cannot use *this to return the object say's the compiler.
    The problem is that you create temp in the scope of the if statement, but attempt to return it after the if statement. Looking at the context, it seems that you want to write:
    Code:
    switch (choice)
    {
    case 1:
        {
            cout << "Before creating an object of type car" << endl;
            int x = 1;
            if (x >= 1)
            {
                cars temp;    //construct an object called 'temp' of type cars
    
                cout << "Enter the MPG" << endl;
                cin >> temp.MPG;
    
                cout << "After creating an object of type car" << endl;
                return temp;
            }
            --x;
        }
    }
    Frankly, the variable x seems not very useful here.

    Quote Originally Posted by Swerve
    I mean the container type, a 'variable length array' .
    Maybe egawtry just means that you should be more precise and say std::vector instead of vector, though personally I feel that it could be deduced from context (then again, such assumptions can lead to miscommunication).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Mar 2008
    Posts
    31

    Re: Creating objects with functions.

    Oh wow

    That has just totally fixed the error, I was expecting the use of return to not be possible at all.

    I was thinking I'd have to use operator overloading instead.

    Thanks for all the great help!!

    Appreciated.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Creating objects with functions.

    Quote Originally Posted by egawtry View Post
    Hmm, I have been using C++ for 25 years, and I don't know what a "vector" is. Must be a pretty obscure part of the language.

    -Erik
    I really hope that's a joke.

  8. #8
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: Creating objects with functions.

    Quote Originally Posted by GCDEF View Post
    I really hope that's a joke.
    In a way. It is obscure, I haven't used one since 1986 when I first learned C++. I realized what is was after writing the reply. I have been using C++ in primarily Windows development since 1991, so I normally use CPtrList or CObList.

    -Erik

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Creating objects with functions.

    STL classes aren't obscure if you use them a lot (which many people do). I suppose there aren't that many ways to encapsulate a dynamic array, so one is probably as good as another.

    Don't know MFC (I presume that's what those come from?) well enough to judge it relative to the STL approach. Does it have equivalents of all the standard containers (vector, list, double-ended queue, map, set, string)? Does it have generic algorithms which can be applied to any of them and automatically template-specialize on some for more efficient operation? Does it perchance have the one thing STL does not (quite yet) have----a hash table container?
    Last edited by Lindley; March 21st, 2009 at 01:19 AM.

  10. #10
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: Creating objects with functions.

    Quote Originally Posted by Lindley View Post
    Does it perchance have the one thing STL does not (quite yet) have----a hash table container?
    Yes. MFC has CMap with Hash members and a template class HashKey.

    MFC has been around for so long that it has everything if you just dig a bit. ;-)

    -Erik

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Creating objects with functions.

    Quote Originally Posted by egawtry View Post
    Yes. MFC has CMap with Hash members and a template class HashKey.

    MFC has been around for so long that it has everything if you just dig a bit. ;-)

    -Erik
    MFC does not have algorithms or generic iterators. For example, where is the "Sort" for a CArray? The funny thing is that the STL algorithms such as "sort" work flawlessly when used on an MFC CArray.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: Creating objects with functions.

    Quote Originally Posted by Paul McKenzie View Post
    MFC does not have algorithms or generic iterators. For example, where is the "Sort" for a CArray? The funny thing is that the STL algorithms such as "sort" work flawlessly when used on an MFC CArray.
    That is because you can do a simple qsort without much difficulty. Why complicate things with STL?

    I bet you are one of those who tries to pass C++ objects from a DLL. :-)

    -Erik

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Creating objects with functions.

    Quote Originally Posted by egawtry
    That is because you can do a simple qsort without much difficulty.
    I had the impression that qsort is not guaranteed to correctly sort non-POD objects, even when a correct comparator function is supplied.

    Quote Originally Posted by egawtry
    Why complicate things with STL?
    We can change the question to ask: where is stable sort, partial sort and (partitioning according to the) nth element for CArray?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Creating objects with functions.

    Quote Originally Posted by egawtry View Post
    That is because you can do a simple qsort without much difficulty.
    There is no guarantee that qsort will correctly copy non-POD objects when it decides to move things around.
    Why complicate things with STL?
    Have you seen the difference between a call to qsort() and a call to std::sort? Here is how std::sort works with an array of integer:
    Code:
    #include <algorithm>
    
    int array[100];  
    //...
    int main()
    {
       //... Assume that array has been populated
       //...
       std::sort(array, array + 100);
    }
    That's all. Nothing else.

    With qsort()
    Code:
    #include <stdlib.h>
    int array[100];
    //...
    int compare (const void * a, const void * b)
    {
        return ( *(int*)a - *(int*)b );
    }
    
    int main ()
    {
       //... Assume that array has been populated
       //...
        qsort (array, 100, sizeof(int), compare);
    }
    I leave it to others to determine which is easier.
    I bet you are one of those who tries to pass C++ objects from a DLL. :-)
    Nope. My company creates DLL's, so we know that the only safe things to pass are POD types or pointers. However, the internals of the DLL uses STL.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Creating objects with functions.

    Quote Originally Posted by laserlight View Post
    We can change the question to ask: where is stable sort, partial sort and (partitioning according to the) nth element for CArray?
    You can also mention next_permuation, max_element, min_element, partition, set_difference, set_intersection, lower_bound, transform, etc. etc. Either you have to write all of these functions for CArray, or just use these function on the CArray itself. That's the beauty of the algorithm functions -- these algorithms work regardless of whether you're using them on MFC containers (CArray mostly) or not.

    That's why whenever there is a discussion between MFC or STL, I always mention that STL isn't just containers. There are functions that work on sequences of data, regardless of whether that sequence is a regular array, an MFC CArray, a std::vector, or your own home-made array class.

    Regards,

    Paul McKenzie

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured