-4
// const objects
#include <iostream>
using namespace std;

class MyClass {
    int x;
  public:
    MyClass(int val) : x(val) {}
    const int& get() const {return x;}
};

void print (const MyClass& arg) { // Need to understand this line
  cout << arg.get() << '\n';
}

int main() {
  MyClass foo (10);
  print(foo);

  return 0;
}

I am new to C++. Need to understand what are the parameters passed in print function. If this is address then why are we passing foo is print function call.

Sumit
  • 1,953
  • 6
  • 32
  • 58
  • 4
    I wouldn't recommend looking at random pieces of code, guess how it might work and then ask stackoverflow when you couldn't figure it out. This is covered in all introductory C++ books and decent tutorials. – eerorika Feb 25 '16 at 12:35
  • Also see: http://stackoverflow.com/questions/2582797/why-pass-by-const-reference-instead-of-by-value, http://stackoverflow.com/questions/5060137/passing-as-const-and-by-reference-worth-it, http://stackoverflow.com/questions/19349023/what-is-the-point-of-const-being-used-with-pass-by-reference-c, http://stackoverflow.com/questions/2582797/why-pass-by-const-reference-instead-of-by-value, and your favorite introductory C++ book. – Cody Gray - on strike Feb 25 '16 at 12:40

1 Answers1

1

The & in void print (const MyClass& arg) that arg is passed by reference. It is C++s way to make pointers and things a little bit easier.

References allow you to manipulate a variable inside of a function and make the results visible on the outside too. So a bit like pointers. But you don't need to explicitly get the address of the variable to do that.

The const statement is a way to prevent the described behavior. const forbids the manipulation of arg inside print.

muXXmit2X
  • 2,745
  • 3
  • 17
  • 34
  • Why am I getting so many downvotes? What's wrong with the answer? – muXXmit2X Feb 25 '16 at 12:52
  • I upvoted ur answer :). I need little more explanation. In 'print (const MyClass& arg)` , does it mean that arg is a object referrence of `MyClass` – Sumit Feb 25 '16 at 13:01
  • 1
    `arg`is an instance/object of `MyClass` passed by reference. – muXXmit2X Feb 25 '16 at 13:03
  • Then when we are calling print as print(foo), why we have not passed any pointer in print. Why we have passed object? – Sumit Feb 25 '16 at 13:04
  • 1
    Thats the cool thing about references. You get the pros of using a pointer but can work with them like a normal variable. So you don't have to derefence or reference when using them. – muXXmit2X Feb 25 '16 at 13:06
  • "so many downvotes" == 1. It was my downvote, if you must know. Three general reasons: (1) you answered what was obviously a duplicate question instead of finding the duplicate and flagging the question accordingly, (2) you formatted the name of the language (C++) as code, when it is clearly not code, (3) the claim that references are a "way to make pointers and things a little bit easier" is at the very least misleading but probably just wrong. I considered changing this when I edited the formatting, but didn't want to put words in your mouth, so I just downvoted instead. – Cody Gray - on strike Feb 25 '16 at 14:08
  • @CodyGray (1) I see, my fault. (2) understandable but no reason to downvote. (3) Yeah you are kind of right. however I just wanted to explain it in a very simple manner, and they're making some things pointers do a little bit easier. I know that there is stil a difference between both concepts. And btw. it were 3 downvotes (up, down, up, down, etc.) – muXXmit2X Feb 25 '16 at 14:14
  • No, there are only two votes on this answer. One up and one down. I suppose I can understand the attempt to make things simple, considering the person who asked this question is clearly a beginner. The misuse of code formatting and the answering of duplicates has become an epidemic problem, and I am not alone in downvoting answers like that. I've removed the downvote from this answer, though. (So now there is only one vote. :-) – Cody Gray - on strike Feb 25 '16 at 14:17
  • @CodyGray I will try not to missuse the code formatting anymore ;) And thanks for removing the downvote. – muXXmit2X Feb 25 '16 at 14:21