Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointer vs non-pointer members of a class

Tags:

c++

class

My questions is, suppose we have two classes A and B. I want to have an object of B in class A.

Should I use,

class A
{
  public:
          A();
          ~A();
          B* b;
};

or

class A
{
      public:
              A();
              ~A();
              B b;
};

As far as I know, in the first scenario, I can initialize the object *b using new operator and for the second scenario, I can initialize b using an initialization list if I don't want to use the default constructor of class B. Which is more convenient to use?

like image 872
hk84 Avatar asked Dec 21 '11 12:12

hk84


People also ask

Can pointers be class members?

Pointers to members allow you to refer to nonstatic members of class objects. You cannot use a pointer to member to point to a static class member because the address of a static member is not associated with any particular object. To point to a static class member, you must use a normal pointer.

What is non pointer?

Non-pointers are objects that, well, hold the actual type. int i; is a single integer. char name is a single character and so on. Pointers are objects that contains addresses to other objects. when you define "struct foo x;" then x will be an object of type struct foo.

Why do we use pointers to objects in C++?

Pointers are used for file handling. Pointers are used to allocate memory dynamically. In C++, a pointer declared to a base class could access the object of a derived class. However, a pointer to a derived class cannot access the object of a base class.

What is pointer to a class?

A pointer to a C++ class is done exactly the same way as a pointer to a structure and to access members of a pointer to a class you use the member access operator -> operator, just as you do with pointers to structures. Also as with all pointers, you must initialize the pointer before using it.


1 Answers

It depends.

Even if you use the pointer, you can initialize the member in the initialization list, but that's not your biggest concern.

There are advantages and disadvantages to both:

Using a pointer:

Pros:

  • the class will be smaller in memory

  • you only need a forward declaration to the other class - no need to include the other header in your header

  • can use derived objects from B as members

Cons:

  • memory management - this is a pretty big one. Do you want the memory to be managed by the class (override destructor, assignment operator and copy constructor)?

Using an object:

Pros:

  • no need to worry about memory management

Cons:

  • can only use objects of the base class. What if you want to derive from B?

  • more memory for each instance

  • constructor of B will be called on construction of A

  • you need the full definition of B inside the header

I'll edit my answer if I think of more.

like image 173
Luchian Grigore Avatar answered Sep 29 '22 20:09

Luchian Grigore