crazy = and [] operator overloading question

This is a discussion on crazy = and [] operator overloading question within the C++ Programming forums, part of the General Programming Boards category; I have two structs: Code: struct FaceVertex { float u; float v; int vertex; }; struct IntermediateFace { int *vertices; ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    17

    crazy = and [] operator overloading question

    I have two structs:

    Code:
       struct FaceVertex {
          float u;
          float v;
          int vertex;
       };
    
       struct IntermediateFace {
          int *vertices;
          float *u;
          float *v;
          int numVertices;
    
          FaceVertex& operator[] (int index);
          IntermediateFace& operator=(FaceVertex vertex);
       };
    is it possible to write a [] and = operator for IntermediateFace so that I can do this:
    IntermediateFace face;
    FaceVertex vertex;

    //set values here

    face[0] = vertex;

    where [] accesses the ith element of the three pointer elements in IntermediateFace and = sets the pointer elements to the elements of FaceVertex?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    No. The most obvious way to do what you want is not via assignment operator[]() or operator=(). It is to provide an additional member function of IntermediateFace that sets this up for you.

    Code:
         void SetVertex(int index, FaceVertex vertex)
         {
               // assume vertices array is set up so this is valid
               vertices[index] = vertex;
               u = &(vertices[index].u);
               v = &(vertices[index].v);         
         }
    Generally, I'd suggest you rethink your design rather than doing this. Having an object contain pointers to subobjects implies some pretty strange dependencies ..... and difficulty gettin the various objects playing together nicely.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    17
    I didn't think it would work. I was playing around with it and it wasn't quite working.
    I could just do what you said either with a function of just directly.

    I think you misunderstood my design.

    Basically I need to read in three vertex uv sets from a file. So I do this by reading them in a loop into a FaceVertex and then copying the vertex, u and v to the ith element of IntermediateFace's vertices, u and v arrays respectively.

    It's just so that my function that reads the vertex is simpler and then I can read in a variable number of vertices for a face.

    Just to make you cringe a bit, this is also in a class, so my prototypes are starting to look like
    Code:
    ParsePFModel::FaceVertex& ParsePFModel::IntermediateFace::operator[]
    (not that I'm using that one anymore, but you get the point).

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by jarro_2783

    is it possible to write a [] and = operator for IntermediateFace so that I can do this:
    IntermediateFace face;
    FaceVertex vertex;

    //set values here

    face[0] = vertex;

    where [] accesses the ith element of the three pointer elements in IntermediateFace and = sets the pointer elements to the elements of FaceVertex?
    You should be able to accomplish this. To continue on the current path you may need to create a third object type, which stores either pointers or references though, so your temporary object can modify the IntermediateFace that it comes from.

    Whatever type your return value from operator[] is, though, determines which class your operator= will be in. For example, if face[0] returns a FaceVertex&, then the = will be FaceVertex::operator=(), not IntermediateFace::operator=().

    Although, in the end, you'd be much better off redesigning this -- particularly the use of raw pointers, and multiple pointers to boot. Why not a std::vector<FaceVertex> ?

    That would also make the operator[] incredibly simple, and operator= will be unnecessary as the compiler-generated version will work just fine.
    Last edited by Cat; 09-15-2006 at 03:38 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    Cat, the original question is whether he could do it with operator=() and operator[] for IntermediateFace. And the answer to that remains "no". Particularly as the example assignment "face[0] = vertex;" does not invoke operator=() for IntermediateFace. It would be possible to do something like he wants by monkeying with the assignment operator of FaceVertex, if each FaceVertex contained a pointer to its container and that pointer was initialised BEFORE trying to do the assignment. That would be extremely poor design in many ways (as the sense it would allow users of the classes to introduce errors if they forget to do something before the assignment).

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    17
    don't worry, it's more effort than it's worth even if it is possible. I get what you're saying about a third object storing references, that would probably work. But I can just do it with a function that assigns the value. In fact I've already done it and it was fine and didn't take lots of code and didn't involve making new objects and doing wierd things with operators.

    I guess I could use vectors and push_back instead of pointers and dynamic arrays, but how slow is that? Speed isn't on the top of the list, but it is fairly important.
    Last edited by jarro_2783; 09-15-2006 at 07:20 PM.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by jarro_2783
    I guess I could use vectors and push_back instead of pointers and dynamic arrays, but how slow is that? Speed isn't on the top of the list, but it is fairly important.
    In general the vector will be FASTER than the array. It will be a tiny, tiny bit slower on the initial creation (we're talking less than a microsecond) but in general, because of its smart sizing (where it actually allocates more space than it needs) it will need to reallocate fewer times when expanding the array (either that, or you'll be adding code and variables to replicate its actions, and in consequence your own implementation will end up having the same speed as vector).

    Anything except for the initial creation should, on a good compiler, be as fast as an array. The vector class was designed as a replacement for arrays, and an important part of that design is efficiency.

    And I didn't say vectors, I said vector. You only need one, unless there's a solid reason to do otherwise.

    Code:
    class IntermediateFace{
    private:
        std::vector<FaceVertex> data;
    public:
        FaceVertex& operator[](int i) { return data[i]; }
    };
    That would be all you'd need to replace the code you showed, and it would allow your original syntax idea to work.
    Last edited by Cat; 09-15-2006 at 07:51 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    17
    well that's handy to know.

    The pointers will only ever be an array of three or four elements. I'm allocating that with a function (setNumVertices) which allocates the pointers. Then in a loop I read the data from the file into FaceVertex then add it into the current position using another function (that was the one I used instead of my operator thing).

    So using a vector I would just push_back as it reads them in and I can then use your definition of the struct.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help on understandind arrays
    By C++mastawannabe in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2007, 10:50 PM
  2. Replies: 9
    Last Post: 07-07-2006, 11:03 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  5. Maths Question about Braces ({[]})
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-30-2004, 10:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21