473,508 Members | 2,236 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ const member function question

Hi

The following code compiles fine on VC++2003, but am I violating any
standards in writing such code? More specifically, why can a const
member function call a non-const member function of a reference type
member? It seems strange and unintuitive to me. Thanks!

class T
{
public:
void B() { a = 10; }
private:
int a;
};

class A
{
public:
A(T& t_): t(t_) {}
void Test() const
{
t.B();
}
private:
T& t;
};

int main()
{
T t;
A a(t);
a.Test();
}

Oct 26 '07 #1
1 1470
On 2007-10-26 10:21, in*************@gmail.com wrote:
Hi

The following code compiles fine on VC++2003, but am I violating any
standards in writing such code? More specifically, why can a const
member function call a non-const member function of a reference type
member? It seems strange and unintuitive to me. Thanks!

class T
{
public:
void B() { a = 10; }
private:
int a;
};

class A
{
public:
A(T& t_): t(t_) {}
void Test() const
{
t.B();
}
private:
T& t;
};

int main()
{
T t;
A a(t);
a.Test();
}
When a is const it means that a is const, not that any other objects
that a might know about are const too. Since t is a reference to a non-
const T there is nothing wrong with calling functions that modify t.
What happens when a is const it that you can not modify any of its
members, which means that you can not make a refer to some other object
(though that is not possible when a is non-const either, but if you make
t a pointer instead there would be a difference).

--
Erik Wikström
Oct 26 '07 #2

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

Similar topics

17
3252
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
2
3620
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
3
2302
by: qWake | last post by:
The C++ language standard stipulates at section 9.4.1 that " A static member function shall not be declared const " The question is: what problem(s) could possibly exist in allowing static...
3
3582
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
4
1963
by: Mahesh Tomar | last post by:
Dear Readers, I am porting my existing C code to C++. In my existing code there are numerous functions that has been defined with CONST qualifier. For eg. foo(const DATA_TYPE *x); DATA_TYPE is...
7
1827
by: Siemel Naran | last post by:
Hi. I have a function template <class InputIter, class OutputIter> void f(InputIter begin, InputIter end, OutputIter result); With c of type char* and cc of type const char*, the code...
9
2119
by: Alex | last post by:
Hi. I'll try my problem with this example: class C { protected: virtual int* getProtected(int index)=0; public: const int* get(int index) const { return (const int*) getProtected(index); } ...
15
2085
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this...
0
1213
by: lavanya81 | last post by:
Hi, I am a software engg. working as developer.Right now I am working in compiler validation project. And my question is - what are the possibilities of writing C codes using the keyword...
10
2155
by: subramanian100in | last post by:
The following is a beginner's question. Suppose TYPE1 and TYPE2 are two types for which suitable ctors and operator= are defined. Suppose I have class Test { TYPE1 mem1;
0
7227
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
7391
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...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.