const/non-const operator

N

Nick Savoiu

In the code below how can I get x[3] to use the const T& operator[]?

Thanks,
Nick

#include <stdio.h>

template <typename T> class C
{
public:
T lv, rv;

const T& operator[](int p) const { printf("CONST\n"); return rv; }
T& operator[](int p) { printf("NON-CONST\n"); return lv; }
};

int main()
{
C<int> x;
return x[1]=x[3];
}
 
M

Mike Wahler

Nick Savoiu said:
In the code below how can I get x[3] to use the const T& operator[]?

Pass it a 'const C' object.
Thanks,
Nick

#include <stdio.h>

Aside: why not use iostreams?
template <typename T> class C
{
public:
T lv, rv;

const T& operator[](int p) const { printf("CONST\n"); return rv; }
T& operator[](int p) { printf("NON-CONST\n"); return lv; }
};

You need a default ctor. I added one to my adaptation
of your code below.
int main()
{
C<int> x;
return x[1]=x[3];
}

#include <stdio.h>

template <typename T> class C
{
public:
T lv, rv;

/* MKW added default constructor: */
C(const T& lvparm = T(), const T& rvparm = T())
: lv(lvparm), rv(rvparm) { }

const T& operator[](int p) const { printf("CONST\n"); return rv; }
T& operator[](int p) { printf("NON-CONST\n"); return lv; }
};

int main()
{
C<int> x;
return x[1]=(const C<int>(x))[3];
}

Output:

CONST
NON-CONST

Final note: The expression being returned here from main()
will only be portable if it evaluates to zero (in this
case 'x.rv' must have a value of zero.)

-Mike
 
N

Nick Savoiu

Pass it a 'const C' object.

Hmm, but that would make the code rather ugly... x[3] should be const by
virtue of being an r-value.

The code I posted was designed to be minimal so as to keep it simple.
Therefore no streams, constructors, etc.

Nick

Mike Wahler said:
Nick Savoiu said:
In the code below how can I get x[3] to use the const T& operator[]?


Thanks,
Nick

#include <stdio.h>

Aside: why not use iostreams?
template <typename T> class C
{
public:
T lv, rv;

const T& operator[](int p) const { printf("CONST\n"); return rv; }
T& operator[](int p) { printf("NON-CONST\n"); return lv; }
};

You need a default ctor. I added one to my adaptation
of your code below.
int main()
{
C<int> x;
return x[1]=x[3];
}

#include <stdio.h>

template <typename T> class C
{
public:
T lv, rv;

/* MKW added default constructor: */
C(const T& lvparm = T(), const T& rvparm = T())
: lv(lvparm), rv(rvparm) { }

const T& operator[](int p) const { printf("CONST\n"); return rv; }
T& operator[](int p) { printf("NON-CONST\n"); return lv; }
};

int main()
{
C<int> x;
return x[1]=(const C<int>(x))[3];
}

Output:

CONST
NON-CONST

Final note: The expression being returned here from main()
will only be portable if it evaluates to zero (in this
case 'x.rv' must have a value of zero.)

-Mike
 
J

Jonathan Mcdougall

In the code below how can I get x[3] to use the const T& operator[]?
Pass it a 'const C' object.
Thanks,
Nick

#include <stdio.h>

Aside: why not use iostreams?
template <typename T> class C
{
public:
T lv, rv;

const T& operator[](int p) const { printf("CONST\n"); return rv; }
T& operator[](int p) { printf("NON-CONST\n"); return lv; }
};

You need a default ctor. I added one to my adaptation
of your code below.
int main()
{
C<int> x;
return x[1]=x[3];
}

#include <stdio.h>

template <typename T> class C
{
public:
T lv, rv;

/* MKW added default constructor: */
C(const T& lvparm = T(), const T& rvparm = T())
: lv(lvparm), rv(rvparm) { }

const T& operator[](int p) const { printf("CONST\n"); return rv; }
T& operator[](int p) { printf("NON-CONST\n"); return lv; }
};

int main()
{
C<int> x;
return x[1]=(const C<int>(x))[3];

return x[1] = ( (const C said:
}

Output:

CONST
NON-CONST

Final note: The expression being returned here from main()
will only be portable if it evaluates to zero (in this
case 'x.rv' must have a value of zero.)


Jonathan
 
J

Jonathan Mcdougall

Please do not top-post. Rearranged.
In the code below how can I get x[3] to use the const T& operator[]?
template <typename T> class C
{
public:
T lv, rv;

const T& operator[](int p) const { printf("CONST\n"); return rv; }
T& operator[](int p) { printf("NON-CONST\n"); return lv; }
};
int main()
{
C<int> x;
return x[1]=x[3];
}
Pass it a 'const C' object.
int main()
{
C<int> x;
return x[1]=(const C<int>(x))[3];
}
Hmm, but that would make the code rather ugly... x[3] should be const by
virtue of being an r-value.

What is happening here is

x.operator[](3);

The operator[]() is called on 'x' and this decides which version is called.
Since 'x' is not const, the non-const version of operator[] is called.

C<int> x;
x[1]; // operator[]

const C<int> y;
y[1]; // const operator[]


Jonathan
 
N

Nick Savoiu

Mike Wahler said:
Nick Savoiu said:
Pass it a 'const C' object.

Hmm, but that would make the code rather ugly... x[3] should be const by
virtue of being an r-value.

It's not an rvalue. What specifically are you trying to do?


I'm trying to get the operator chosen to change based on the side (left or
right) that the operand is on. I was thinking that a compiler would favor a
const operator for an r-value.

Nick
 
M

Mike Wahler

Nick Savoiu said:
Mike Wahler said:
Nick Savoiu said:
Pass it a 'const C' object.

Hmm, but that would make the code rather ugly... x[3] should be const by
virtue of being an r-value.

It's not an rvalue. What specifically are you trying to do?


I'm trying to get the operator chosen to change based on the side (left or
right) that the operand is on. I was thinking that a compiler would favor a
const operator for an r-value.

It doesn't work that way. 'const' is chosen when
the object for which the operator is invoked is
const. You're still too close to the details.
Tell the real problem you're trying to solve,
not your proposed solution.

-Mike
 
R

Ron Natalie

Nick Savoiu said:
Pass it a 'const C' object.

Hmm, but that would make the code rather ugly... x[3] should be const by
virtue of being an r-value.

C++ does not base overload decisions on the return value of functions.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top