Is the temporary objects created by compiler is const?

In the following code snippet
myclass testTemp()
{
return myclass(3,4);

}
in this, The myclass object created by compiler is a const?
No, it's not const.

To make it const you would need to add the keyword const in front of it like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
const myclass testTemp()
{
return myclass(3,4);

}

//now the actual method is declared const
myclass testTemp() const
{
return myclass(3,4);

}


By the way, everything not allocated by the keywords new and new[] are stack variables (compile-time). And all functions that are not polymorphic methods of a class/struct are binded at compile-time.
Last edited on
answer - no.
It is not const.

The catch is that the caller does not actually get the same object of type myclass; instead, the copy constructor is called and relevant (usually all) data is copied. If myclass does not have a working copy constructor or assignment operator, you will experience bugs.

In the new C++ standard, this would actually be handled by a move constructor. The way it works now is analogous to giving a document to a coworker by copying the document, handing over the copy, and shredding the original. However, the speed benefits only come up if you write the extra constructor and assignment operator.
Ok, if the temporary objects are not const why the pgrm below giving errors?

//: C08:ConstReturnValues.cpp
// Constant return by value
// Result cannot be used as an lvalue
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();
}
const X f6() {
return X();
}
void f7(X& x) { // Pass by non-const reference
x.modify();
}
int main() {
f5() = X(1); // OK -- non-const return value
f5().modify(); // OK
// Causes compile-time errors:
 f7(f5()); 

}


OMG! it.s complely unreadable...
Last edited on
Is it readable now?
Ok, if the temporary objects are not const why the pgrm below giving errors?

//: C08:ConstReturnValues.cpp
// Constant return by value
// Result cannot be used as an lvalue
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();
}
const X f6() {
return X();
}
void f7(X& x) { // Pass by non-const reference
x.modify();
}
int main() {
f5() = X(1); // OK -- non-const return value
f5().modify(); // OK
// Causes compile-time errors:
f7(f5());
1
2
f5() = X(1); // OK -- non-const return value
f5().modify(); // OK 

That shouldn't be OK.

f5() returns an X object, but the object itself is temporary. It can be copied (X myX = f5();), but it can't be used. Why? For the exact reason you stated: it's a TEMPORARY object. Once the function call of f5 is done, the object is gone.

You can probably pass it by value, but not by reference (I don't think a return value has an address?).
try this:

1
2
X x = f5();
f7(x); 


or

1
2
3
4
5
6
7
8
9
10
X f5() {
return X();
}
void f7(X x) { // Pass by value
x.modify();
}

int main () {

f7(f5()); 


Don't bitch at me, I didn't write C++.
Last edited on
What about the following line in the code snippet?
// Causes compile-time errors:
f7(f5());
Why are you trying to return this:
1
2
3
X f5() {
return X();
}


In this function and (the next one of course) you are expected to return a X.
Let's see what do you return:
return X();
you are calling X default constructor inside a function so you create an object that exist only inside the scope of this function and then return it. Well as previous guys mentioned you are returning a already deleted object.
And no
1
2
X x = f5();
f7(x); 

would cause to maintain an address of a deleted object (at least your compiler considers it this way) not a good thing to do.

As far as I can see it you are experiencing with different versions of member function and so maybe I am guessing you wanted to return a on the stack allocated object which COULD be returned:
1
2
3
X *f5() {
return new X;
}


You can use it as you please BUT you must define a function to delete this object (using delete on the address returned by new) for example in a destructor

Also as an advice use some more descriptive function names. You wont remember a week after this what f5, f7 etc mean!
Topic archived. No new replies allowed.