Board index » cppbuilder » Strange behaviour of the BCB 5.0 compiler

Strange behaviour of the BCB 5.0 compiler


2006-10-23 09:18:05 PM
cppbuilder108
In the example below the BCB 5.0 compiler issues the same warnings for
differently declared variables.
IMO the first one should be an error message!
#include <iostream>
#pragma hdrstop
#include <conio.h>
//---------------------------------------------------------------------------
int x = 1;
class WithStatic
{
private:
volatile static int x;
static int y;
public:
int getX() const { return x; }
int getY() const { return y; }
void setX( int theX ){ x = theX; }
void setY( int theY ){ y = theY; }
WithStatic( int theX = 0 ){ x = theX; }
};
volatile int WithStatic:: x;
int WithStatic:: y;
#pragma argsused
int main(int argc, char* argv[])
{
const WithStatic X;
std::cout << "X.x = " << X.getX() << std::endl;
std::cout << "X.y = " << X.getY() << std::endl;
X.setY( 10 ); // <== W8037 Non-const function WithStatic:setY(int)
called for const object
X.setX( 10 ); // <== W8037 Non-const function WithStatic:setX(int)
called for const object
std::cout << "X.x = " << X.getX() << std::endl;
std::cout << "X.y = " << X.getY() << std::endl;
std::cout << "\nPress any key to exit...";
getch();
return 0;
}
Vladimir Grigoriev
 
 

Re:Strange behaviour of the BCB 5.0 compiler

Vladimir Grigoriev wrote:
Quote
In the example below the BCB 5.0 compiler issues the same warnings for
differently declared variables.
IMO the first one should be an error message!

#include <iostream>
#pragma hdrstop
#include <conio.h>

//---------------------------------------------------------------------------
int x = 1;

class WithStatic
{
private:
volatile static int x;
static int y;
public:
int getX() const { return x; }
int getY() const { return y; }
void setX( int theX ){ x = theX; }
void setY( int theY ){ y = theY; }
WithStatic( int theX = 0 ){ x = theX; }
};

volatile int WithStatic:: x;
int WithStatic:: y;

#pragma argsused
int main(int argc, char* argv[])
{
const WithStatic X;
std::cout << "X.x = " << X.getX() << std::endl;
std::cout << "X.y = " << X.getY() << std::endl;

X.setY( 10 ); // <== W8037 Non-const function WithStatic:setY(int)
called for const object
X.setX( 10 ); // <== W8037 Non-const function WithStatic:setX(int)
called for const object

std::cout << "X.x = " << X.getX() << std::endl;
std::cout << "X.y = " << X.getY() << std::endl;

std::cout << "\nPress any key to exit...";
getch();

return 0;
}

Vladimir Grigoriev


Vladimir,
The compiler is telling you that you are calling a non-const function.
Because you used setter functions, the code in main does not know or
care that the x and y members inside WithStatic class are declared
differently.
The problem is that the WithStatic X is a a const. So you can only call
const functions of that object's class. Your getter functions are
declared as const functions, so they are OK to use with X. If you remove
the const from the definition of X in main then the compiler messages
will go away because it will be OK to modify X by calling the non-const
setter functions.
HTH
Dennis Cote