0

I don't get the point at all. I just know that; if I don't write like this

void samplefunctionname(const randomclass &classmember) {
  //code goes here...
}

the function will copy every information in classmember to itself. So it's not good for memory. Maybe I just want to write classmember's variables that are chosen by me, but if I do not write const and & it will just write all of the information.

But what actually these guys are doing ? Why class should be const etc.

Nasreddine Galfout
  • 2,550
  • 2
  • 18
  • 36

1 Answers1

1

Why should I use const and & while declaring class in function?

In order to avoid copying the whole class. When you use const&, you pass the object by reference, which doesn't copy any data at all.

gsamaras
  • 71,951
  • 46
  • 188
  • 305