People used to use reference in operator overloading like this

Compex operator+(const Complex& c1, const Complex& c2);

because its good for reference and static data also?
(c1, c2 can be complex or complex reference also? or mixed too)

Since

Compex operator+(const Complex c1, const Complex c2);

is good for only static data?
Therfore the previous one is more preferable?

>Therfore the previous one is more preferable?
Passing objects by const reference is preferable because it's pretty much always more efficient than passing by value. The reference generally takes up less memory than an object instance, and the cost of a copy constructor call is avoided.

And if we put a static object into the operand the compiler automatically converts into its reference?

If the class has only two double member data and functions, than its better to pass it by reference?
I mean passing two double is better than passing such kind of class not by reference? (because we have to pass not only the data but functions also?)

given valid object x of type X:
X x;

if prototype is void foo(X&); and function call is
foo(x); //compiler automatically converts x from type X to X&

if prototype is void bar(X*); and function call is
bar(x); //no automatic conversion of x from type X to type X* by compiler so this should be flagged as error by compiler.

Using references in function calls passes the address of the actual object and doesn't require additional memory be allocated.

Passing objects by value requires that a "temporary" object be created, thus requiring addtional memory and the time necessary to allocate that memory, etc.

I suppose you could argue that the language could be written without passing by value, using a const reference instead of a copy of the object, but that's not how it was set up.

So its definitelly better to pass everything by reference not just classes but builtin type like a simple double also?

>So its definitelly better to pass everything by reference not
>just classes but builtin type like a simple double also?

Not necessarily. A reference is likely to take up more memory than a char object, for instance, so there's little point in using a reference unless you need to modify the char object. Don't think of things in terms of "definitely" or "always". Instead, think of things in terms of "in this case...", and apply your knowledge to come up with the answer.

So the question is how big is a pointer? it depends on the RAM of the system? tipically it is greater than a double or not?

>So the question is how big is a pointer?
>tipically it is greater than a double or not?

It's implementation-defined, but easy to test:

#include <iostream>

int main()
{
  std::cout<<"double:  "<< sizeof ( double ) <<'\n';
  std::cout<<"double*: "<< sizeof ( double* ) <<'\n';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.