简体   繁体   中英

Class member vs function argument

I have a question related to how to design a class using object-orienting languages such as c++. In many circumstances variables can either be assigned to class members or function arguments. I give the following examples to make my point clear:

class MyClass
{
public:
  int variable1;
  int variable2:
  MyClass (int vr1, int vr2)
  {
     variable1 = vr1;
     variable2 = vr2;
   }
  bool perform_one_task()
  {
    // do something
    return true;
  }
}

When we want to use this class, we can use the following codes:

  int a,b;
  MyClass mine(a,b);
  mine.perform_one_task();

It is also possible to design a class without variable1 and variable2 .

    class MyClass
    {
    public:

      bool perform_one_task(int variabl1, int variable2)
      {
        // do something
        return true;
      }
    }

In this class variable1 and variable2 are not members of the class but become the arguments of the function. My question is in which condition the first design strategy is preferable and in which condition the second design pattern is better. The given example only assume the structure of variable1 and variable2 are small, but what if they are big, for example, variable1 may be an large array of data.

What is the class actually modelling ? What needs encapsulating ? And who actually owns that data ?

In your first example, the members are an implementation detail. They're hidden from the users/clients of the class, and you could replace those and change the implementation completely. The clients of the method perform_one_task() are unaware that those fields exist.

eg your class could represent a 2-d coordinate and your members the X/Y attributes. At some stage you could change that class to represent the coordinate in polar terms (angle + radius). The API to the coordinate class would remain the same, but the implementation would be fundamentally different.

Your second value is purely encapsulating the functionality. The class can be represented with no data that it itself owns. This isn't unreasonable. You can legitimately implement (say) a Calculator class that simply performs calculations and encapsulates that functionality. The benefit of putting it in a class is that it achieves some reuseability and isn't tied to the originating source code.

The bottom line is that you can write classes that take/hold/own data, and classes that don't hold state. Both approaches are legitimate in differing scenarios. The fundamental questions remain as to who owns/controls that data.

当需要在类实例生命周期的其他地方使用值时,应使用第一种模式(成员):否则,如果仅需要在方法中使用值,则应使用第二种模式

According to principles of object oriented programming ( OOP ) when you develop your class you usually choose what will be object attributes (state) and what will be object methods (behavior). Herein methods can have input parameters. So we could reword your question to: what is the difference between object attributes and method parameters.

Object attributes are its properties which characterize this object (state characteristics), eg 3D point is characterized by 3 coordinates: x, y, z; circle is characterized by center coordinates and radius.

Object methods (behavior) are calculations applied to this object, eg triangle can have such methods as calculateSquare(), calculatePerimeter(), rotate(angle), move(newX, newY) etc.

And finally method parameters are input data for this method but they are not object properties . Eg 2D shape object can have method rotate(angle) where angle is not property of shape object but is input parameter for rotate method, namely the angle to rotate the shape.

Your first example is object-oriented (assuming that perform_one_task() uses the variables!). Your second example isn't, it is just a function wrapped in an empty class wrapper.

Generally speaking object oriented classes are a way of packaging up some data and all of the associated operations (methods). Classes work best when they model some real world object or concept. The data encapsulated within the class is protected and can only be accessed or manipulated by the operations provided by the class. You would normally expect a class to have several methods operating on the same data in different ways, so your examples are a little too simple.

If you try adding another task to your examples then you'll see that in the second example you'll just end up with two unrelated functions, whereas in the first example each task has the ability to operate on the same data.

Class members are class members. These are properties of an instance of class object or static values used by whole class - so values closely related to the class.

You don't store in your class values that don't model your class, unless you need them (and then they do model your class actually). This is unnecessary and waste of resources. Always avoid unnecessary variables. So basically:

class MyClass{
public:
  bool perform_one_task(int onlyMethodArg1, int onlyMethodArg2){
    // do something
    return true;
    // onlyMethodArgs are gone since nobody really needs them, we are interested 
    // only in result of calculation
  }
void saveProperties(int prop1, prop2){
    property1=prop1;
    property2=prop2;
} 
private:
    int property1;
    int property2;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM