By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,156 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,156 IT Pros & Developers. It's quick & easy.

Won't call a const member function

P: n/a
I've written the following code that won't compile, trimmed down to
just the pertinent stuff:

--- WindowClass.hxx ----------------------------------------------------
#include <set>

class Window;

class WindowClass {
public:
addWindow (Window const* const window);
private:
static std::set<Window*> s_constructing;
};
------------------------------------------------------------------------

--- WindowClass.cxx ----------------------------------------------------
#include "WindowClass.hxx"

using namespace std;

void WindowClass::addWindow (Window const* const window) {

if (s_constructing.count(window) > 0) {
<snip>
}

}
------------------------------------------------------------------------

The error I get is on the line:

if (s_constructing.count(window) > 0) {

The compiler tells me that there's no match for my call to .count, but
it has a near match const function. The thing I don't understand is,
why wouldn't my non-const function call a const function. I know it
won't work the other way around, but that's not what I'm trying to do.
I can get it to compile if I remove const from addWindow()'s
parameter, but I want to make this const correct. Can anyone enlighten
me?
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Dave C" <da*********@gmail.com> wrote in message
news:17**************************@posting.google.c om...
I've written the following code that won't compile, trimmed down to
just the pertinent stuff:

--- WindowClass.hxx ----------------------------------------------------
#include <set>

class Window;

class WindowClass {
public:
addWindow (Window const* const window);
private:
static std::set<Window*> s_constructing;
};
------------------------------------------------------------------------

--- WindowClass.cxx ----------------------------------------------------
#include "WindowClass.hxx"

using namespace std;

void WindowClass::addWindow (Window const* const window) {

if (s_constructing.count(window) > 0) {
<snip>
}

}
------------------------------------------------------------------------

You have a pointer to a constant object that you are trying to pass to a
member of a set of pointers to a non-constant object. You are trying to
implicitly convert a constant window to a window, and the compiler is tell
you that that would be bad.
Jul 22 '05 #2

P: n/a
In article <17**************************@posting.google.com >,
da*********@gmail.com (Dave C) wrote:
The compiler tells me that there's no match for my call to .count, but
it has a near match const function. The thing I don't understand is,
why wouldn't my non-const function call a const function.


I believe it is not complaining about the function, but about the data
type. Your s_constructing set would need to be...

static std::set<Window const*> s_constructing;

....for the 'count' to be happy.

--
Phillip Mills
Multi-platform software development
(416) 224-0714
Jul 22 '05 #3

P: n/a
da*********@gmail.com (Dave C) wrote:..
class WindowClass {
public:
addWindow (Window const* const window); ^^^^^ this is ignored private:
static std::set<Window*> s_constructing;
};

void WindowClass::addWindow (Window const* const window) {
The second 'const' here is mostly useless, if you can avoid
problems by removing it, then do so.
if (s_constructing.count(window) > 0) {


set<T>::count() expects a 'T *'. You can't convert
(const Window *) to (Window *) implicitly (it only works the
other way around, as you noted). You should use const_cast
to fix this:

if (s_constructing.count(const_cast<Window *>(window)) > 0) {
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.