Pointers and Efficiency

This is a discussion on Pointers and Efficiency within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by CornedBee Nothing, really. You just have to remember that, once you have a reference member, several things ...

  1. #76
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by CornedBee View Post
    Nothing, really. You just have to remember that, once you have a reference member, several things happen. First, you don't get a generated copy assignment operator. Second, the class is no longer a POD, even if it would have been otherwise. Third, if the reference can refer to different objects in different instances, you cannot implement a reliable assignment operator or a reliable swap, because the functions would have to fail for objects coming - in your example - from different worlds. Built-in xenophobia

    So when I said that references make poor class members, I was referring to the fact that having a reference member affects the design of the class.
    Ah, understood. So what do you prefer for saving an argument taken by const reference as class member, if you want to get a (relyable) copy/assignment operator?

    Code:
    class Entity
    {
       Entity(const World& _world)
       :m_world(_world)
       // or
       :msp_world(&_world)
       {}
    
       World m_world; // initiate (probably complex)   copying
    
       // or
    
       const shared_ptr<World> msp_world;
    };

  2. #77
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If I want to simulate Java's implicit outer class pointers that inner classes have, I use a raw pointer. Unless I don't need them to be assignable, in which case I use a reference. I can't think of anything more semantic.
    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

  3. #78
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by CornedBee View Post
    Unless I don't need them to be assignable, in which case I use a reference. I can't think of anything more semantic.
    Yes, but you pointed out that then there is no copy assignment operator generated and a hand made one can't e relieable, if there are more than one possible object to reference. So what to use if I'd absolutely need copying and assignment?

  4. #79
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    As I said, a raw pointer. Given that the outer object pretty much has to outlive the inner object, the shallow copy semantics are fine.
    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

  5. #80
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Looming on 80 replies. Is this thread still on topic?
    Perhaps start new threads with more specific questions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #81
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Ok, that java comparison confused me, because I'm not into java very much. Thank you

Page 6 of 6 FirstFirst 123456
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Improving the efficiency..
    By aaronljx in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 09:09 AM
  2. trouble with file pointers...
    By quasigreat in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 08:12 AM
  3. Function Pointers in C
    By Gobinath in forum C Programming
    Replies: 7
    Last Post: 03-10-2005, 10:54 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Question about efficiency... structures and stuff
    By rmullen3 in forum C++ Programming
    Replies: 2
    Last Post: 01-05-2002, 12:00 PM

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