0

Hello! I wanted to learn more about pointers, so i wrote the following:

 struct Data {
     int day, month, year;
     double hour;
 }; 

class Movie {
    protected:
        string name;
        Data data;
        int requiredAge;
        int freeSeats;

    public:
        Movie(){  }
        Movie(string moviename, Data dat, int age, int seats) {
            name = moviename;
            data = dat;
            requiredAge = age;
            freeSeats = seats;
        }
        int getFreeSeats() { return freeSeats; } 
        ~Movie(){  }

};

And then i initialize it into the main:

int _tmain(int argc, _TCHAR* argv[])
{
    Data d; d.day = 12; d.hour = 16; d.month = 04; d.year = 2016;
    Movie movie("Movie Name", d, 16, 35 );

    system("pause");

    return 1;
}

Why should i use pointers here and where?

  • 7
    If you can write the code without the use of pointers (which it looks like you have done), then you shouldn't use pointers anywhere; they are unnecessary. – R_Kapp Apr 08 '16 at 16:32
  • 1
    Not even for optimisation reasons? – petru herro Apr 08 '16 at 16:37
  • 1
    Having to look up another level of indirection will slow you down, not speed you up. – Mad Physicist Apr 08 '16 at 16:40
  • @petruherro _"Not even for optimisation reasons?"_ Pointers don't optimize anything, the compiler does that for you. – πάντα ῥεῖ Apr 08 '16 at 16:44
  • Think of pointers in terms of sharing. Pointers allow the same physical entity (mostly kind of object instances with languages like c++) to be referenced and used (modified) from different places in your code. Especially when calling methods you need to provide a reference to parameter object for the method body to truely use the parameter. Otherwise the compiker would need to create a coipy of the object on the stack and at return copy it back to the original instance. This would work with immutables as the last step is not needed, but in general would be horribly inefficient. – rpy Apr 08 '16 at 16:45
  • Since you're talking about performance, it would also break **data locality**. Imagine you had a huge array of `Movie`, a version with pointer would have you look all over the place to fetch its members, inducing more **cache misses**. A good read on the topic : http://gameprogrammingpatterns.com/data-locality.html – giant_teapot Apr 08 '16 at 16:46
  • @petruherro BTW, `Ticket` isn't specified in your code. – πάντα ῥεῖ Apr 08 '16 at 16:47
  • So you could think of this as optimization. But it is more a doifferent kind of tretment of logical instances. Consider a lookup table that would not just return the reference to an object but a true copy. – rpy Apr 08 '16 at 16:47
  • Thanks for all your answers, it helped me figure some things out – petru herro Apr 08 '16 at 16:55

1 Answers1

0

The only place where it could make sense to use pointers here is for passing the struct to the constructor:

    Movie(string moviename, Data* dat, int age, int seats) {
        name = moviename;
        data = *dat;
        requiredAge = age;
        freeSeats = seats;
    }

This would avoid creating a complete copy of the struct when the constructor is called. Just passing the pointer is a bit cheaper.

While in C, this would be the only option, in C++ there is a better way: Use references:

    Movie( const string& moviename, const Data& dat, int age, int seats) {
        name = moviename;
        data = dat;
        requiredAge = age;
        freeSeats = seats;
    }

By the way, for the intarguments it is not useful to pass them by reference bacause they are not larger (in terms of bytes) than a pointer.

An even better version would use an initialization list:

    Movie( const string& moviename, const Data& dat, int age, int seats)
        : name(moviename), data(dat), requiredAge(age), freeSeats(seats) {}

(Thanks for the hint, @NathanOliver)

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
  • You may also want to show the OP they can use a member initialization list to initialize all of the class members and save some cpu cycles. Normally you want 0 code in the constructor body. – NathanOliver Apr 08 '16 at 16:55
  • What are the pros for using a initialization list? – petru herro Apr 08 '16 at 17:02
  • @petruherro: You find some info here: http://stackoverflow.com/questions/1598967/benefits-of-initialization-lists and in many other places. – Frank Puffer Apr 08 '16 at 17:10
  • @petruherro This explains it very well: http://forums.codeguru.com/showthread.php?464084-C-General-What-is-the-initialization-list-and-why-should-I-use-it – Ceros Apr 08 '16 at 17:10
  • @FrankPuffer Thank you for all the help. – petru herro Apr 08 '16 at 17:10