Jump to content
  • Advertisement
Sign in to follow this  

Operator Overloading Problem

This topic is 4121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I have a class that has an overloaded operator:
class ObjectModel
    ObjectModel(string file);
    ObjectModel& operator=(const ObjectModel &a)
        NumVertex = a.NumVertex;
        NumNormal = a.NumNormal;
        NumTexCoord = a.NumTexCoord;
        NumTriangle = a.NumTriangle;
        VertexArray = a.VertexArray;
        NormalArray = a.NormalArray;
        TexCoordArray = a.TexCoordArray;
        TriangleArray = a.TriangleArray;
        Filename = a.Filename;
        return *this;
    int NumVertex;
    int NumNormal;
    int NumTexCoord;
    int NumTriangle;
    ObjectVertex *VertexArray;
    ObjectNormal *NormalArray;
    ObjectTexCoord *TexCoordArray;
    ObjectTriangle *TriangleArray;
    string *Filename;

However, it is not working later when it is called:
ObjectLoader::ObjectLoader(int objects)
    ObjectArray = new ObjectModel[objects];
    for(int a=0; a < objects; a++)
        ObjectArray[a] = NULL; //Here
void ObjectLoader::LoadObject(int index)
    if(ObjectArray[index] != NULL) FreeObject(index); //Here
    ObjectArray[index] = new ObjectModel; //Here

Can someone help me weed out this problem? Thanks.

Share this post

Link to post
Share on other sites
None of these make much sense:

ObjectArray[a] = NULL;
if(ObjectArray[index] != NULL)
ObjectArray[index] = new ObjectModel;

From the looks of this:

ObjectArray = new ObjectModel[objects];

Each ObjectArray
  • is an instance of ObjectModel, not a pointer-to-ObjectModel. Value types usually have no concept of NULL, nor does it make sense to assign them to pointers to "new ObjectModel"s from the heap.

    Note that your overloaded operator= only does what the default operator= does as well (e.g. assign all individual objects).

    It sounds like you might want to be using:

    ObjectModel** ObjectArray = new ObjectModel*[index];. This causes that code to make sense, since you're dealing with pointers to ObjectModels now, but then the ObjectModel's operator= becomes entirely unrelated.

    Share this post

    Link to post
    Share on other sites
    As MaulingMonkey You overloaded only the ascription(?! don't know how to say it correct) operator. It mean that you can do something like this:
    Object a, b;
    //some operations with a
    b = a;

    If you want to get to specific element of object you should overload [] operator too.

    Share this post

    Link to post
    Share on other sites
    Sign in to follow this  

    • Advertisement

    Important Information

    By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

    We are the game development community.

    Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

    Sign me up!