473,387 Members | 1,534 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,387 software developers and data experts.

question on const object - compilation error

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

Nov 14 '07 #1
3 2090
su**************@yahoo.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 14 '07 #2
On Nov 14, 9:45 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
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

Nov 14 '07 #3
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.

Nov 15 '07 #4

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

Similar topics

5
by: Jim West | last post by:
Could someone please explain to me why the code segment class FOO { public: double *begin(); }; void bar(const FOO &foo) { foo.begin(); }
5
by: xuatla | last post by:
I have read FAQ but have not quite understood the usage of constant member function. class A { private: int n; double *v; // ....
5
by: hn.ft.pris | last post by:
Hi: I'm a beginer of STL, and I'm wondering why none of below works: ######################################################################## .......... string str("string"); if ( str == "s" )...
8
by: Bart Simpson | last post by:
I have a class that has a member that is a const reference: class MyClass { public: MyClass(const AnotherClass& ac); MyClass(const MyClass& mc); //.... private:
10
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class X { public: int value() const { return val; }
6
by: subramanian100in | last post by:
consider the following program: #include <iostream> #include <cstdlib> using namespace std; template<typename Tvoid fcn(T arg) { cout << "from fcn(T arg)" << endl;
2
by: junky_fellow | last post by:
guys, If I declare a const variable and then try to change it as follows; const int i=5; i = 10; What would be the behaviour? Should compiler give compilation error or Warning ? Or, would...
8
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format that I'm not understanding. I have code that...
5
by: Rahul | last post by:
Hi, Why does the following gives a compilation error int const * * p1= 0; int * * p2 = 0; p1 = p2; p1 has more const restriction than p2 so increasing const'ness should always be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.