Picking const vs. non-const member functions upon a call

C

cheeser

Hello all,

Please see the question in the code below...

Thanks!
Dave


#include <iostream>

using namespace std;

class foo
{
public:
foo(int d) : data(d) {}
void print() const {cout << "const" << endl;}
void print() {cout << "non-const" << endl;}

private:
int data;
};

int main()
{
foo a(42);

// The call below calls the non-const print();
// I want the const print() to be called!
// a.print();

// Here's one way to do it. Is there a more elegant way?
// Making a const is not within the scope of my question!
static_cast<const foo>(a).print();

return 0;
}
 
E

E. Robert Tisdale

cheeser said:
#include <iostream>

class foo {
private:
int data;
public:
foo(int d): data(d) { }
void print(void) const {std::cout << "const" << std::endl;}
void print(void) {std::cout << "non-const" << std::endl;}
};

int main(int argc, char* argv[]) {
const foo a(42);
a.print();
return 0;
}
 
M

Mike Wahler

cheeser said:
Hello all,

Please see the question in the code below...

Thanks!
Dave


#include <iostream>

using namespace std;

class foo
{
public:
foo(int d) : data(d) {}
void print() const {cout << "const" << endl;}
void print() {cout << "non-const" << endl;}

private:
int data;
};

int main()
{
foo a(42);

// The call below calls the non-const print();
// I want the const print() to be called!

The invoke the function with a const object.

const foo a(42);
// a.print();

// Here's one way to do it. Is there a more elegant way?

Define "elegant".
// Making a const is not within the scope of my question!

You're asking about const member functions, so yes it is,
whether you deny it or not. :)
static_cast<const foo>(a).print();

return 0;
}

What real specific problem are you trying to solve?

-Mike
 
J

Jonathan Mcdougall

Please see the question in the code below...
Thanks!
Dave


#include <iostream>

using namespace std;

Are you sure you want that?
class foo
{
public:
foo(int d) : data(d) {}
void print() const {cout << "const" << endl;}

This would be called with

const foo f;
f.print();
void print() {cout << "non-const" << endl;}

And that would be called with

foo f;
f.print();
private:
int data;
};

int main()
{
foo a(42);

// The call below calls the non-const print();
// I want the const print() to be called!
// a.print();

// Here's one way to do it. Is there a more elegant way?
// Making a const is not within the scope of my question!
static_cast<const foo>(a).print();

Whether a const member function will be called or not depends
on the constness of the object. Why do you want that? Perhaps
we could find another way.


Jonathan
 
C

cheeser

int main()
Whether a const member function will be called or not depends
on the constness of the object. Why do you want that? Perhaps
we could find another way.

Here's the original code again:

#include <iostream>

using namespace std;

class foo
{
public:
foo(int d) : data(d) {}
void print() const {cout << "const" << endl;}
void print() {cout << "non-const" << endl;}

private:
int data;
};

int main()
{
foo a(42);

// The call below calls the non-const print();
// I want the const print() to be called!
// a.print();

// Here's one way to do it. Is there a more elegant way?
// Making a const is not within the scope of my question!
static_cast<const foo>(a).print();

return 0;
}


OK, let me take a stab again at articulating what I'm after. There are two
member functions in class foo that I care about: print() and print() const.
If I had a const foo, I would be restricted to calling only print() const.
However, I don't have a const foo (it's the whole point of the problem;
please don't change it to const or you'll be answering a question different
than what I asked, and one that I know the answer too!). Therefore, it is
*semantically* valid for me to call either print() or print() const. This
can be verified by removing the non-const print(). In that case, the
non-const foo object will correctly invoke print() cont.

My question is nothing more than trying to verify that, other than with a
cast to const foo, it is not *syntactically* possible for me to call print()
const (even though it is *semantically* legal, as detailed above). In other
words, I can't do this:

a.print() cost;

