Thread: Class acessing caller?

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Class acessing caller?

    Ok, I have a class function:

    Code:
    //Just an example
    class Pointless {
     private:
      int x;
     public:
      Pointless();
      int GetX() {return x;}
    
      Pointless ReturnSelf();
    };
    
    Pointless Pointless::ReturnSelf() {
     return Self;
    }
    Thats an example, I know I could inline ReturnSelf to :P.

    So, I just want to return whatever called ReturnSelf. Like:

    Code:
    int main() {
     Pointless a;
     Pointless *b;
    
     b = &a.ReturnSelf(); // Is this even valid expression? -,-.
    Once again, thats an example :P. But it would be the eqivalent of:

    Code:
    int main() {
     Pointless a;
     Pointless *b;
    
     b = &a;
    }
    Thank you for your help!

    [edit]
    Miswrote my example... -,-.
    Last edited by Blackroot; 02-05-2006 at 11:14 PM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Copy constructor.

    www.google.com

    If that isn't what you're looking for, then maybe someone else will get your question better.
    Last edited by SlyMaelstrom; 02-05-2006 at 11:07 PM.
    Sent from my iPadŽ

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Umm...? What do I copy when I dont know the name of what I'm attempting to copy...

    Code:
    class Pointless {
     private:
      int x;
     public:
      Pointless();
      int GetX() {return x;}
      Pointless(const Pointless &p) {x=p.GetX();}
    
      Pointless ReturnSelf();
    };
    
    Pointless Pointless::ReturnSelf() {
     return Self;
    }
    What? Once again, I'm trying to get the object that called it.

    Code:
    Pointless a;
    Pointless *b;
    
    b = &a.ReturnSelf();
    =
    Code:
    Pointless a;
    Pointless *b;
    
    b = &a;
    Last edited by Blackroot; 02-05-2006 at 11:14 PM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You're trying to copy a into b. What do you mean you don't know what you're attempting to copy?

    Code:
    #include <iostream>
    
    class Foo {
       public:
         Foo(int);           // The constructor
         Foo(const Foo&);    // The copy constructor
         ~Foo();
         
         int x;
    };
    
    Foo::Foo(int num) : x(num) {}
    Foo::Foo(const Foo& foo) { x = foo.x; }
    Foo::~Foo() {}
        
    int main() {
        Foo One(5);       // Calls the constructor
        Foo Two(One);     // Calls the copy constructor
        
        std::cout << Two.x;
        std::cin.get();
    
        return 0;
    
    }
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Maybe you want to use the this pointer?
    Code:
    Pointless *Pointless::ReturnSelf() {
     return this;
    }

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    How are you referring to the object calling the function there?
    Sent from my iPadŽ

  7. #7
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    I dunno, maybe this would do what you are trying, but I haven't compiled to double check.
    Code:
    &Pointless Pointless::ReturnSelf() {
     return &this;
    }
    Code:
    int main() {
     Pointless a;
     Pointless *b;
    
     b = a.ReturnSelf();

    As Quantum stated:

    the this keyword should return the caller that you are attempting to do.

    ------------------------------------------------------------------------------------------
    This section is if you are trying to make a copy, which I am unsure if this is what your intention ultimately is. So you can ignore this part if it doesn't help you.

    Note in your example:
    Code:
    int main() {
     Pointless a;
     Pointless *b;
    
     b = &a.ReturnSelf(); // Is this even valid expression? -,-.
    If this code works, which it might if you replace Self with the this pointer it still won't make a copy of a and store it in b, rather it just makes b point to a. So any changes to one will also be made to the other. b & a are now essentially one object b a.k.a. a.

    If you want a true copy you need a copy constructor like someone else mentioned.

    Code:
    // Note this is a public funtion in Pointless Class.
    
    void Pointless::Operator=(Pointless pIn)
    {
       this.x = pIn.x;
    }
    Code:
    int main()
    {
    Pointless a, b;
    b = a;
    }
    Something like this. Look up overloading the = operator or just use the word copy instead like:

    Code:
    // Note this is a public funtion in Pointless Class.
    
    void Pointless::copy(Pointless pIn)
    {
       this.x = pIn.x;
    }
    Code:
    int main()
    {
    Pointless a, b;
    b.copy(a);
    }
    P.S. Some of my code may not compile correctly, but the principles should be correct.

    Hope some, if any, of this helped.
    ----------------------------------------------
    By the way, how do you guys have signatures on your posts? I didn't see a sig. option anywhere. Is it because I haven't posted enough yet?
    Last edited by Kurisu; 02-06-2006 at 12:12 AM.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Oh, very nice. I've never seen that keyword. I suppose you can give that a try.
    Sent from my iPadŽ

  9. #9
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Ah thank you, I was trying to find out how to get at "this". Thanks .
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by SlyMaelstrom
    Oh, very nice. I've never seen that keyword. I suppose you can give that a try.
    If you've never come across the "this" pointer before I'll give a little explanation.
    When ever you call a function that is a member of a class the first argument which the compiler hides for you is the "this" pointer which points to the specific object who's member function is being called. Whenever you access a member variable you are actually using the "this" pointer but the compiler takes care of that for you so
    Code:
    a=1;
    is actually
    Code:
    this->a=1;

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Blackroot
    Ah thank you, I was trying to find out how to get at "this". Thanks .
    Why, though? What's wrong with &?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM