Hi !
The code below compiles without warning or error in gcc 3.3.4,
3.4.2 and 4.0 (from CVS).
However I think the const function B::func() must not compile
since it changes B.
When I substitute the pointer to A (A* _a) in class B by an object
of type A (removing the new in the ctor) then I get a compiletime
error as expected.
Therefor my question:
Does using a pointer to A instead of an object of type A directly
kind of make A "not part of B" (and therefor gcc behaves correct)
or is this a bug in gcc ?
The compile commands I used are:
g++ -s -o test test.cpp
g++ -s -Wall -o test test.cpp
g++ -s -Wall -pedantic -o test test.cpp
g++ -s -Wall -pedantic -O0 -o test test.cpp
=2D--------------------------------------------------------------------
#include <iostream>
using namespace std;
typedef struct tagA
{
int a;
} A;
class B
{
public:
B() {_a =3D new A; (*_a).a=3D-1; cout << "ctor: a=3D" << (*_a).a << end=
l;}
A& a() const {return *_a;}
void func() const;
private:
A* _a;
};
void B::func() const
{
++a().a; // IMO this should create a compile-time error
}
int main(int argc, char** argv)
{
B b;
cout << "before: a=3D" << b.a().a << endl;
b.func();
cout << "after: a=3D" << b.a().a << endl;
exit(0);
}
=2D--------------------------------------------------------------------
Best,
Michael
=2D-=20
Vote against SPAM - see http://www.politik-digital.de/spam/
Michael Gerdau email: mg...@te...
GPG-keys available on request or at public keyserver
|