Hmmm, it just occurred to me that there may be a way with using a
pointer-to-member, but that would be somewhat ugly too. I think I may have
exhausted the possibilities, but I'm merely throwing this out to those more
experienced than I for confirmation...
 
M

Mike Wahler

cheeser said:
Here's the original code again:

#include <iostream>

using namespace std;

class foo
{
public:
foo(int d) : data(d) {}
void print() const {cout << "const" << endl;}
void print() {cout << "non-const" << endl;}

private:
int data;
};

int main()
{
foo a(42);

// The call below calls the non-const print();
// I want the const print() to be called!
// a.print();

// Here's one way to do it. Is there a more elegant way?
// Making a const is not within the scope of my question!
static_cast<const foo>(a).print();

return 0;
}


OK, let me take a stab again at articulating what I'm after. There are two
member functions in class foo that I care about: print() and print() const.
If I had a const foo, I would be restricted to calling only print() const.
However, I don't have a const foo (it's the whole point of the problem;
please don't change it to const or you'll be answering a question different
than what I asked, and one that I know the answer too!). Therefore, it is
*semantically* valid for me to call either print() or print() const. This
can be verified by removing the non-const print(). In that case, the
non-const foo object will correctly invoke print() cont.

My question is nothing more than trying to verify that, other than with a
cast to const foo, it is not *syntactically* possible for me to call print()
const (even though it is *semantically* legal, as detailed above). In other
words, I can't do this:

a.print() cost;

Hmmm, it just occurred to me that there may be a way with using a
pointer-to-member, but that would be somewhat ugly too. I think I may have
exhausted the possibilities, but I'm merely throwing this out to those more
experienced than I for confirmation...

Since neither version of your 'print()' function modifies
the object, there isn't really any reason to define a non-const
'print()' function at all.

Just remove the nonconst version, and the const version
will be invoked for const or nonconst objects.

-Mike
 
C

cheeser

Just remove the nonconst version, and the const version
will be invoked for const or nonconst objects.

-Mike

That is correct, but it evades the whole point of the question! The question
may as well not even be asked if I go with something that doesn't address
the point of what I'm getting at!

I think by now I could probably safely conclude that the answer to my
question is that there's no way to do what I was asking about.

Thanks,
Dave
 
M

Mike Wahler

cheeser said:
That is correct, but it evades the whole point of the question!

I still fail to see the point.
The question
may as well not even be asked if I go with something that doesn't address
the point of what I'm getting at!

Which is?

Are you asking if you can circumvent the design of C++?
Yes, in many cases you can. Your casting example is how
in this particular case.
I think by now I could probably safely conclude that the answer to my
question is that there's no way to do what I was asking about.

No, not without a cast. I think I have a much better question:
*Why* do you want to do this?

-Mike
 
J

Jonathan Mcdougall

Whether a const member function will be called or not depends
Here's the original code again:

OK, let me take a stab again at articulating what I'm after.

That is not what I asked. Your question has been answered, it
is not possible to call a const member function from a non
const object if it is overloaded with a non-const function,
except with casting (which makes the object const, back to the
starting point).

My question was : why do you want that? Perhaps we could find
another way. Post some real code with your real design with
some real problems.


Jonathan
 
C

cheeser

Mike, Jonathan and Robert,

I merely wanted to verify that it is not possible (without undesirable casts
or other things of that ilk) to invoke a const method from a non-const
object if there is also a non-const method of the same name. I do not have
a specific application at hand; I just wanted confirmation that my
understnading that it can't be done is correct. You have provided that
confirmation. For this I offer my thanks.

A good evening to all,
Dave
 
J

John Carson

cheeser said:
Mike, Jonathan and Robert,

I merely wanted to verify that it is not possible (without
undesirable casts or other things of that ilk) to invoke a const
method from a non-const object if there is also a non-const method of
the same name. I do not have a specific application at hand; I just
wanted confirmation that my understnading that it can't be done is
correct. You have provided that confirmation. For this I offer my
thanks.

