Hello everyone,
In MSDN sample for const_cast, http://msdn2.microsoft.com/en-us/lib...5h(VS.80).aspx
There is a statement like this,
--------------------
On the line containing the const_cast, the data type of the this
pointer is const CCTest *. The const_cast operator changes the data
type of the this pointer to CCTest *
--------------------
I think in a const member function, like void printNumber() const, the
type of this pointer is const pointer to current type, so we use this
const_cast to remove its const properties.
For a non-const member function, I think this pointer should not be a
const pointer, right?
So, conclusion is,
1. in const member function, this pointer is a const pointer;
2. in non-const member function, this pointer is a non-const pointer.
Right?
thanks in advance,
George 2 2128
George2 wrote:
Hello everyone,
In MSDN sample for const_cast,
http://msdn2.microsoft.com/en-us/lib...5h(VS.80).aspx
There is a statement like this,
--------------------
On the line containing the const_cast, the data type of the this
pointer is const CCTest *. The const_cast operator changes the data
type of the this pointer to CCTest *
--------------------
I think in a const member function, like void printNumber() const, the
type of this pointer is const pointer to current type, so we use this
const_cast to remove its const properties.
For a non-const member function, I think this pointer should not be a
const pointer, right?
So, conclusion is,
1. in const member function, this pointer is a const pointer;
2. in non-const member function, this pointer is a non-const pointer.
Right?
My simple test (which may or may not work in your implementation) seems to
suggest this.
Output is:
class Foo *
class Foo const *
#include <iostream>
class Foo
{
public:
void ShowType() { std::cout << typeid(this).na me() << "\n"; }
void ShowTypeConst() const { std::cout << typeid(this).na me() << "\n"; }
private:
};
int main()
{
Foo Bar;
Bar.ShowType();
Bar.ShowTypeCon st();
}
But, yes, in a const member function, the this* is const.
--
Jim Langston ta*******@rocke tmail.com
Jim Langston wrote:
[..]
But, yes, in a const member function, the this* is const.
I'd probably say that "the *this is const". I am sure you
meant to dereference the pointer.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: drowned |
last post by:
all right, check it out... I've got a practice exercise from "thinking
in c++" whose answer isn't covered in the annotated solutions guide,
so I'm trying to handle it, but I don't understand what I'm doing
wrong. Here is the exercise:
27. Create a const array of double and a volatile array of double.
Index through each array and use...
|
by: R. Anbeeswaran |
last post by:
Hi All,
void main()
{
const int i = 20;
int *p = const_cast<int*>(&i);
*p = 40;
cout <<"i="<< i <<"\t"<<"*p="<<*p<<"\n";
}
|
by: S.Senthilvel |
last post by:
hi,
I am a little confused about the const_cast.I've thought it this way till
now.
//Proper cast
int i = 10;
const int* pci = &i;
int *pi = const_cast<int*>(pci);
|
by: johny smith |
last post by:
Can someone please give me some good reference site/information for
understanding when and why I would use
1.) const_cast
Don't know why you would do this?
2.) reinterpret_cast.
I have used this a little when I converted a primitive to a class, but
would like to see other applications.
|
by: AlesD |
last post by:
Hello,
I can't figure out how to build assignment operator for class which
contains "type* const value".
--- example ---
class parent_t;
class child_t {
| |
by: CoolPint |
last post by:
While I was reading about const_cast, I got curious and wanted to know
if I could modify a constant variable through a pointer which has been
"const_cast"ed. Since the pointer would be pointing to the constant
variable and if I changed the value through the pointer, the constant
variable should have been modified. Well, that's what I thought...
|
by: Simon Bailey |
last post by:
In the following code at the end of the program z = 20 & y = 99.
void doit(const int* x)
{
int* nonconst;
nonconst = const_cast<int*>(x);
*nonconst = 99;
}
int main(int argc, char* argv)
|
by: Alexander Stippler |
last post by:
Hello,
I wonder if and in what cases casting const away by a const_cast can be
dangerous. In one place the only way I can imagine to realize a pretty
nice feature is casting away const. Now I wonder if this is dangerous. I
have something like that:
const Object &
Object::dummy(const AType &a) const
{
|
by: Squeamizh |
last post by:
class my_class {
public:
my_class() : value(0) { }
int& get_value() { return value; }
const int& get_value() const {
my_class& c = const_cast<my_class&>(*this);
return c.get_value();
}
|
by: George2 |
last post by:
Hello everyone,
In MSDN sample for const_cast,
http://msdn2.microsoft.com/en-us/library/bz6at95h(VS.80).aspx
There is a statement like this,
--------------------
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |