Passing pointer as parmaneter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • randysimes
    New Member
    • Oct 2009
    • 54

    #1

    Passing pointer as parmaneter

    I have to pass a pointer as a parameter, but I can't quite get the syntax down.

    I have this variable:
    std::vector<Foo > fooVector;

    And this function of another class, Foo
    void Add (Foo * N)

    So I need to pass fooVector_[size_t] to the Add function.

    I have tried this:
    Foo foo;
    foo.Add(foo.foo Vector_[size_t]) but get the error (shortened) Needs class::Foo* not class::Foo&

    Could you please point* me in the right direction.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    fooVector is implemented as an array of foo. Therefore, fooVector[size_t] is a foo. Therefore, &fooVector[size_t] is a foo*.

    Comment

    • randysimes
      New Member
      • Oct 2009
      • 54

      #3
      Thanks so much, weaknessforcats !

      Comment

      • randysimes
        New Member
        • Oct 2009
        • 54

        #4
        So, say that the void Add(Foo * N) function has access to a private variable that is a list and I want to add more than one item at any given item in the list. For instance, I want to add a North, and South to item 0 in the list, how can I do this?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Call the function twice, unless you are able to write a function with more semantic meaning such as

          Code:
          void Foo::AddExits(Foo* northExit, Foo* southExit, Foo* eastExit, Foo* WestExit);

          Comment

          • randysimes
            New Member
            • Oct 2009
            • 54

            #6
            Banfa, thanks, but I was going in the wrong direction with that.
            However, say I have
            Code:
            class Foo
            {
              Bar * start;
              start = &fooVector[size_t]
            ...
            }
            class Bar
            {
            ...
            }
            How can I output the value of start? I did not overload the operator << so I cannot cout << *start

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              You would need to call methods on class Bar that return the values you are interested in.

              Comment

              Working...