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
140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 40567463
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
ID: 40567545
>>> 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
ID: 40567555
>>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
ID: 40567579
jkr, answered like a boss :)
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

627 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