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
122 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Header of docx file 17 102
gdb doesn't stop on breakpoint 2 67
max float value 3 41
Why isn't object file created? 6 58
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

776 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