473,394 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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


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;
}

Jul 19 '05 #1
17 3240
cheeser wrote:
#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;
}


Jul 19 '05 #2
"cheeser" <ch**********@yahoo.com> wrote in message
news:vo************@news.supernews.com...

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
Jul 19 '05 #3
> 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
Jul 19 '05 #4
> > 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.


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...
Jul 19 '05 #5

"cheeser" <ch**********@yahoo.com> wrote in message
news:vo************@news.supernews.com...
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.


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
Jul 19 '05 #6
> 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
Jul 19 '05 #7

"cheeser" <ch**********@yahoo.com> wrote in message
news:vo************@news.supernews.com...
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!


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
Jul 19 '05 #8
> > 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:


<snipped 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
Jul 19 '05 #9
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
Jul 19 '05 #10
"cheeser" <ch**********@yahoo.com> wrote in message
news:vo************@news.supernews.com
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;
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 19 '05 #11
cheeser wrote:

// 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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #12
John Carson wrote:
"cheeser" <ch**********@yahoo.com> wrote in message
news:vo************@news.supernews.com
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.


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;
}

Jul 19 '05 #13

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:ie****************@newsread4.news.pas.earthli nk.net...
cheeser wrote:

// 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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


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...
Jul 19 '05 #14
In article <vo************@news.supernews.com>, ch**********@yahoo.com
says...

[ ... ]
// 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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #15

"cheeser" <ch**********@yahoo.com> wrote in message
news:vo************@news.supernews.com...

#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.
Jul 19 '05 #16

"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
news:N6********************@weber.videotron.net...

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.
Jul 19 '05 #17

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:ie****************@newsread4.news.pas.earthli nk.net...
cheeser wrote:

// 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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


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;
}
Jul 19 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Bart Nessux | last post by:
I am using method 'a' below to pick 25 names from a pool of 225. A co-worker is using method 'b' by running it 25 times and throwing out the winning name (names are associated with numbers) after...
2
by: Erik Max Francis | last post by:
I was interesting in adding selection and hit testing to ZOE, and was looking at the PyOpenGL wrappers' handling of selection and picking. I see glSelectBuffer to specify the size of the buffer,...
46
by: RoSsIaCrIiLoIA | last post by:
Write a function that gets an array of unsigned int fill it with random values all differents, and sorts it. It should be faster than qsort too. Do you like my solution? _______________________...
1
by: Adam Clauss | last post by:
I have a few variables that I want declared as global. To accomplish this, I made the files globals.h and globals.cpp. In globals.h I have the declarations for the structs, and one variable...
5
by: kpp9c | last post by:
I have a several list of songs that i pick from, lets, say that there are 10 songs in each list and there are 2 lists. For a time i pick from my songs, but i only play a few of the songs in that...
8
by: Candace | last post by:
I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the...
3
by: prassaad | last post by:
HI! I 'm working on linux .I hv difficulty for picking particular line from a file... I like to make some manipulation on LINE such as 3 sda7 333333 66666 888888 444444 from file /PROC/DISKSTATS...
2
by: manuboy | last post by:
i am struggling to come up with a c program that is capable of picking up the Y, U and V parameters from a video stream and compare them with that of another stream to calculate the PSNR. i was...
0
by: Jonathan | last post by:
I have 3 webforms on 1 page each with Submit buttons. VB.net code activated by clicking the submit button in form 3 is not picking up the value of a field in form 2. It is just returning Null or...
9
by: newbiegalore | last post by:
Hello everyone :-) , Thanks to the gentle people on this group for helping me out with previous issues. :-D This time round I am facing what I perceive as a simple problem, which I have not found...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.