Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

What does return X() mean in this C++ program, as X is not an object?

Status
Not open for further replies.

booklog

Junior Member level 3
Joined
Jun 22, 2006
Messages
30
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,286
Activity points
1,441
In the below program, i want to know what function f5 is trying to do. What does
return X() mean, since X() is not a object.


class X
{
int i;
public:
X(int ii = 0);
void modify();
};

X::X(int ii)
{
i = ii;
}

void X::modify()
{
i++;
}

X f5()
{
return X();
}
 

Re: For C++ expert

can u post full program ......... i think its only a part of the program..
 

Re: For C++ expert

The return X() in the f5 function creates a temporary object of class X with the default constructor (ii=0) and copies it to the variable where you assigned the result. E.g.:

X myVar;

myVar = f5();

myVar.i will be equal to 0. (I don't remember if the compiler generates the default copy constructor)

Best regards,
Mauro H. Leggieri
**broken link removed**
 

Re: For C++ expert

mxmauro said:
The return X() in the f5 function creates a temporary object of class X with the default constructor (ii=0) and copies it to the variable where you assigned the result. E.g.:

X myVar;

myVar = f5();

myVar.i will be equal to 0. (I don't remember if the compiler generates the default copy constructor)

Best regards,
Mauro H. Leggieri
h**p://www.mauroleggieri.com.ar

The compiler generate the default copy constructor, destructer and conscructor without parameters
 

Re: For C++ expert

I dont completly understand your question. f5() func simply return X object with default construction and the func Modify() was never called. I think that's all.
 

For C++ expert

here is it returning a result of function X()
 

Re: For C++ expert

Hi,

X f5()
{
return X();
}

The function f5 creates temporary object of class X by calling the constructor you have declared X::X(int ii) with ii value with 0 and returns the same to the caller. Remember you have defined a single argument constructor with default value. If you dont supply a value while calling a constructor, it will call this single argument constructor with default value you specified ie 0 in this case.

If there is no copy-constructor, constructor, destructor then compiler will create.

Kars
 

For C++ expert

function f5 returns an X object by calling the constructor without any arguments.
 

Re: For C++ expert

f5() func simply return X object with default construction and the func
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top