4. Is it possible to pass objects as parameters and which options are available? What about returning as a result?

Is it possible to pass objects as parameters and which options are available? What about returning as a result?
Nusrat NuriyevAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
jkrConnect With a Mentor Commented:
>>1. Did you mean that the change which occures on the copied object absolutely does not affect to the original object?

Yes, the function that is called receives a copy of the object that you specify in the call. That copy is discarderd when the function returns, along with any local variables. When the call is made, the class' copy constructor and assignment are invoked, which causes additional overhead.

>>1.1 The copied object lives its own life, and has its own life cycle?

Absolutely, except that it contains exactly the same data as the original one.

>> 2 .Do you mean this:

Yes, exactly. That would be the 'const correct' version, albeit that's close to nipicking ;o)

>>3. So it's possible also to pass as pointer? Is there any difference in speed while

>>passing by pointer and passing by reference?
>>They basically affect to the original object. Should we prepare some way over other way?

Pointers and references are handled the same way technically  so there will not be much differene in speed in between these two.
"Preparing" here is the awareness of the programmer in regard what he or she is doing. If you want the object to be changed by the calling function, everything is fine. If you want to avoid that, use a const reference.

>>4. is it impossible to return object by reference? Why?

No, it a
ctually is quite common to do that. Howeve returning a reference of a pointer to a variable local to the called function will not work, since these are destroyed once the function returns. Common uses for returning references is the 'chaining' of operators, e.g. with 'operator<<()', which return the parameter they're passed in order to be used by the next '<<' operation. E.g.

#include <iostream>
class MyClass {

public:
  std::ostream& operator<<(std::ostream& os) {

    // write data to the stream
    // ...

    return os;
  }
};

int main () {

  MyClass a;

  //...

  // the following invokes 'MyClass::operator<<()'
  std::cout << "This is the data that 'MyClass a;' holds:" << a << std::endl;
}

Open in new window


>>5. Still do not get const stuff. Why we need const references?

As above: const references come in handy when you want to pass large objects, but also want them to not be tampered with. For that, they're ideal.
0
 
jkrConnect With a Mentor Commented:
Yes, that is possible. The available options are

- by value

The object will be copied during the call, all changes are null and void. Thus, you should declare it as a 'const' parameter to help the compiler optimise.

- by reference

This includes both using a real reference as a parameter (denoted by '&' being included in the called functions parameter list when it is declared) or pointers (denoted by using '*' in the declaration). Any changes will be reflected in the original instances you used to make the call. That's also one way to 'return' objects from functions, e.g.

class MyClass {

  // ...
};

void foo(MyClass& ref) {

  MyClass local_obj;

  // ...

   ref = local_object; // the calling function will receive the local 'MyClass' instance, it will be copied to
  // 'ref'
}

Open in new window


Note that there also a const references, which comes in handy when you don't want to copy large objects on the stack to make a function call

As for returning objects from function calls, pretty much the same applies. E.g.

class MyClass {

  // ...
};

MyClass foo() {

  MyClass local;

  // ...

  return local; // will be returned by value, i.e. physically copied when the function returns
}

int main () {

  MyClass obj = foo();
}

Open in new window

Another way is to return a pointer to an object, which saves you from the copying operation:
MyClass* foo() {

  MyClass* ptr = new MyClass();

  // ...

  return ptr; 
}

int main () {

  MyClass* p = foo();
}

Open in new window

0
 
Nusrat NuriyevAuthor Commented:
>>> The object will be copied during the call, all changes are null and void.
1. Did you mean that the change which occures on the copied object absolutely does not affect to the original object?
1.1 The copied object lives its own life, and has its own life cycle?

>>> Thus, you should declare it as a 'const' parameter to help the compiler optimise.

2 .Do you mean this:

class MyClass {

  // ...
};


void foo(const MyClass obj)
{
 
    MyClass local_obj;
    // ...
    local_obj = obj;
    
}

Open in new window


3. So it's possible also to pass as pointer? Is there any difference in speed while passing by pointer and passing by reference? They basically affect to the original object. Should we prefer some way over other way?
4. is it impossible to return object by reference? Why?

>>> Note that there also a const references, which comes in handy when you don't want to copy large objects on the stack to make a function call.

5. Still do not get const stuff. Why we need const references?
0
 
Nusrat NuriyevAuthor Commented:
jkr, answered like a boss :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.