Thread: dot operator vs arrow.

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    16

    dot operator vs arrow.

    Hello everyone. I'm sure this has been asked multiple times and googling I've found plenty of explanations. However, some things just don't come to me unless it's explained with my specific example. I understand that the dot operator is:

    Code:
    a.b
    "a" is the actual object (not a memory location) and "b" is the member. I also understand the arrow to mean:

    Code:
    a->b
    "a" is a pointer to a struct and "b" is it's member so "a" is dereferenced then "b" is given.

    Here is where I get a little confused. I have a class:

    exampleclass.h
    Code:
    #ifndef EXAMPLECLASS_H_INCLUDED
    #define EXAMPLECLASS_H_INCLUDED
    
    class exampleclass{
    
        private:
            int number;
    
        public:
            exampleclass();  //Constructor
            ~exampleclass(); //Destructor
            void changeNumber(int number);
            int getNumber();
    
    
    };
    
    #endif // EXAMPLECLASS_H_INCLUDED
    exampleclass.cpp
    Code:
    #include "exampleclass.h"
    
    exampleclass::exampleclass(){
            this.number = 0;
    }
    
    exampleclass::~exampleclass(){
             
    }
    
    void exampleclass::changeNumber(int number){
           this.number = number;
    }
    
    int exampleclass::getNumber(){
           return this.number;
    }
    and last I have my main client code.

    Code:
    #include <iostream>
    #include "exampleclass.h"
    
    int main(){
           exampleclass ec1 = new exampleclass;
           //This is where I'm confused. Is ec1 my object
           //or is ec1 a pointer to the address of my object
          ec1.changeNumber(5); //Is this correct
          ec1->changeNumber(9); //or this?
    
    }
    Also if ec1 had a public variable and I wanted to access it would it be referenced the same way as the method I call in ec1?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is an error:
    Code:
    exampleclass ec1 = new exampleclass;
    It should be:
    Code:
    exampleclass* ec1 = new exampleclass;
    upon which it should become apparent that you want:
    Code:
    (*ec1).changeNumber(9);
    or equivalently and preferably:
    Code:
    ec1->changeNumber(9);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2014
    Posts
    16
    Thank you very much. One more question. If I were to write my class in the same source file as my client code like many examples would it still be instantiated like so:
    Code:
    exampleclass* ec1 = new exampleclass;
    also is ec1 always a pointer to the object because it is created on the HEAP sorta llike malloc?
    Thank you once again.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by seanrsolutions View Post
    also is ec1 always a pointer to the object because it is created on the HEAP sorta llike malloc?
    Yes. The new operator is the C++ replacement for malloc. In fact, the use of malloc is generally strongly discouraged in C++.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by seanrsolutions View Post
    also is ec1 always a pointer to the object because it is created on the HEAP sorta llike malloc?
    Thank you once again.
    In this case, you declare ec1 to be a pointer, so yes, it must always be a pointer because you told the compiler you wanted a pointer.
    If you do
    exampleclass ec1;
    Then ec1 is not a pointer.
    You choose whether to put things on the stack of the heap. If putting things on the heap, you need a pointer (smart pointers are preferred).
    No one is forcing you to put things on the heap, though.

    "a->b" is just syntactic sugar for "(*a).b", so it doesn't matter what "b" really is.
    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. Arrow Key
    By peckitt99 in forum Windows Programming
    Replies: 2
    Last Post: 03-24-2007, 05:00 AM
  2. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM
  3. Arrow keys
    By Katushai in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2005, 01:43 PM
  4. Arrow keys
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 03-27-2002, 11:49 AM

Tags for this Thread