class data member or member function argument?

Discussion in 'C++' started by Dwight Army of Champions, Feb 21, 2010.

  1. When designing a class, how do you know whether to make a variable a
    private data member or just an argument to the class's member
    functions? If I make it a private data member, then it's imperative I
    have a valuie for it at the time I create the object (put it in the
    constructor). On the other hand, if I just use it as a member function
    argument, then I have to be responsible for defining and initializing
    the variable before the object is created, and then deleting the
    variable after the object is deleted. THis leads to code like this:

    // code begins here
    // ... define and initialize many variables called MyVariable

    MyClass* MyObject = new MyClass(this);

    MyObject->DoSomething(MyVariable1, MyVariable2, /* ... */ );
    MyObject->DoSomethingElse(MyVariable2, MyVariable6);

    delete MyObject; MyObject = 0;

    // delete many MyVariables
    // code ends here

    What is the best way to do this?
     
    Dwight Army of Champions, Feb 21, 2010
    #1
    1. Advertising

  2. Dwight Army of Champions

    Robert Fendt Guest

    And thus spake Dwight Army of Champions
    <> Sun, 21 Feb 2010 09:21:10 -0800 (PST):

    > When designing a class, how do you know whether to make a variable a
    > private data member or just an argument to the class's member
    > functions? If I make it a private data member, then it's imperative I
    > have a valuie for it at the time I create the object (put it in the
    > constructor). On the other hand, if I just use it as a member function


    A variable is made a member of the class precisely when it is
    part of an object's state. If and when that is the case depends
    mostly on the problem that is being solved.

    Of course you can implement two-phase construction (i.e. provide
    functions that 'fill in' object state information after the
    object has been actually created). But in that case you have to
    be *very* careful of undefined behaviour of your objects (e.g.
    when someone calls a function without having called the needed
    setup functions first). This essentially violates encapsulation
    and is thus very bad mojo.

    > argument, then I have to be responsible for defining and initializing
    > the variable before the object is created, and then deleting the
    > variable after the object is deleted. THis leads to code like this:


    This does not make much sense. *Of course* you have to have the
    data ready that is needed for a function to run. Whether the
    data is part of the object (in form of members) or you feed it
    to the function at call time does not change the simple fact
    that you have to have all the bits and pieces together that are
    needed for that particular function to run.

    The 'deleting' part is largely moot, at least if you stay clear
    of needless 'naked' heap allocation (and that's bad practice
    anyway). Only use 'new' and 'delete' if it is absolutely
    necessary that an object 'survives' exiting the currect scope
    (and consider smart pointer templates even then).

    > What is the best way to do this?


    The best way to do this is to buy a book on object oriented
    software design, read it carefully and then try again. Sorry if
    that sounds harsh, but it seems to me that your problem is not
    how to implement a specific pattern in C++ but rather how
    object-orientation and encapsulation is supposed to work in
    general. And for that, comp.lang.c++ is just not the right
    place, in my opinion.

    Regards,
    Robert
     
    Robert Fendt, Feb 21, 2010
    #2
    1. Advertising

  3. Dwight Army of Champions

    osmium Guest

    "Dwight Army of Champions" wrote:


    > When designing a class, how do you know whether to make a variable a
    > private data member or just an argument to the class's member
    > functions? If I make it a private data member, then it's imperative I
    > have a valuie for it at the time I create the object (put it in the
    > constructor). On the other hand, if I just use it as a member function
    > argument, then I have to be responsible for defining and initializing
    > the variable before the object is created, and then deleting the
    > variable after the object is deleted.


    My guess is you are missing some fundamentals here. Focus on what deleting
    an object does. The member function is gone after you delete the object.
    What did the member function consist of? I didn't look at your code, only
    your question.
     
    osmium, Feb 21, 2010
    #3
  4. OK I definitely need to get a book on object oriented software design.
    Recommendations?
     
    Dwight Army of Champions, Feb 21, 2010
    #4
  5. Dwight Army of Champions

    tonydee Guest

    On Feb 22, 2:21 am, Dwight Army of Champions
    <> wrote:
    > When designing a class, how do you know whether to make a variable a
    > private data member or just an argument to the class's member
    > functions? If I make it a private data member, then it's imperative I
    > have a valuie for it at the time I create the object (put it in the
    > constructor). On the other hand, if I just use it as a member function
    > argument, then I have to be responsible for defining and initializing
    > the variable before the object is created, and then deleting the
    > variable after the object is deleted. THis leads to code like this:
    >
    > // code begins here
    > // ... define and initialize many variables called MyVariable
    >
    > MyClass* MyObject = new MyClass(this);
    >
    > MyObject->DoSomething(MyVariable1, MyVariable2, /* ... */ );
    > MyObject->DoSomethingElse(MyVariable2, MyVariable6);
    >
    > delete MyObject; MyObject = 0;
    >
    > // delete many MyVariables
    > // code ends here
    >
    > What is the best way to do this?


    I agree buying a book on OO is a good idea, but can't recommend any.

    Just to summarise very briefly, a class is generally used to represent
    a type of "entity" in your program: something you could say "I've got
    one of those". It might be an employee, a cat, a car, a line in a
    file. Then, you can create your objects to represent individual
    instances of these concepts. You want to put private data into the
    class so that someone using the class can just say "I want one
    employee record here", or "I'm going to use a list of cats", but they
    don't need additional variables to record any specifics of an
    individual cat, because the cat class itself wraps up all the data
    about each cat.

    You've got a couple mechanisms to avoid new and delete everywhere, and
    having to explicitly initialise data members or local data. One is
    scoping: the idea that your program is broken up into chunks of work -
    namely a scope delimited with { and }, and variables can be placed
    inside that scope. If they are classes, their constructor will run
    and can initialise them to some default value, or you can typically
    explicitly request a value. The object's destructor is then called at
    the end of that scope, so you don't have to put anything in your code
    to reclaim the memory. For example:

    int fn()
    {
    std::string x = "hello";
    std::cout << x << '\n';
    std::list<Cat> cats;
    cats.push_back("Bluey");
    feed(cats);
    }

    fn() doesn't need any news or deletes. Similarly, in a class, you can
    have data members that are STL containers and don't need explicit
    initialisation.

    class Cat
    {
    public:
    Cat(const std::string& name, int age) : name_(name), age_(age)
    { }
    int get_age() const { return age_; }
    const std::string& name() const { return name_; }
    private:
    std::string name_;
    int age_;
    };

    Again, all the memory allocation is taken care of for you. The way
    the constructor is written ensures you'll have to supply a name and
    age when creating an object instance, ala:

    Cat cat("Smokey", 4);

    Cheers,
    Tony
     
    tonydee, Feb 22, 2010
    #5
    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,788
    Thomas Weidenfeller
    Oct 12, 2005
  2. Gert  Van den Eynde
    Replies:
    8
    Views:
    636
    Keith H Duggar
    Jan 26, 2004
  3. Jason
    Replies:
    2
    Views:
    514
    Jonathan Mcdougall
    May 13, 2006
  4. Reckoner
    Replies:
    11
    Views:
    704
    Steven D'Aprano
    Jan 19, 2009
  5. AikidoGuy
    Replies:
    11
    Views:
    560
    Seebs
    Nov 21, 2011
Loading...

Share This Page