pointers as data members

This is a discussion on pointers as data members within the C++ Programming forums, part of the General Programming Boards category; I've read in my book that if you use pointers as data members in your class, you likely need to ...

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    pointers as data members

    I've read in my book that if you use pointers as data members in your class, you likely need to make a new copy constructor, and = operator, and probably also a constructor and destructor.

    Now, I can understand the point. Since the default copy-constructor just copies the value of each data-member, the result when doing this on a pointer is simply copying the pointer-adress. Which would make 2 objects pointing to the same space in memory, which usually isnt what the have in mind.

    That's all good.

    But what about the string-class? It uses pointers and even new / delete behind the scenes.
    But I guess the string-class comes with a proper copy constructor and assignment operator?
    If so, using strings wouldnt make more trouble than using a normal int, right?
    (In the means of pointer and datamember trouble)

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Yes, the string class will handle its own memory management, so you do not have to worry.
    Whether it copies the pointer or the data is not your worry.

    It simply is your worry when using raw pointers.
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by Drogin
    But what about the string-class? It uses pointers and even new / delete behind the scenes.
    But I guess the string-class comes with a proper copy constructor and assignment operator?
    If so, using strings wouldnt make more trouble than using a normal int, right?
    (In the means of pointer and datamember trouble)
    Yes, std::string has normal copy semantics. You can copy a std::string and expect to really get a copy. The same goes for the standard containers.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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