Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > class data member or member function argument?

Reply
Thread Tools

class data member or member function argument?

 
 
Dwight Army of Champions
Guest
Posts: n/a
 
      02-21-2010
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?
 
Reply With Quote
 
 
 
 
Robert Fendt
Guest
Posts: n/a
 
      02-21-2010
And thus spake Dwight Army of Champions
<(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      02-21-2010
"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.


 
Reply With Quote
 
Dwight Army of Champions
Guest
Posts: n/a
 
      02-21-2010
OK I definitely need to get a book on object oriented software design.
Recommendations?
 
Reply With Quote
 
tonydee
Guest
Posts: n/a
 
      02-22-2010
On Feb 22, 2:21*am, Dwight Army of Champions
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Pointer-to-member-function pointing to a member function of an inherited class akiriwas@gmail.com C++ 12 02-11-2005 05:15 PM



Advertisments