memory ? about objects within a class

Discussion in 'C++' started by Mark K, Jul 25, 2003.

  1. Mark K

    Mark K Guest

    Im just getting back into C++ after a long hiatus. Ive been coding in
    Java for the last 3 years and Im finding it hard to get back into the
    swing of things since C++ went standard. Anyhow,,,here's my question

    given the following class def and implementation, how should I be
    cleaning up my variables?

    Also, when implementing the "has a" relationship with other classes
    and regular datatypes, when should pointers or just references be
    used?

    As you can see, I lost most of my C++ knowledge to Java's
    over-simplified impl.

    Thanks for any clarification

    Mark

    /*in Customer.h*/
    #ifndef COMMON_CUSTOMER_H
    #define COMMON_CUSTOMER_H

    #include <string>

    class Customer
    {
    public:
    Customer();
    virtual ~Customer();
    const string getName() const ;
    void setName( const string & ) ;
    private:
    string name ;
    };

    #endif

    /* in Customer.cxx */
    #include <string>
    #include "Customer.h"

    Customer::Customer()
    {
    name = "";
    }

    Customer::~Customer()
    {
    //delete name ; <- I know this isn't right, but how do I release this
    }

    //Or should I be returning a pointer to this reference?
    const string Customer::getName() const
    {
    return this->name ;
    }

    void Customer::setName( const string &newName)
    {
    name = newName ;
    }
    Mark K, Jul 25, 2003
    #1
    1. Advertising

  2. "Mark K" <> wrote...
    > Im just getting back into C++ after a long hiatus. Ive been coding in
    > Java for the last 3 years and Im finding it hard to get back into the
    > swing of things since C++ went standard. Anyhow,,,here's my question
    >
    > given the following class def and implementation, how should I be
    > cleaning up my variables?


    See below.

    > Also, when implementing the "has a" relationship with other classes
    > and regular datatypes, when should pointers or just references be
    > used?


    Pointers should be used if you are not sure whether during run-time
    the "had" object may or may not exist (pointers can be NULL). Use
    reference if the lifetime of the object is the same as the object
    that has it, but the relationship is not really a "has-a", but more
    of a "associated-with".

    > As you can see, I lost most of my C++ knowledge to Java's
    > over-simplified impl.
    >
    > Thanks for any clarification
    >
    > Mark
    >
    > /*in Customer.h*/
    > #ifndef COMMON_CUSTOMER_H
    > #define COMMON_CUSTOMER_H
    >
    > #include <string>
    >
    > class Customer
    > {
    > public:
    > Customer();
    > virtual ~Customer();
    > const string getName() const ;
    > void setName( const string & ) ;
    > private:
    > string name ;
    > };
    >
    > #endif
    >
    > /* in Customer.cxx */
    > #include <string>
    > #include "Customer.h"
    >
    > Customer::Customer()
    > {
    > name = "";


    There is no need to do that. The string 'name' will be set to
    <empty> on construction.

    > }
    >
    > Customer::~Customer()
    > {
    > //delete name ; <- I know this isn't right, but how do I release this


    You don't need to. Since the 'name' object is contained within
    the 'Customer', it will be destroyed when this Customer is.

    > }
    >
    > //Or should I be returning a pointer to this reference?
    > const string Customer::getName() const



    Returing a const ref or a non-const object are both fine.

    > {
    > return this->name ;
    > }
    >
    > void Customer::setName( const string &newName)
    > {
    > name = newName ;
    > }


    Victor
    Victor Bazarov, Jul 25, 2003
    #2
    1. Advertising

  3. On 24 Jul 2003 16:34:08 -0700, (Mark K) wrote:

    >Im just getting back into C++ after a long hiatus. Ive been coding in
    >Java for the last 3 years


    yurk

    >Also, when implementing the "has a" relationship with other classes
    >and regular datatypes, when should pointers or just references be
    >used?


    It always depend, there is (unfortunately) no "right" answer.

    Most of the time, you will aggregate an object simply by having it in
    the class :

    class A
    {
    private:
    Customer c_;

    };

    And 'c_' will get initialized by A's constructor (you will have to
    specify it in the initialization list if Customer's constructor needs
    arguments).

    The thing with this method is that maybe you don't *know* what to pass
    to c_'s constructor, or maybe you don't want c_ at all at that moment.
    For example, the user could click something and then you would create
    the object. If he does not click anything, you never create it : time
    and memory are saved.

    In that case, you will need a pointer :

    class B
    {
    private:
    Customer *c_;
    };

    which will be new'ed when you wish and deleted when you must (usually
    in the destructor).

    The third case would be that your class needs access to another object
    of which you don't control the lifetime. In that case, since you do
    not 'own' the object, use a reference :

    class C
    {
    private:
    Customer &c_;

    public:
    C(Customer &c) : c_(c)
    {
    }
    };


    >/*in Customer.h*/
    >#ifndef COMMON_CUSTOMER_H
    >#define COMMON_CUSTOMER_H
    >
    >#include <string>
    >
    >class Customer
    >{
    > public:
    > Customer();
    > virtual ~Customer();


    Not necessary if not a base class.

    > const string getName() const ;


    If this function returns a string by value (that is, a copy), it is
    useless to make it const.

    > void setName( const string & ) ;
    > private:
    > string name ;


    This should not compile. Either fully qualify the names (such as
    std::string) or put a 'using namespace std;' on top (which is not
    recommended though).

    >};
    >
    >#endif
    >
    >/* in Customer.cxx */
    >#include <string>
    >#include "Customer.h"
    >
    >Customer::Customer()
    >{
    > name = "";
    >}


    Prefer using initialization lists in ctor :

    Customer::Customer()
    : name("")
    {
    }

    >
    >Customer::~Customer()
    >{
    > //delete name ; <- I know this isn't right, but how do I release this
    >}


    You don't have to, std::string is a class. When your Customer object
    will get out of scope, it will be destroyed automatically.

    the only reason for which you would want to delete something is when
    YOU created it :

    int main()
    {
    Customer *c1 = new Customer;
    Customer c2;

    // you created c1, so delete it
    delete c1;

    // and you didn't create c2, so don't touch it
    // as it will go out of scope, its memory will be released

    // if c2 owns any object (like a std::string), they will get
    // destroyed automatically
    }

    You have to make a difference between automatic objects and pointers.
    Both of them are deleted correctly by the run-time system, so you
    don't want to touch them :

    int main()
    {
    Customer c1;
    Customer *c2;

    delete &c1; // ouch!!
    delete &c2; // ouch!!!
    }

    The thing is, the pointer could *point* to something you allocated
    yourself and this memory must be deleted :

    int main()
    {
    Customer *c1 = new Customer;

    delete c1; // you are deleting the Customer object you allocated
    // and the *pointer* itself will be deleted by the
    // system
    }

    >//Or should I be returning a pointer to this reference?
    >const string Customer::getName() const
    >{
    > return this->name ;
    >}


    No, you return that string by value or reference. The time of
    pointers is long gone now, use them only if you must. If you don't
    know when to use them, it means you don't need them.

    Jonathan
    Jonathan Mcdougall, Jul 25, 2003
    #3
  4. Jonathan Mcdougall wrote:
    >
    > The thing with this method is that maybe you don't *know* what to pass
    > to c_'s constructor, or maybe you don't want c_ at all at that moment.
    > For example, the user could click something and then you would create
    > the object. If he does not click anything, you never create it : time
    > and memory are saved.
    >
    > In that case, you will need a pointer :
    >
    > class B
    > {
    > private:
    > Customer *c_;
    > };
    >
    > which will be new'ed when you wish and deleted when you must (usually
    > in the destructor).
    >


    I think it's worth to emphasize: You want to avoid that case whenever you can.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Jul 25, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,684
    Thomas Weidenfeller
    Oct 12, 2005
  2. Simon Elliott

    Inheritance of objects within objects

    Simon Elliott, Dec 10, 2004, in forum: C++
    Replies:
    2
    Views:
    321
    Simon Elliott
    Dec 10, 2004
  3. 7stud
    Replies:
    11
    Views:
    662
    Dennis Lee Bieber
    Mar 20, 2007
  4. Brandon Coleman

    Objects within objects... ??

    Brandon Coleman, Jul 19, 2006, in forum: Ruby
    Replies:
    6
    Views:
    97
    Wes Oldenbeuving
    Jul 20, 2006
  5. Replies:
    1
    Views:
    79
    Matt Kruse
    Aug 15, 2006
Loading...

Share This Page