C++ Question. Operator overloading

Discussion in 'OT Technology' started by Swerve, Mar 20, 2009.

  1. Swerve

    Swerve OT Supporter

    Joined:
    Feb 20, 2005
    Messages:
    3,175
    Likes Received:
    3
    C++ Question. datatypes for retuning objects?

    Hi,



    I'm calling a constructor inside a global function, then trying to return the newly existing object back to main.

    But I have a problem choosing the functions return type, as it's an object being returned.

    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;
    
            {         
                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;
    
    
    }
    
    
    I would try to just return a reference or pointer to the object, but I want to:

    Copy the object's data values into a text file.

    Store the object within a vector or a doubly linked list.

    I'm thinking that if I just return a reference or a pointer, the object will be destroyed when the function call is completed.

    So I'm wondering if anyone can explain how to return an object from a function, the 'datatype' where void can go.

    Thanks!
     
    Last edited: Mar 20, 2009
  2. Swerve

    Swerve OT Supporter

    Joined:
    Feb 20, 2005
    Messages:
    3,175
    Likes Received:
    3
    OK, I'm stuck with this:



    Header file:


    The function from main.cpp:

    Now I think I've returned the object to main() inside 'temp' and want to enter it into a Vector, but when I try:

    So basically, I think I've created and returned a new object of type Cars, but cannot store it inside the vector vector1 due to:


    I have included in main.cpp:

    If anyone could explain where I might be going wrong it would be most appreciated.

    Many thanks!!

    :)
     
    Last edited: Mar 20, 2009
  3. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    if functionName() is the function returning the object, then you need another object variable that receives the returned info.

    cars temp = functionName();
     
  4. Swerve

    Swerve OT Supporter

    Joined:
    Feb 20, 2005
    Messages:
    3,175
    Likes Received:
    3
    Thanks skinjob :)

    Could I copy the object 'temp' into a global variable and then use the normal assignment operator?

    How would I declare the variable/datatype since it's an object?
     
  5. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    What you have is fine. You get an overloaded operator= for free when you build a class. It only does shallow copy, but for the simple class you posted, shallow copy is sufficient. As far as the variable declaration, you just declare it as the class name.
     

Share This Page