Should I use -> or . ?

This is a discussion on Should I use -> or . ? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by brewbuck In other words, if the LHS is a pointer, magically transform "." into "->". Well, here ...

  1. #16
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Quote Originally Posted by brewbuck View Post
    In other words, if the LHS is a pointer, magically transform "." into "->".
    Well, here you have the first problem. Code semantics are being hidden away, making the code less clear. And isn't this precisely what should be avoided on a strongly typed language? And wouldn't that just complicate the compiler's life too?

    I believe we can agree (*ptr).member() needs an alternative. However, I don't think it ever crossed anyone's mind to replace it with the dereference operator simply because there is important information that suddenly is lost and a semantics contract being broken.

    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.
    Absolutely not!
    You may look at references as const pointers, but regardless of how they are implemented by the compiler, their semantics are vastly different. A reference is never dereferenced. Or to put it bluntly, a reference is not a pointer, it is a reference.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #17
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Mario F. View Post
    Absolutely not!
    You may look at references as const pointers, but regardless of how they are implemented by the compiler, their semantics are vastly different. A reference is never dereferenced. Or to put it bluntly, a reference is not a pointer, it is a reference.
    My point is, in the following code snippet, can you tell me whether x is a reference to an object, or an actual object?

    Code:
    foo.bar();
    I don't see how that's different from asking, is foo an object or a pointer to an object?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #18
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    I have another question about header files ... I have been searching and cannot figure this out.

    testing.h
    Code:
    #ifndef TESTING_H
    #define TESTING_H
    
    int test();
    
    #endif
    testing.cpp
    Code:
    #include "testing.h"
    
    int test(){
      return 3;
    }
    test.cpp
    Code:
    #include<iostream>
    #include "testing.h"
    
    int main(){
      
      std::cout << test();
      
      system("PAUSE");
      return 0;
    }
    [Linker error] undefined reference to `test()' using Dev-C++

    Does anyone know why I would get this?

    TIA!

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    If you're building with a project, you have to include both testing.cpp and test.cpp in the project so both can be compiled and then linked together.

    As it is, you are only compiling test.cpp, which can see the declaration for test() but cannot link to the definition.

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Aside from the linking issue, Dev-C++ is a bit outdated. I would recommend a new IDE.
    Some good IDEs that are available can be found at: http://cpwiki.sf.net/IDE
    Visual Studio and Code::Blocks are two favorites.
    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.

  6. #21
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by brewbuck View Post
    My point is, in the following code snippet, can you tell me whether x is a reference to an object, or an actual object?

    Code:
    foo.bar();
    I don't see how that's different from asking, is foo an object or a pointer to an object?
    References and pointers have different semantics though. Asking if something is an object or an alias (aka reference) of that object really doesn't matter. Thantos is a reference to me, so if you address me as Thantos or as my real name it doesn't really make any difference.

  7. #22
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Quote Originally Posted by brewbuck View Post
    My point is, in the following code snippet, can you tell me whether x is a reference to an object, or an actual object?

    Code:
    foo.bar();
    I don't see how that's different from asking, is foo an object or a pointer to an object?
    Ah, I see you point.
    I don't have the vocabulary to put this in the right words. But I believe it is largely irrelevant to know if it is a reference or the actual object, since any operator semantics remain the same.

    However, * and -> offer vastly different semantics. Let me remind you of their overload declarations:

    Code:
    myClass &operator*() { /*...*/ }
    myClass *operator->() { /*...*/ }
    const myClass &operator*() const { /*...*/ }
    const myClass *operator->() const { /*...*/ }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> Asking if something is an object or an alias (aka reference) of that object really doesn't matter.
    When an object goes out of scope, it is destroyed. When a reference (or alias) goes out of scope, the object is not destroyed. That is as much of a difference as the difference between a pointer and an object with respect to this question.

    >> However, * and -> offer vastly different semantics.
    The question was between . and ->, not * and ->.

  9. #24
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Installed Code::Blocks ... looks like a winner.

    I got everything up and running. Thanks for everyone's help and the interesting reading!

  10. #25
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    And never ever include .cpp files. They are compiled separately. Header files can be included.
    You can include .cpp files as .inl or as .hpp when using templates if you want to put your definitions in a separate file. I prefer .inl but I have seen it done as .cpp.

  11. #26
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    btw, the "blood type" function is implemented wrong
    you can't just make a char pointer data member point at another char
    you gotta allocate an indepent piece of memory for it
    Last edited by Hussain Hani; 07-21-2009 at 09:31 PM.
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  12. #27
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> you can't just make a char pointer data member point at another char
    In this case you can, because they are all pointers to string literals.

    They should be const char* variables which would make this more clear, but it's still legal as is and should work.

  13. #28
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by brewbuck View Post
    My point is, in the following code snippet, can you tell me whether x is a reference to an object, or an actual object?

    Code:
    foo.bar();
    I don't see how that's different from asking, is foo an object or a pointer to an object?
    The pointer could be null. This is the most significant difference between references and pointers: references cannot ever be null, and if you use them properly, they cannot ever be invalid either. A pointer, on the other hand, can be null, and it's not uncommon for it to be invalid either.

    Thus, dereferencing a reference can be implicit (by using the value directly or by using the direct member access operator), whereas dereferencing a pointer really should always be explicit, using * or ->.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #29
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Well, my GCC allows a NULL reference:

    Code:
    #include <iostream>
    
    int main(void)
    {
        int &ref = *(int *)0;
        std::cout << "&ref is 0" << std::endl;
        std::cout << "ref is now " << ref << std::endl;
    }
    The code crashes after printing "&ref is now 0". That's not standard, is it?

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,733
    Quote Originally Posted by Brafil
    That's not standard, is it?
    That is undefined behaviour due to an attempt to dereference a null pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 3 FirstFirst 123 LastLast
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, 01: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, 01:16 PM

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