0
#include <iostream>

class A{

public:
A(){std::cout << "basic constructor called \n";};

A(const A& other) {
    val = other.x
    std::cout << "copy constructor is called \n";
}

A& operator=(const A& other){
    val = other.x
    std::cout << "\n\nassignment operator " << other.val << "\n\n"; 
}
~A(){
    std::cout << "destructor of value " << val <<" called !!\n";
}


A(int x){
    val = x;
    std::cout << " A("<<x<<") constructor called \n";
}

int get_val(){

    return val;
}
private: 

int val;
};

int main(){

    // non pointer way
    A a;

    a = A(1);

    std::cout << a.get_val() << std::endl;

    a = A(2);

    std::cout << a.get_val() << std::endl;
    // pointer way
    A* ap;

    ap = new A(13);

    std::cout << ap->get_val() << std::endl;

    delete ap;
    ap = new A(232);

    std::cout << ap->get_val() << std::endl;

    delete ap;

    return 0;
}

I initially create an object out of default constructor and then assign tmp r-value objects A(x) to a. This ends up calling assignment operator. So in this approach there are 3 steps involved

(non-pointer way)

1) constructor

2) assignment operator

3) destructor

Where as when I use pointer it only requires two step

(pointer way)

1) constructor

2) destructor

My question is should I use non-pointer way to create new classes or should I use pointer way. Because I have been said that I should avoid pointers (I know I could also use shared_ptr here).

pokche
  • 1,141
  • 12
  • 36
  • In "modern" C++ (since C++11) pointers are rarely needed outside of polymorphism. – Some programmer dude Feb 03 '17 at 18:35
  • why do you say that ? because of std::moves? since std::moves avoid assignment operator ? – pokche Feb 03 '17 at 18:36
  • 2
    And you can easily do the initialization in a single step for the "non-pointer way" as well, by doing e.g. `A a = 1;` or `A a(1);`, thereby skipping the assignment step. – Some programmer dude Feb 03 '17 at 18:37
  • You seem to be in need of [a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?lq=1) which would clearly instruct you in the proper way. – Rob K Feb 03 '17 at 21:24

1 Answers1

1

Rule of thumb: Prefer to create objects on the stack. There is less work for memory management when you create objects on the stack. It is also more efficient.

When do you have to create objects on the heap?

Here are some situations that need it:

  1. You need to create an array of objects where the size of the array is known only at run time.

  2. You need an object to live beyond the function in which it was constructed.

  3. You need to store and/or pass around a pointer to a base class type but the pointer points to a derived class object. In this case, the derived class object, most likely, will need to be created using heap memory.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • but but what about the assignment operator that needs to done while using stack approach. – pokche Feb 03 '17 at 19:15
  • 1
    1. You don't need to assign unless you want to. You create the object with the right value to start with. 2. Assignment is cheap for simple classes, such as you have in your post. – R Sahu Feb 03 '17 at 19:19
  • (used to be the first comment, but I needed to edit it) You don't _have_ to use the heap for variable-length data, you know, as long as you check the size before allocating on the stack... (and now comes R Sahu's comment about "a finer point") – einpoklum Feb 03 '17 at 20:27
  • @RSahu it makes sense when you said I don't have to use assign unless needed – pokche Feb 03 '17 at 21:48