Jump to content
  • Advertisement
Sign in to follow this  
OrenGL

operator overloading

This topic is 5460 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

I'd like to overide operator-> so that I can call the funcitons of the object it contains. But I'd like this to work for a pointer to the obejct . Example:
class Obj {
public:
  void f() const {}
};

class TestOp {
 Obj* m_obj;
public:
  TestOp() : m_obj(0) {
  }
  Obj* operator->() const {
    return m_obj;
  }
};

int main(){
    TestOp op1;
    op1->f();				<-- This is what works now...
    TestOp* op2 = new TestOp();
    op2->f();				<-- HOW TO MAKE THIS LINE WORK?
    delete op2;
}







Thanks

Share this post


Link to post
Share on other sites
Advertisement
Aside from that, you could maybe do

op2->operator->()->f()

But that's disgusting.

That said though, how can you even do that? I've seen that before and I think I'm seriously missing something. Using the -> operator to return a pointer to a member doesn't change the fact that it's also acting as a dereference/object access operator? I would think it'd have to be something funky like op2->->f().

If someone could explain that one to me, I'd greatly appreciate it.

-Auron

Share this post


Link to post
Share on other sites
You can't overload operators purely on built-in types, and pointers are built-in types. Can-not-do-this.

What are you trying to be able to do? The only place you'd normally overload operator-> is for non-pointer types that pretend they're pointers, like smart pointers and iterators.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
You can't overload operators purely on built-in types, and pointers are built-in types. Can-not-do-this.

What are you trying to be able to do? The only place you'd normally overload operator-> is for non-pointer types that pretend they're pointers, like smart pointers and iterators.


lol, thats what happens when you get too much into implementation and forget what you started out doing. Just had a silly design, was trying to wrap a pointer with another pointer.

Thanks anyway, at least now I'm sure it can't be done :P

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!