Thread: Should I use -> or . ?

  1. #1
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92

    Should I use -> or . ?

    Hello all ... I am trying to learn C++ ... again (PHP is so much easier).

    Here is my question:

    Should I use the arrow operator to access class methods and the dot operator to access class members?

    Here is the code I am using:

    Code:
    #ifndef CREATURE
    
    class Creature {
      public:
      Creature(){
        std::cout << "Creating Creature" << std::endl;
      }
      
      ~Creature(){
        std::cout << "Destroying Creature" << std::endl;
      }
      
      void eat(){
        std::cout << "Creature is eating" << std::endl;
      }
      
      void setBloodType(char* bType){
        bloodType = bType;
      }
      
      char* getBloodType(){
        return bloodType;
      }
      
      protected:
        char* bloodType;
      
    };
    
    #define CREATURE 1
    #endif
    Code:
    #ifndef MAMMAL
    
    class Mammal : public Creature {
      
      public:
        
        Mammal(){
          std::cout << "Creating Mammal" << std::endl;
        }
    
        ~Mammal(){
          std::cout << "Destroying Mammal" << std::endl;
        }
        
        void eat(){
          std::cout << "Mammal is eating" << std::endl;
        }
      
    };
    
    #define MAMMAL 1
    #endif
    Code:
    #include<iostream>
    #include "Creature.cpp"
    #include "Mammal.cpp"
    
    using namespace std;
    
    int main(){
      
      Creature *creature = new Creature;
      char* CreatureBloodType = "Unknown";
      creature->setBloodType(CreatureBloodType);
      creature->eat();
      cout << "Creature blood type is " << creature->getBloodType() << endl;
      delete creature;
      
      char* MammalBloodType = "O Negative";
      Mammal *mammal = new Mammal;
      mammal->eat();
      mammal->setBloodType(MammalBloodType);
      cout << "Mammal blood type is " << mammal->getBloodType() << endl;
      delete mammal;
      
      system("PAUSE");
      return 0;
    }
    TIA!

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You use -> when the variable is a pointer to an object and . when the variable is the object.

  3. #3
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Come to think of it is there really any good reason for the existance of "->" and not using . for pointers too?

  4. #4
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    So I would (usually) only be using the dot operator within the classes?

    From what I have seen thus far, you instantiate an object as a pointer.

  5. #5
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by Kudose View Post
    So I would (usually) only be using the dot operator within the classes
    That really depends on what kind of variables you will operate on.

  6. #6
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Quote Originally Posted by Tux0r View Post
    Come to think of it is there really any good reason for the existance of "->" and not using . for pointers too?
    I am learning, obvisouly, but replacing the arrows with dots breaks everything ( as I am working with the object as a pointer )

    Could you please elaborate on this?

  7. #7
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by Kudose View Post
    I am learning, obvisouly, but replacing the arrows with dots breaks everything ( as I am working with the object as a pointer )

    Could you please elaborate on this?
    No, could you? I really don't see why they had to do this. And I have been programming in C++ for years.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Tux0r View Post
    Come to think of it is there really any good reason for the existance of "->" and not using . for pointers too?
    Keep in mind that pointers are a legacy from C.
    And pointers are variables that stores memory addresses, thus they can be reassigned and all that. So to access the members of an object pointed to by a pointer, you'd have to dereference the pointer first and then use the . operator.
    But since the . operator has higher precedence than the * operator, we have to prioritize it:
    (*p).mymember.
    This is obviously awkward syntax and even more so when there are several indirections, so they invented the -> syntax to replace it.

    Quote Originally Posted by Kudose View Post
    So I would (usually) only be using the dot operator within the classes?

    From what I have seen thus far, you instantiate an object as a pointer.
    Not everything needs to be created with new. In your code, there is no need.
    Just type the name: Creature creature; and there you have your object. No need for new, no need for delete.

    Also, you have mixed up source files and headers. Class declarations should go into headers (.h files). Implementation of the class should go into a source file (.cpp file).
    And never ever include .cpp files. They are compiled separately. Header files can be included.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Might not matter in this particular example as you're not using a generic Creature pointer to a Mammal object anywhere, but these:
    Code:
    class Creature {
      
      ~Creature(){
        std::cout << "Destroying Creature" << std::endl;
      }
      
      void eat(){
        std::cout << "Creature is eating" << std::endl;
      }
    ...should really be virtual.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by Tux0r View Post
    Come to think of it is there really any good reason for the existance of "->" and not using . for pointers too?
    Yes.

  11. #11
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    I am digesting the info above, but before I forget: I spy a Weedpacket.

  12. #12
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Quote Originally Posted by Elysia View Post
    Keep in mind that pointers are a legacy from C.
    And pointers are variables that stores memory addresses, thus they can be reassigned and all that. So to access the members of an object pointed to by a pointer, you'd have to dereference the pointer first and then use the . operator.
    But since the . operator has higher precedence than the * operator, we have to prioritize it:
    (*p).mymember.
    This is obviously awkward syntax and even more so when there are several indirections, so they invented the -> syntax to replace it.
    That helps a lot.



    Quote Originally Posted by Elysia View Post
    Not everything needs to be created with new. In your code, there is no need.
    Just type the name: Creature creature; and there you have your object. No need for new, no need for delete.
    In which instances would I want to create an object using new?

    Quote Originally Posted by Elysia View Post
    Also, you have mixed up source files and headers. Class declarations should go into headers (.h files). Implementation of the class should go into a source file (.cpp file).
    And never ever include .cpp files. They are compiled separately. Header files can be included.
    Yes, I just didn't do this for this small learning task. I would include the cpp file in the h (header) file and include the header file in my main code ... correct?

  13. #13
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Quote Originally Posted by hk_mp5kpdw View Post
    Might not matter in this particular example as you're not using a generic Creature pointer to a Mammal object anywhere, but these:
    Code:
    class Creature {
      
      ~Creature(){
        std::cout << "Destroying Creature" << std::endl;
      }
      
      void eat(){
        std::cout << "Creature is eating" << std::endl;
      }
    ...should really be virtual.
    Am I correct in thinking that a virtual method allows it to be overridden by derived classes?

    So, if I created Dog class, the Creature and Mammal eat/destructor methods should be virtual?

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Keep in mind that pointers are a legacy from C.
    And pointers are variables that stores memory addresses, thus they can be reassigned and all that. So to access the members of an object pointed to by a pointer, you'd have to dereference the pointer first and then use the . operator.
    But since the . operator has higher precedence than the * operator, we have to prioritize it:
    (*p).mymember.
    I think the question was more along the lines of, the compiler does in fact know whether the variable is a pointer or an object, so why can't it apply an implicit dereference if it's a pointer? Then we would say "obj.foo()" and "ptrToObj.foo()" in either case. In other words, if the LHS is a pointer, magically transform "." into "->".

    After thinking a bit, I can't come up with a compelling reason why it couldn't have been done that way. At first I thought it might negatively impact code clarity, by making it impossible to tell at a glance whether the LHS is a pointer or an object. But if you accept that argument, then you should also accept that C++ references are equally confusing because they "magically" dereference themselves wherever they are used, and are difficult to distinguish in precisely the same way.

    I happen to think that C++ references are NOT confusing, and similarly, I don't think it would be confusing to have a single operator for member access in both objects and pointers, but that just isn't how it is, and it is never going to change.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Kudose View Post
    In which instances would I want to create an object using new?
    Objects created that way I showed are created on the stack and disappear whenever the function ends. So if you want the objects to last longer than that, then the new keyword is the answer. It's called dynamic memory.

    Yes, I just didn't do this for this small learning task. I would include the cpp file in the h (header) file and include the header file in my main code ... correct?
    No, you never include any .cpp files. The compiler will compile all of those.
    You are correct that you include the header in your main code.

    Quote Originally Posted by Kudose View Post
    Am I correct in thinking that a virtual method allows it to be overridden by derived classes?

    So, if I created Dog class, the Creature and Mammal eat/destructor methods should be virtual?
    This is a topic called polymorphism. If you wanted to store a pointer or reference to a Mammal via a Creature* or Creature& variable, then you can make certain functions virtual. By doing so, the compiler looks up the actual type of the object and call the right function.
    Try this code with and without virtual before eat (in both classes) and look at the results.
    Code:
    Mammal m;
    Creature& c = m;
    m.eat();
    There is usually a rule that says that when you have a virtual function in a class, the destructor of the class should also be virtual. The reason is because of the same code snippet above.

    Quote Originally Posted by brewbuck View Post
    I think the question was more along the lines of, the compiler does in fact know whether the variable is a pointer or an object, so why can't it apply an implicit dereference if it's a pointer? Then we would say "obj.foo()" and "ptrToObj.foo()" in either case. In other words, if the LHS is a pointer, magically transform "." into "->".

    After thinking a bit, I can't come up with a compelling reason why it couldn't have been done that way. At first I thought it might negatively impact code clarity, by making it impossible to tell at a glance whether the LHS is a pointer or an object. But if you accept that argument, then you should also accept that C++ references are equally confusing because they "magically" dereference themselves wherever they are used, and are difficult to distinguish in precisely the same way.

    I happen to think that C++ references are NOT confusing, and similarly, I don't think it would be confusing to have a single operator for member access in both objects and pointers, but that just isn't how it is, and it is never going to change.
    The way I see it, it's the C legacy that created that. Now, why C never did this is another matter. But I suppose it's just to separate pointers from pointees, some language thingy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. small -> big -> bigger -> bigger than bigger -> ?
    By happyclown in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-11-2009, 12:12 PM
  2. Dev-C++ -> Tools - > Editor -> Syntax
    By Yuri2 in forum C++ Programming
    Replies: 19
    Last Post: 07-03-2006, 07:48 AM
  3. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 02:06 PM
  4. electricity > AC circuits > tesla coil
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 03-14-2002, 02:16 PM