2

See this example.

an University class has a Director and many student So my class will be like this

a)

class University {
    Director d;
    Student list[1000];
};

or

b)

class University {
    Director* d;
    Student* list[1000];
};

My problem is how to decide whether class attributes should be pointer or value.

iammilind
  • 68,093
  • 33
  • 169
  • 336
user966379
  • 2,823
  • 3
  • 24
  • 30
  • Did you mean `Student* list;` (instead of `Student* list[1000];`) – iammilind Feb 24 '12 at 04:25
  • @iammilind - no. whether list should be an array of Student Object or an array of pointer to student object. You can concentrate only on statement Director d; Both have similar problem in my case – user966379 Feb 24 '12 at 04:33

7 Answers7

4

Most all other answers focus on the detail of heap vs. direct containment (or provide no information at all, like use pointers when you want pointers... Rather than focusing on the details, consider the overall design of the application.

The first question would be about ownership. In your program, are those students and director owned by the class? Or do they exist outside of the class scope. In most simple applications, the objects might only exist inside the class, but in other more complex designs, the students might belong to the school, and only be referenced in the class (or the director might also teach some courses to other classes). If the class owns the objects, the composition will be the best approach: hold the director directly as a member, and the students inside a container that is directly held by the class (I would recommend a vector, which is the safe choice for most cases).

If the objects don't belong to the class, then you will rather use aggregation. Whoever owns the object will have to manage the lifetimes and decide how to store the real objects and the class would only hold references (in the general sense) to those objects. Things get more complicated as there are more choices. If ownership can be transferred, then you would dynamically allocate the objects and hold pointers, where you should read smart pointers so that memory will be managed for you.

If ownership does not change and the lifetime of the students/director are guaranteed to extend beyond the lifetime of the class, you could use references. In particular for the director. In the case of the students, it will be more complex as you cannot have containers of plain references, so the solution might still be pointers there, a vector of pointers. Another issue with references is that they cannot be reseated, which means that if you hold a reference to the director, the director of the class will be fixed for the whole lifetime of the class and you won't be able to replace her.

Design is somehow complicated and you will learn with experience, but hopefully this will provide a quick start onto your problem.

Community
  • 1
  • 1
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
1

The issue here is: Where is the storage for these member variables? Sometimes it makes sense that a piece of data was allocated somewhere else and used other places. In that case a pointer may make sense (rather than using a copy constructor). However, usually that isn't the case (especially with encapsulation). Then you want to store the member data in the class. In such a case, and your example looks like it is, you don't want to use a pointer.

Avi
  • 1,231
  • 10
  • 23
1

how to decide whether class attributes should be pointer or value

I would mostly go for value (i.e. object). In some special cases, I will choose a pointer (may be a smart one!). For your case, below would suffice:

class University {
    Director d;
    std::vector<Student> list;    
public:
    University () { list.reserve(1000); }
};

The advantage of having an object is that you don't have to do your own garbage collection as the resource management will be automatic.

Pointers can be used, when you want to change the ownership of the resource (similar to shallow copy), at the same time avoiding expensive copies created during copy c-tor or assignment. In all other cases, use objects (i.e. value) for composition.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • There's two other reasons for using pointers (or references) that might be worth mentioning. 1. Polymorphism, 2. Implementation hiding. – Michael Anderson Feb 24 '12 at 04:38
0

Well it depends. Pointers should be used when you want to add stuff to the heap, while this means you have a bit more freedom in when/how you allocate memory, you have to add more code to avoid memory leaks: ie destructors and deleting stuff. It also allows you to easily modify the values from other functions/classes without having to pass a reference, just pass it in its pointer form.

One obvious situation when pointers are totally needed is in a binary tree node object, since it must contain objects of the same type as itself, it must use pointers to those objects. IE:

struct Node{
  Node* left;
  Node* right;
  //Other stuff

};

In many situations however, its up to your own discretion. Just be responsible for your pointers if you use them.

daniel gratzer
  • 52,833
  • 11
  • 94
  • 134
0

Actually there are three options
1. Object
2. Reference
3. Pointer

It's part of the design/architect .. on what to use for what object. Mostly .. the deciding criteria will be, lifecycles of the objects and the containers.

vrdhn
  • 4,024
  • 3
  • 31
  • 39
0

In both cases the class attributes are being stored by value, it just happens that in the second case those values are pointers.

Use pointers when you want pointers, use non-pointers when you don't want pointers. This entirely depends on the desired semantics of the class that you are writing.

Mankarse
  • 39,818
  • 11
  • 97
  • 141
0

This is what i would go for:

 class University {
        Director d;
        Student **list;
    };

Even though its much of a personal matter. i think using pointer to pointer is better in this case if you know what you are playing with!

I dont think a pointer array is a good choice. If you dont want pointers then use Value

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112