Passing Pointer To Class In A Function

This is a discussion on Passing Pointer To Class In A Function within the C++ Programming forums, part of the General Programming Boards category; Hey guys just another is this the right way to do it question. I like to make sure im doing ...

  1. #1
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810

    Passing Pointer To Class In A Function

    Hey guys just another is this the right way to do it question.
    I like to make sure im doing it right even if it seems to work
    Code:
    #include <iostream>
    class loser
    {
      public:
        int loserPower;
    };
    
    void modifyingLoser(loser *mLoser);
    int main()
    {
      loser me;
      loser *ptrMe;
      ptrMe=&me;
      modifyingLoser(ptrMe);
      std::cout<<ptrMe->loserPower;
      getchar();
      return 0;
    }
    void modifyingLoser(loser *mLoser)
    {
        mLoser->loserPower=10;
    }
    Thanks as Always
    Woop?

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    All looks good to me.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    Ok, thank you zach
    Woop?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It's easier to do this, but perhaps you need the pointer for some reason.
    Code:
      modifyingLoser(&me);
      std::cout<<me.loserPower;

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Another thing to consider, if you aren't wedded to the current version, is to make modifyingLoser() a public mutator method of the loser class and changing access of loserPower to private instead of public to protect it from unwanted changing by non-class functions.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    Quote Originally Posted by swoopy
    It's easier to do this, but perhaps you need the pointer for some reason.
    Code:
      modifyingLoser(&me);
      std::cout<<me.loserPower;
    Thank you swoopy that is much easier
    Woop?

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Oowie, something that I was having trouble with.
    I don't understand what the "->" operator does in classes/structures. Is it a balance of a condition, operator, or a statement and what does it do? Does it do anything related to the misunderstood "." operator? Or, does it have to do with pointers hence ("int x->e;" == "int * x = &e)?
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The arrow operator dereferences and accesses pointers. For example:
    Code:
    struct foo {
       int bar;
    };
    
    int main( )
    {
       foo* ptr = new foo;
       // The following lines are equivalent.
       ptr->bar = 5;
       (*ptr).bar = 5;
    }

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    19
    I hope it won't confuse you, but in C++, other than C, you must not use pointer for function parameters you want to change. Instead better use reference-parameters wherever you can.

    Code:
    #include <iostream>
    class loser
    {
      public:
        int loserPower;
    };
    
    void modifyingLoser(loser& mLoser);
    int main()
    {
      loser me;
      modifyingLoser(me);
      std::cout<<me.loserPower;
      getchar();
      return 0;
    }
    
    void modifyingLoser(loser& mLoser)
    {
        mLoser.loserPower=10;
    }
    mfg JJ

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    I guess its really a matter of taste because in the end its the same result as this
    Code:
    #include <iostream>
    class loser
    {
      public:
        int loserPower;
    };
    
    void modifyingLoser(loser *mLoser);
    int main()
    {
      loser me;
      modifyingLoser(&me);
      std::cout<<me.loserPower;
      getchar();
      return 0;
    }
    
    void modifyingLoser(loser *mLoser)
    {
        mLoser->loserPower=10;
    }
    I Think the -> is cooler loooking
    Woop?

  11. #11
    Registered User
    Join Date
    Dec 2003
    Posts
    4
    It would be better to initialize your loserPower integer before you work with it.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I generally pass a pointer only if the object I am dealing with could be NULL, I know that in the rest of my code I will be dealing with pointers rather than the objects themselves, or I'm going to need to cast up from a base class to do something specific to whatever derived class I happen to have passed (which is rare, and generally done away with through the use of virtual functions, but not always -- and it is more pleasant to check to see if a pointer is NULL, than catch an exception). Other than that, there is less you can accidently do with a reference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing class member function to pthread_create
    By lehe in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2009, 08:47 PM
  2. Passing a pointer to a member function
    By Potterd64 in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2006, 12:15 AM
  3. class passing a pointer to self to other class
    By daioyayubi in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2005, 10:25 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21