Get pointer of object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike

    #1

    Get pointer of object

    How do I get a pointer of an object?

    e.g.

    std::string s;

    How do I get a pointer to s so I can use s->Method();


  • Artie Gold

    #2
    Re: Get pointer of object

    Mike wrote:[color=blue]
    > How do I get a pointer of an object?
    >
    > e.g.
    >
    > std::string s;
    >
    > How do I get a pointer to s so I can use s->Method();
    >
    >[/color]
    And what, may I ask, is wrong with s.Method()?

    HTH,
    --ag

    [Use `&', the address-of operator if a pointer is what you *really*
    want. In the above case you don't.]

    --
    Artie Gold -- Austin, Texas
    I will (ir)regularly write about things that are important to me -- that I hope interest you. Of course, since I see most things as being political, that will be most of it.

    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"

    Comment

    • Mike

      #3
      Re: Get pointer of object

      "Artie Gold" <artiegold@aust in.rr.com> wrote in message
      news:3umr6pF11k g7hU1@individua l.net...[color=blue]
      > Mike wrote:[color=green]
      >> How do I get a pointer of an object?
      >>
      >> e.g.
      >>
      >> std::string s;
      >>
      >> How do I get a pointer to s so I can use s->Method();[/color]
      > And what, may I ask, is wrong with s.Method()?[/color]

      Because I'm not really using std::string (using my own class) - I want to
      access the pointer it returns from another class.


      Comment

      • Artie Gold

        #4
        Re: Get pointer of object

        Mike wrote:[color=blue]
        > "Artie Gold" <artiegold@aust in.rr.com> wrote in message
        > news:3umr6pF11k g7hU1@individua l.net...
        >[color=green]
        >>Mike wrote:
        >>[color=darkred]
        >>>How do I get a pointer of an object?
        >>>
        >>>e.g.
        >>>
        >>>std::strin g s;
        >>>
        >>>How do I get a pointer to s so I can use s->Method();[/color]
        >>
        >>And what, may I ask, is wrong with s.Method()?[/color]
        >
        >
        > Because I'm not really using std::string (using my own class) - I want to
        > access the pointer it returns from another class.
        >
        >[/color]

        Huh? `Pointer it returns from another class?'

        In any event, the answer remains the same as before -- which you elided:

        [Use `&', the address-of operator if a pointer is what you *really*
        want. In the above case you don't.]

        HTH,
        --ag

        --
        Artie Gold -- Austin, Texas
        I will (ir)regularly write about things that are important to me -- that I hope interest you. Of course, since I see most things as being political, that will be most of it.

        http://www.cafepress.com/goldsays
        "If you have nothing to hide, you're not trying!"

        Comment

        • David White

          #5
          Re: Get pointer of object

          Artie Gold wrote:[color=blue]
          > Mike wrote:[color=green]
          >> Because I'm not really using std::string (using my own class) - I
          >> want to access the pointer it returns from another class.
          >>
          >>[/color]
          >
          > Huh? `Pointer it returns from another class?'
          >
          > In any event, the answer remains the same as before -- which you
          > elided:
          >
          > [Use `&', the address-of operator if a pointer is what you *really*
          > want. In the above case you don't.][/color]

          Well, '&' would be wrong to use on a pointer returned from somewhere, though
          it might be necessary for the thing that returns it.

          To the OP, how to make a pointer:
          MyClass ob;
          MyClass *pOb = &ob;
          pOb->Method;

          If the pointer is returned from somewhere, then just use it:
          MyClass *pOb = ob2.GetMyClassP ointer();
          pOb->Method();

          DW


          Comment

          • Mike Wahler

            #6
            Re: Get pointer of object


            "Mike" <mikestanworth@ gmailNOSPAM.com > wrote in message
            news:uBqhf.4114 $aU5.2403@newsf e3-win.ntli.net...[color=blue]
            > How do I get a pointer of an object?
            >
            > e.g.
            >
            > std::string s;
            >
            > How do I get a pointer to s so I can use s->Method();[/color]

            You cannot write 's->Method()' for two reasons:
            's' is not a pointer, and 'std::string' has no
            member function named 'Method()'.

            Obtain a pointer to a std::string object the same
            as for any other type of object; with the address
            (&) operator.

            #include <iostream>
            #include <string>

            int main()
            {
            std::string s("Hello");
            std::string *p = &s;
            std::cout << p->size() << '\n'; /* prints 5 */
            return 0;
            }

            Whether or not you really need this pointer I cannot
            say without more context, but I suspect you don't.

            -Mike


            Comment

            Working...