Jump to content

  • Log In with Google      Sign In   
  • Create Account


Help with weird code


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 noatom   Members   -  Reputation: 769

Like
0Likes
Like

Posted 06 April 2013 - 07:34 AM


#include <iostream>
#include <vector>

using namespace std;

class Obj {
  static int i, j;
public:
  void f() const { cout << i++ << endl; }
  void g() const { cout << j++ << endl; }
};

// Static member definitions:
int Obj::i = 47;
int Obj::j = 11;

// Container:
class ObjContainer {
  vector<Obj*> a;
public:
  void add(Obj* obj) { a.push_back(obj); }
  friend class SmartPointer;
};

class SmartPointer {
  ObjContainer& oc;
  int index;
public:
  SmartPointer(ObjContainer& objc) : oc(objc) {
    index = 0;
  }
  // Return value indicates end of list:
  bool operator++() { // Prefix
    if(index >= oc.a.size()) return false;
    if(oc.a[++index] == 0) return false;
    return true;
  }
  bool operator++(int) { // Postfix
    return operator++(); // Use prefix version
  }
  Obj* operator->() const {
   
    return oc.a[index];
  }
};

int main() {
  const int sz = 10;
  Obj o[sz];
  ObjContainer oc;
  for(int i = 0; i < sz; i++)
    oc.add(&o[i]); // Fill it up
  SmartPointer sp(oc); // Create an iterator
  do {
    sp->f(); // Pointer dereference operator call
    sp->g();
  } while(sp++);
} ///:~

 

The code compiles,in case you want to know.Now,the problem:

when the function for the overloaded -> is called,it returns a pointer.I don't understand how an UNDEREFERENCED pointer can acces a function.

 

sp->g();

right there,the -> function is called,it returns a pointer and it immediatly has acces to functions,why? Shouldn't it be dereferenced first? Or it got dereferenced somewhere else,but I can't see where?



Sponsor:

#2 Juliean   GDNet+   -  Reputation: 2244

Like
0Likes
Like

Posted 06 April 2013 - 07:41 AM

Did you get this code from here? If so, the answer is right at the top. If not, let me quote:

 

The dereferencing operator-> can be defined as a unary postfix operator. That is, given a class:

 

class Ptr{
//...
X * operator->();
};
 

 

Objects of class Ptr can be used to access members of class X in a very similar manner to the way pointers are used. For example:

 

void f(Ptr p )
{
p->m = 10 ; // (p.operator->())->m = 10
}
 

The statement p->m is interpreted as (p.operator->())->m. Using the same concept, following example explain how a class access operator -> can be overloaded.



#3 SiCrane   Moderators   -  Reputation: 9407

Like
0Likes
Like

Posted 06 April 2013 - 07:42 AM

When the -> operator is overloaded the returned value is then used as the left hand side of another application of -> with the previous right hand side. This is why -> is overloaded as a unary operator rather than a binary operator. So your smart pointer returns a pointer and that pointer then has -> applied to it with f() or g() on the right hand side.

#4 noatom   Members   -  Reputation: 769

Like
0Likes
Like

Posted 06 April 2013 - 07:51 AM

ok,this is the point where i say: i'm on the weird side of the internet again :)

 

it's kinda weird,the return value of -> is then used with -> ...oh






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS