operator++ function

R

Rahul

Hi Everyone,

I have the following code,

class A
{
public : ~A()
{
printf("in ~A\n");
}
A operator++(int)
{
A obj;
printf("in operator function\n");
return(obj);
}
A()
{
printf("in A\n");
}
};


int main()
{
A obj;
printf("1\n");
obj++;
printf("2\n");
return(0);
}

and the output i get is,

in A
1
in A
in operator function
in ~A // Destructor for local object
in operator function
in ~A // Destructor is invoked for
which object???
2
in ~A

second, is it possible to have the operator++(int) [postfix operator+
+] to return a reference? As per my program it isn't possible as
returning reference of a local variable doesn't make any sense...

Thanks in advance!!!
 
J

Jeff Schwab

Rahul said:
I have the following code,

[code similar to:]

#include <iostream>

struct A {
A() { std::cout << "in A\n"; }
~A() { std::cout << "in ~A\n"; }

A operator++(int) {
std::cout << "in operator function\n";
return A();
}
};

int main() {
A obj;
std::cout << "1\n";
obj++;
std::cout << "2\n";
return 0;
}

and the output i get is,

in A
1
in A
in operator function
in ~A // Destructor for local object
in operator function
in ~A // Destructor is invoked for
which object???
2
in ~A

I think you may have copied either the code or the output incorrectly.
Your output indicates two invocations of operator++(int), but the code
shows only one.

What you should be seeing is:

in A
1
in operator function
in A
in ~A
2
in ~A

The inner constructor/destructor pair is for the temporary object
returned by the post-increment function.

second, is it possible to have the operator++(int) [postfix operator+
+] to return a reference? As per my program it isn't possible as
returning reference of a local variable doesn't make any sense...

It's possible, but it rarely makes sense. In general, it is better to
define the pre-increment operator first, and return the current object
by reference:

A& operator++() {
// ... increment this object ...
return *this;
}

If you need post-increment, you can them implement it like this:

A operator++(int) {
A const old_value = *this;
++*this;
return old_value;
}
 
R

Rahul

Rahul said:
I have the following code,

[code similar to:]

#include <iostream>

struct A {
A() { std::cout << "in A\n"; }
~A() { std::cout << "in ~A\n"; }

A operator++(int) {
std::cout << "in operator function\n";
return A();
}

};

int main() {
A obj;
std::cout << "1\n";
obj++;
std::cout << "2\n";
return 0;

}
and the output i get is,
in A
1
in A
in operator function
in ~A // Destructor for local object
in operator function
in ~A // Destructor is invoked for
which object???
2
in ~A

I think you may have copied either the code or the output incorrectly.
Your output indicates two invocations of operator++(int), but the code
shows only one.

What you should be seeing is:

in A
1
in operator function
in A
in ~A
2
in ~A

The inner constructor/destructor pair is for the temporary object
returned by the post-increment function.
second, is it possible to have the operator++(int) [postfix operator+
+] to return a reference? As per my program it isn't possible as
returning reference of a local variable doesn't make any sense...

It's possible, but it rarely makes sense. In general, it is better to
define the pre-increment operator first, and return the current object
by reference:

A& operator++() {
// ... increment this object ...
return *this;
}

If you need post-increment, you can them implement it like this:

A operator++(int) {
A const old_value = *this;
++*this;
return old_value;
}

Sorry, that was a editing mistake

in A
1
in A
in operator function
in ~A
in ~A /* this doesn't make sense */
2
in ~A
 
J

Jeff Schwab

Jeff said:
Rahul said:
I have the following code,

[code similar to:]

#include <iostream>

struct A {
A() { std::cout << "in A\n"; }
~A() { std::cout << "in ~A\n"; }

A operator++(int) {
std::cout << "in operator function\n";
return A();
}
};

int main() {
A obj;
std::cout << "1\n";
obj++;
std::cout << "2\n";
return 0;
}

and the output i get is,

in A
1
in A
in operator function
in ~A // Destructor for local object
in operator function
in ~A // Destructor is invoked for
which object???
2
in ~A

I think you may have copied either the code or the output incorrectly.
Your output indicates two invocations of operator++(int), but the code
shows only one.

What you should be seeing is:

in A
1
in operator function
in A
in ~A
2
in ~A

The inner constructor/destructor pair is for the temporary object
returned by the post-increment function.

second, is it possible to have the operator++(int) [postfix operator+
+] to return a reference? As per my program it isn't possible as
returning reference of a local variable doesn't make any sense...

It's possible, but it rarely makes sense. In general, it is better to
define the pre-increment operator first, and return the current object
by reference:

A& operator++() {
// ... increment this object ...
return *this;
}

If you need post-increment, you can them implement it like this:

A operator++(int) {

Whoops! That should be:

A const operator++(int) {

Otherwise, the client of this code might inadvertently call a non-const
method on the copy, but think they are modifying the original object.
For example, the following will by syntactically valid, but probably
will not do what the programmer expects:

A a;
++(a++);
 
B

Bo Persson

Rahul said:
Rahul said:
I have the following code,

[code similar to:]

#include <iostream>

struct A {
A() { std::cout << "in A\n"; }
~A() { std::cout << "in ~A\n"; }

A operator++(int) {
std::cout << "in operator function\n";
return A();
}

};

int main() {
A obj;
std::cout << "1\n";
obj++;
std::cout << "2\n";
return 0;

}
and the output i get is,
in A
1
in A
in operator function
in ~A // Destructor for local
object in operator function
in ~A // Destructor is invoked for
which object???
2
in ~A

I think you may have copied either the code or the output
incorrectly. Your output indicates two invocations of
operator++(int), but the code shows only one.

What you should be seeing is:

in A
1
in operator function
in A
in ~A
2
in ~A

The inner constructor/destructor pair is for the temporary object
returned by the post-increment function.
second, is it possible to have the operator++(int) [postfix
operator+ +] to return a reference? As per my program it isn't
possible as returning reference of a local variable doesn't make
any sense...

It's possible, but it rarely makes sense. In general, it is
better to define the pre-increment operator first, and return the
current object by reference:

A& operator++() {
// ... increment this object ...
return *this;
}

If you need post-increment, you can them implement it like this:

A operator++(int) {
A const old_value = *this;
++*this;
return old_value;
}

Sorry, that was a editing mistake

in A
1
in A
in operator function
in ~A
in ~A /* this doesn't make sense */
2
in ~A

It does, if you add a copy constructor to the class:

A(const A&)
{ printf("in copy constructor\n"); }

You will then see that the return statement copies the returned value,
and that copy has to be destructed:

in A
1
in A
in operator function
in copy constructor
in ~A
in ~A
2
in ~A



Bo Persson
 
D

Daniel T.

Rahul said:
Hi Everyone,

I have the following code,

class A
{
public : ~A()
{
printf("in ~A\n");
}
A operator++(int)
{
A obj;
printf("in operator function\n");
return(obj);
}
A()
{
printf("in A\n");
}
};


int main()
{
A obj;
printf("1\n");
obj++;
printf("2\n");
return(0);
}

and the output i get is,

Double check your output. Or try this code:

int count = 0;

class A
{
int id;
public :
~A() {
cout << "Object: " << id << " destroyed.\n";
}
A operator++(int) {
A obj;
cout << "In post-inc of object: " << id << '\n';
return obj;
}
A(): id( ++count ) {
cout << "Creating object: " << id << '\n';
}
A(const A& o): id( ++count ) {
cout << "Creating object: " << id << "as a copy of " << o.id <<
'\n';
}
A& operator=( const A& o ) {
cout << "Assigning object: " << o.id << " to " << id << '\n';
}
};


int main()
{
A obj;
cout << "at point 1\n";
obj++;
cout << "at point 2\n";
}

Your output should be:

Creating object: 1
at point 1
Creating object: 2
In post-inc of object: 1
Object: 2 destroyed.
at point 2
Object: 1 destroyed.
 
R

Rahul

Jeff said:
Rahul wrote:
[code similar to:]
#include <iostream>
struct A {
A() { std::cout << "in A\n"; }
~A() { std::cout << "in ~A\n"; }
A operator++(int) {
std::cout << "in operator function\n";
return A();
}
};
int main() {
A obj;
std::cout << "1\n";
obj++;
std::cout << "2\n";
return 0;
}
I think you may have copied either the code or the output incorrectly.
Your output indicates two invocations of operator++(int), but the code
shows only one.
What you should be seeing is:
in A
1
in operator function
in A
in ~A
2
in ~A
The inner constructor/destructor pair is for the temporary object
returned by the post-increment function.
second, is it possible to have the operator++(int) [postfix operator+
+] to return a reference? As per my program it isn't possible as
returning reference of a local variable doesn't make any sense...
It's possible, but it rarely makes sense. In general, it is better to
define the pre-increment operator first, and return the current object
by reference:
A& operator++() {
// ... increment this object ...
return *this;
}
If you need post-increment, you can them implement it like this:
A operator++(int) {

Whoops! That should be:

A const operator++(int) {

Otherwise, the client of this code might inadvertently call a non-const
method on the copy, but think they are modifying the original object.
For example, the following will by syntactically valid, but probably
will not do what the programmer expects:

A a;
++(a++);
A const old_value = *this;
++*this;
return old_value;
}

Isn't the temp object constant by default?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,179
Messages
2,570,956
Members
47,509
Latest member
Jack116

Latest Threads

Top