Thread: pointers as data members

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,412
    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.
    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

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, 09: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, 10:33 AM