question on const object - compilation error

S

subramanian100in

Consider the following program:

#include <iostream>

using namespace std;

class Base
{
public:
Base(int x = 0);

int val;
};

Base::Base(int x) : val(x)
{
}

class Test
{
public:
Test(Base & arg, Base * p);
Base & obj;
Base another;
Base * ptr;
};

Test::Test(Base & arg, Base * p) : obj(arg), another(arg), ptr(p)
{
}

int main()
{
Base a(9);
Base b(100);
const Test t(a, &b);
t.obj.val = 0;
t.another.val = 100;
t.ptr->val = 2000;

return 0;
}

When I compile this program under g++, I get
// x.cpp:36: error: assignment of data-member `Base::val' in read-only
structure

When I compile this program under VC++ 2005 Express Edition, I get
// x.cpp(36) : error: 't' : you cannot assign to a variable that is
const

When I have const Test t, the data member "Base another;"
becomes constant and hence the compilation error for modifying
t.another.val

But why the other two data members
Base & obj;
Base * ptr;
are not treated as const ? Why don't I get compilation error for
modifying
t.obj.val and t.ptr->val ?

Kindly explain.

Thanks
V.Subramanian
 
V

Victor Bazarov

Consider the following program:

#include <iostream>

using namespace std;

class Base
{
public:
Base(int x = 0);

int val;
};

Base::Base(int x) : val(x)
{
}

class Test
{
public:
Test(Base & arg, Base * p);
Base & obj;
Base another;
Base * ptr;
};

Test::Test(Base & arg, Base * p) : obj(arg), another(arg), ptr(p)
{
}

int main()
{
Base a(9);
Base b(100);
const Test t(a, &b);
t.obj.val = 0;
t.another.val = 100;
t.ptr->val = 2000;

return 0;
}

When I compile this program under g++, I get
// x.cpp:36: error: assignment of data-member `Base::val' in read-only
structure

When I compile this program under VC++ 2005 Express Edition, I get
// x.cpp(36) : error: 't' : you cannot assign to a variable that is
const

When I have const Test t, the data member "Base another;"
becomes constant and hence the compilation error for modifying
t.another.val

But why the other two data members
Base & obj;
Base * ptr;
are not treated as const ? Why don't I get compilation error for
modifying
t.obj.val and t.ptr->val ?

A member of a const object is const. I.e. you cannot change the value
of that object. If your 'Test' contains a member, and an instance of
'Test' is declared 'const', no members of that instance will be allowed
to change. However...

If I declare

struct A {
int *pointer;
};

what is the *nature* of the member? What's its *type*? What does
an instance of 'A' contain? Is that an 'int'? What cannot I change
when 'a' is declared as such

int i = 42;
A const a = { &i };

? Can I change 'i'? Can I change a.pointer? Can I change 'i' using
'a.pointer'? If any of the answers are 'yes', then can you show how?

Now, let's imagine that 'pointer' is not just a pointer to 'int', but
a pointer to another struct:

struct Int {
int value;
};

struct A {
Int *pointer;
};

Int i = { 42 };
A const a = { &i };

Looks about the same, right? Now, the same questions: can I change
'i'? Can I change 'a'? Can I change 'a.pointer'? Can I change the
object to which 'a.pointer' points? BTW, to what object does the
member 'a.pointer' points?

Can you do this as if it is your homework? Try to find the answers
yourself and then post them to see if you got them right.

V
 
D

digz

Consider the following program:

#include <iostream>

using namespace std;

class Base
{
public:
Base(int x = 0);

int val;

};

Base::Base(int x) : val(x)
{

}

class Test
{
public:
Test(Base & arg, Base * p);
Base & obj;
Base another;
Base * ptr;

};

Test::Test(Base & arg, Base * p) : obj(arg), another(arg), ptr(p)
{

}

int main()
{
Base a(9);
Base b(100);
const Test t(a, &b);
t.obj.val = 0;
t.another.val = 100;
t.ptr->val = 2000;

return 0;

}

When I compile this program under g++, I get
// x.cpp:36: error: assignment of data-member `Base::val' in read-only
structure

When I compile this program under VC++ 2005 Express Edition, I get
// x.cpp(36) : error: 't' : you cannot assign to a variable that is
const

When I have const Test t, the data member "Base another;"
becomes constant and hence the compilation error for modifying
t.another.val

But why the other two data members
Base & obj;
Base * ptr;
are not treated as const ? Why don't I get compilation error for
modifying
t.obj.val and t.ptr->val ?

ptr is an address
obj may be implemented as an address
note u are not actually changing the values of ptr, and obj
in ur assignments, ur changing what they point to..etc..so in terms on
Test nothing has changed..
t.obj.val and t.ptr->val ?

however t.another is changing
when u say
t.another.val = 0..and t.another is part of t so const declaration
will enforce that cant happen

Thx
Digz
 
D

Duzy Chan

const Test t(a, &b);
Of cause you make "t" a const variable, which makes all it's members
const, what does mean?

Look through the "class Test":
class Test
{
public:
Test(Base & arg, Base * p);
Base & obj;
Base another;
Base * ptr;
};

For the constant "t", we have constant member t.obj, t.another, t.ptr,
t.obj is of type "Base&", it's constant, but not the object it
referenced to becomes constant; and see t.ptr, of the type "Base*",
it's constant, which means the pointer is constant, you cannot
re-point to other object, but for the object t.ptr pointed to is never
constant; the object t.another is truely an constant Base object, you
cannot modifies it.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top