A good evening to all,
Dave

Same name yes, if the arguments can differ.

class foo
{
public:
foo(int d) : data(d) {}
void print(int a=0) const {cout << "const" << endl;}
void print() {cout << "non-const" << endl;}
private:
int data;
};


int main()
{
foo a(42);
const foo b(43);

a.print(); // non-const
a.print(0); // const
b.print(); // const
return 0;
}
 
K

Kevin Goodsell

cheeser said:
// The call below calls the non-const print();
// I want the const print() to be called!
// a.print();

// Here's one way to do it. Is there a more elegant way?
// Making a const is not within the scope of my question!
static_cast<const foo>(a).print();

Here's an alternative, but I don't know if I'd consider it any more elegant:

const foo &ca = a;
ca.print();

I believe that will do what you are asking.

-Kevin
 
G

Gianni Mariani

Here are a couple of other suggestions.

class foo
{
public:
foo(int d) : data(d) {}
void print() const {cout << "const" << endl;}
void print() {cout << "non-const" << endl;}
void cprint() const { print(); }
private:
int data;
};


int main()
{
foo a(42);
const foo & b(a);

a.print(); // non-const
b.print(); // const
a.cprint(); // const
return 0;
}
 
C

cheeser

Kevin Goodsell said:
Here's an alternative, but I don't know if I'd consider it any more elegant:

const foo &ca = a;
ca.print();

I believe that will do what you are asking.

-Kevin

Ahh, there it is! I had a feeling there had to be a way, but my mind just
didn't get on the right track... Thanks man...
 
J

Jerry Coffin

[ ... ]
// The call below calls the non-const print();
// I want the const print() to be called!

First of all, if you care much about this, it's probably a good clue
that your design has major problems.
// a.print();

// Here's one way to do it. Is there a more elegant way?
// Making a const is not within the scope of my question!
static_cast<const foo>(a).print();

You have (at least) a few other choices, such as temporarily creating a
reference to a const object, but they all come out the same. If a
member function is overloaded based on const-ness, then the const
overload should only be selected when you invoke it on (a reference or
pointer to) a const object. Therefore, if you want to invoke the const
member function, you need to treat the object as const, either through a
cast (as you've already mentioned) or by referring to the object via a
pointer or reference to const.
 
J

jeffc

cheeser said:
#include <iostream>

using namespace std;

class foo
{
public:
foo(int d) : data(d) {}
void print() const {cout << "const" << endl;}
void print() {cout << "non-const" << endl;}

private:
int data;
};

int main()
{
foo a(42);

// The call below calls the non-const print();
// I want the const print() to be called!
// a.print();

// Here's one way to do it. Is there a more elegant way?
// Making a const is not within the scope of my question!
static_cast<const foo>(a).print();

I don't know what the "scope" of your question is, but the obvious answer to
me would be to define
const foo a(42);
But if that's not what you want, I think you should question if you really
want a const and non-const version of the same function.
 
J

jeffc

Jonathan Mcdougall said:
My question was : why do you want that? Perhaps we could find
another way. Post some real code with your real design with
some real problems.

Why are you guys badgering this guy? He already said there is no point
other than knowing whether or not it can be done.
 
C

cpp_weenie

Kevin Goodsell said:
Here's an alternative, but I don't know if I'd consider it any more elegant:

const foo &ca = a;
ca.print();

I believe that will do what you are asking.

-Kevin

In fact, here's the full extent of possibilities:

#include <iostream>

using namespace std;

class foo
{
public:
void bar() {cout << "bar()" << endl;}
void bar() const {cout << "bar() const " << endl;}
void bar() volatile {cout << "bar() volatile" << endl;}
void bar() const volatile {cout << "bar() const volatile" << endl;}
};

int main()
{
foo p;
const foo &c = p;
volatile foo &v = p;
const volatile foo &cv = p;

p.bar();
c.bar();
v.bar();
cv.bar();

return 0;
}
 

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
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top