Solved

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

Posted on 2015-01-23
4
110 Views
Last Modified: 2015-01-23
Is it possible to pass objects as parameters and which options are available? What about returning as a result?
0
Comment
Question by:Nusrat Nuriyev
  • 2
  • 2
4 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
Comment Utility
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
 

Author Comment

by:Nusrat Nuriyev
Comment Utility
>>> 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
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
>>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
 

Author Closing Comment

by:Nusrat Nuriyev
Comment Utility
jkr, answered like a boss :)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now