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

Const memebr function behaviour.

Can somebody can explain the following behaviour. I have a small code
as below:

class B
{
public:
int b;
B () { b = 0; };
void foo2 (int h) { b = h; } // non-const function
};

class A
{
public:
int a;
A () { cout << "Inside default constructor" << endl; b = new B
();}

B* b;

void bar () const { b->foo2 (3); } // problem. how can this
compile ?
};
void foo (const A* a)
{
a->bar ();
}

int
main (int argc, char** argv)
{
A a1;
foo (&a1);
}

My concern is how could this code compile when in bar () function of
class A, I am calling non const function on pointer b ?

If instead of pointer, b is an object of type B, I get correct error
saying that inside const member function of class A, I cannot call non
const member function of B.

Thanks in advance,

-Aseem.

May 18 '07 #1
4 1745
Rusty wrote:
Can somebody can explain the following behaviour. I have a small code
as below:

class B
{
public:
int b;
B () { b = 0; };
void foo2 (int h) { b = h; } // non-const function
};

class A
{
public:
int a;
A () { cout << "Inside default constructor" << endl; b = new B
();}

B* b;

void bar () const { b->foo2 (3); } // problem. how can this
compile ?
};
void foo (const A* a)
{
a->bar ();
}

int
main (int argc, char** argv)
{
A a1;
foo (&a1);
}

My concern is how could this code compile when in bar () function of
class A, I am calling non const function on pointer b ?
Why would that be a problem?
If instead of pointer, b is an object of type B, I get correct error
saying that inside const member function of class A, I cannot call non
const member function of B.
But you have a pointer. The constness of a pointer isn't related to the
constness of the object it points to.

May 18 '07 #2
On 18 Maj, 16:15, Rusty <r_as...@yahoo.co.inwrote:
Can somebody can explain the following behaviour. I have a small code
as below:

class B
{
public:
int b;
B () { b = 0; };
void foo2 (int h) { b = h; } // non-const function

};

class A
{
public:
int a;
A () { cout << "Inside default constructor" << endl; b = new B
();}

B* b;

void bar () const { b->foo2 (3); } // problem. how can this
compile ?

};

void foo (const A* a)
{
a->bar ();

}

int
main (int argc, char** argv)
{
A a1;
foo (&a1);

}

My concern is how could this code compile when in bar () function of
class A, I am calling non const function on pointer b ?

If instead of pointer, b is an object of type B, I get correct error
saying that inside const member function of class A, I cannot call non
const member function of B.
If you have a B member and calls on the function that instance will be
const (since the function is const). However when you have a pointer
and call the function the *pointer* becomes const and not the object
it points to.

--
Erik Wikström

May 18 '07 #3

"Rusty" <r_*****@yahoo.co.inwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
B* b;
void bar () const { b->foo2 (3); } // problem. how can thiscompile ?
'b = 0;' would be illegal there.
Because you are not changing the pointer, but the pointed object, which is
not part of your object (the pointer is).

--*PaN!*
May 18 '07 #4
On May 18, 10:15 am, Rusty <r_as...@yahoo.co.inwrote:
Can somebody can explain the following behaviour. I have a small code
as below:

class B
{
public:
int b;
B () { b = 0; };
void foo2 (int h) { b = h; } // non-const function

};

class A
{
public:
int a;
A () { cout << "Inside default constructor" << endl; b = new B
();}

B* b;

void bar () const { b->foo2 (3); } // problem. how can this
compile ?

};

void foo (const A* a)
{
a->bar ();

}

int
main (int argc, char** argv)
{
A a1;
foo (&a1);

}

My concern is how could this code compile when in bar () function of
class A, I am calling non const function on pointer b ?

If instead of pointer, b is an object of type B, I get correct error
saying that inside const member function of class A, I cannot call non
const member function of B.

Thanks in advance,

-Aseem.
The member function bar () const is not modifying the instance of
class A in any way.
I see a new B in A's ctor but no corresponding deallocation.

May 18 '07 #5

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

Similar topics

5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
27
by: Sundar | last post by:
Do we have something called a const function ? I thought applying the 'const' specifier to a function is meaningless... but when i tried defining a const function it worked !! like... const int...
20
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
19
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
15
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...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
23
by: Kira Yamato | last post by:
It is erroneous to think that const objects will have constant behaviors too. Consider the following snip of code: class Person { public: Person(); string get_name() const
29
by: Rohit kumar Chandel | last post by:
Hi all, I have a doubt in const keyword. My understanding says that a variable declared const can not be modified by the module it is defined in. Then consider the following code segment:...
2
by: casul | last post by:
Hi All, Given the following code : char const & identity( char const &c ) { return c; } char const & f( ) { return identity('A'); } char const & g( ) { return 'A'; } Of course, the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.