473,385 Members | 2,269 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,385 software developers and data experts.

Two questions about const modifier

1. why can not static member function be declared as const?

class Demo
{
public:
static int method() const; // error!!
};

2. is this kind of const_cast usage secure?

class Demo
{
public:
static const int VALUE = 12;

void Read(int* pValue, int offset)
{
if (offset < 0 || offset >= 2)
return;
value[offset] = *pValue;
}

private:
int value[2];
};

int main()
{
int val1 = 0, val4 = Demo::VALUE;
Demo demo;
demo.Read(&val1, 0); // absolutely ok
demo.Read(const_cast<int*>(&val2), 1); // is const_cast secure here?
}

Dec 8 '06 #1
14 1390
int main()
{
int val1 = 0, val4 = Demo::VALUE;
Demo demo;
demo.Read(&val1, 0); // absolutely ok
demo.Read(const_cast<int*>(&val2), 1); // is const_cast secure here?

}
Sorry, main method should be:

int main()
{
int val1 = 0, val4 = Demo::VALUE;
Demo demo;
demo.Read(&val1, 0); // absolutely ok
demo.Read(const_cast<int*>(&Demo::VALUE), 1); // is const_cast secure
here?
demo.Read(&val2, 1); // use this?
}

Dec 8 '06 #2

Allen wrote:
1. why can not static member function be declared as const?

class Demo
{
public:
static int method() const; // error!!
};
Because a const function means that the function will not change the
instance it is called on. Static functions have no instance so can't
exactly change it...it just plain makes no sense for them to be const
as it's meaningless.

Dec 8 '06 #3

Allen wrote:
int main()
{
int val1 = 0, val4 = Demo::VALUE;
Demo demo;
demo.Read(&val1, 0); // absolutely ok
demo.Read(const_cast<int*>(&val2), 1); // is const_cast secure here?

}

Sorry, main method should be:

int main()
{
int val1 = 0, val4 = Demo::VALUE;
Demo demo;
demo.Read(&val1, 0); // absolutely ok
demo.Read(const_cast<int*>(&Demo::VALUE), 1); // is const_cast secure
here?
Absolutely not.

Dec 8 '06 #4
Absolutely not.
So need to write like this?

int main()
{
int val1 = 0, val4 = Demo::VALUE;
Demo demo;
demo.Read(&val1, 0);
demo.Read(&val2, 1);
return 0;
}

why is not secure to use const_cast for static const member address?

Dec 8 '06 #5
Absolutely not.
So need to write like this?

int main()
{
int val1 = 0, val2 = Demo::VALUE;
Demo demo;
demo.Read(&val1, 0);
demo.Read(&val2, 1);
return 0;
}

why is not secure to use const_cast for static const member address?

Dec 8 '06 #6

Allen wrote:
Absolutely not.

So need to write like this?

int main()
{
int val1 = 0, val4 = Demo::VALUE;
Demo demo;
demo.Read(&val1, 0);
demo.Read(&val2, 1);
return 0;
}
This won't work either because val2 doesn't exist.
>
why is not secure to use const_cast for static const member address?
Because modification of a const object causes undefined behavior. In
your case, a static integer constant, that variable may not even exist
in the final executable. Taking the address of course changes that but
it may still be in a location of memory that is considered unwritable
and can cause crashes or whatever.

The cast itself isn't the problem. It is the assumption that by doing
the cast you plan on writing to that location in memory.

Dec 8 '06 #7

"Noah Roberts дµÀ£º
The cast itself isn't the problem. It is the assumption that by doing
the cast you plan on writing to that location in memory.
In fact, I use the static const address to READ value, not WRITE.

value[offset] = *pValue; // pValue point to Demo::VALUE

But, according to you, it is sure not secure because the integer may
not
exist in the excutable file.

Thanks.

Dec 8 '06 #8
One more question about const.

INT32 CFileLogger::Output(const COutLogMesg& logMesg)
{
fstream fs;
fs.open(CLogger::GetLogFile(), ios::in | ios::out);

if (!fs.good())
{
return 0;
}

CHAR szBuf[512];
INT32 nLength = const_cast<COutLogMesg&>(logMesg).FormatMesg(szBuf ,
512);
if (nLength <= 0)
{
...
}
}

I want to pass reference to Output method and limit it not to be
changed. If I the above line
to be
INT32 nLength = logMesg.FormatMesg(szBuf, 512);
The compiler will tell an error saying that
'COutLogMesg::FormatMesg' : cannot convert 'this' pointer from 'const
COutLogMesg' to 'COutLogMesg &'

Why?

Dec 8 '06 #9
Allen wrote:
"Noah Roberts дµÀ£º
The cast itself isn't the problem. It is the assumption that by doing
the cast you plan on writing to that location in memory.

In fact, I use the static const address to READ value, not WRITE.

value[offset] = *pValue; // pValue point to Demo::VALUE

But, according to you, it is sure not secure because the integer may
not
exist in the excutable file.
Actually, he already pointed out that taking its address forces it to
exist (so I'm not sure what his point was, exactly). I suspect he
misread the code and believed you were writing to the value.

The const_cast<is actually valid, so long as you don't try to write
to it.

I suppose you already know that Read() is poorly written, though, and
since it doesn't modify its argument, would've been much better
specified as Read(const int *pValue, int value), in which case no
const_cast<would ever have been necessary.

Dec 8 '06 #10
IR
Allen wrote:
"Noah Roberts дµÀ£º
>The cast itself isn't the problem. It is the assumption that by
doing the cast you plan on writing to that location in memory.

In fact, I use the static const address to READ value, not WRITE.

value[offset] = *pValue; // pValue point to Demo::VALUE

But, according to you, it is sure not secure because the integer
may not
exist in the excutable file.
As Noah said, casting the const away implies that you intend
modifying it. The problem has nothing to do with the existence of
the variable in the resulting binary file.
There are very few legitimate uses of casts in C++. Unless you
really know what you are doing, using a cast generally means that
there is a design problem elsewhere.

Casting is like saying to the compiler: "Get away from my code I
don't need you anymore, you picky language enforcer". And most of
the time this is a Very Bad Thing.
If you don't modify the value in your function, why not declare a
pointer to const in the first place?

void Read(const int* pValue, int offset)

This way, you don't have to cast Demo::VALUE's constness away, and
just reading Read() declaration allows anyone to know that *pValue
will NEVER be modified inside the function. Moreover, if you
accidentally try to modify *pValue, the compiler will yell at you,
which is a good thing.
Cheers,
--
IR
Dec 8 '06 #11
Yes, I see now.
Thank you very much.

Dec 8 '06 #12

Allen wrote:
"Noah Roberts дµÀ£º
The cast itself isn't the problem. It is the assumption that by doing
the cast you plan on writing to that location in memory.

In fact, I use the static const address to READ value, not WRITE.

value[offset] = *pValue; // pValue point to Demo::VALUE
In that case the parameter should be const and then there is no
problem.

Another very important reason why this type of code is not "secure" is
that it removes safety. Imagine that at some point Read is modified is
such a way that it now modifies it's parameters. This would be a
natural thing to do for a maintainer since the parameters are not const
so obviously nobody is going to call Read with a const object...

The compiler will never complain because someone used a const_cast to
get it to shut up about the const parameter. Undefined behavior will
be introduced and might very well only show up in certain obscure
situations many months after anyone has touched any of the code related
to the problem.

No, the use of const_cast in that way in insecure in that it removes
security features and leaves you at the mercy of fate.

Dec 8 '06 #13

Noah Roberts wrote:
No, the use of const_cast in that way in insecure in that it removes
security features and leaves you at the mercy of fate.
Note that one of the big benefits of const_cast exisiting is that if
you are faced with a legacy API that does not declare parameters const
when it should (i.e. when the function in question really does not
modify the variable passed to it), you can use const correctly
throughout your own code and use const_cast to stop the
const-incorrectness propogating outside the API into your own code.

That doesn't seem to be the case here because the OP can simply modify
the called function to have a const-correct signature. But when you
don't have that option, using const_cast in the way described in this
thread is exactly the thing to do.

Gavin Deane

Dec 8 '06 #14

Gavin Deane wrote:
Noah Roberts wrote:
No, the use of const_cast in that way in insecure in that it removes
security features and leaves you at the mercy of fate.

Note that one of the big benefits of const_cast exisiting is that if
you are faced with a legacy API that does not declare parameters const
when it should (i.e. when the function in question really does not
modify the variable passed to it), you can use const correctly
throughout your own code and use const_cast to stop the
const-incorrectness propogating outside the API into your own code.

That doesn't seem to be the case here because the OP can simply modify
the called function to have a const-correct signature. But when you
don't have that option, using const_cast in the way described in this
thread is exactly the thing to do.
Yes, sometimes it is a necissary evil but it is never "secure".

Dec 8 '06 #15

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

Similar topics

3
by: CoolPint | last post by:
If I were to design my own container and its iterators, I understand that I need to provide both "iterator" and "const_iterator" so that begin() and end() return appropriate ones depending on the...
3
by: Zork | last post by:
Hi, I am a little confused with const functions, for instance (here i am just overloading the unary+ operator) if i define: 1) Length Length :: operator+ ( void ) const {return * this;} ... I...
11
by: Der Andere | last post by:
What exactly is the sense of using a "const" modifier before a function declaration? Actually, you can use it in three places. For instance, take a look at the following function declaration (from...
18
by: DaveLessnau | last post by:
I'm trying to learn C on my own and, apparently, my brain went on vacation somewhere. I just can't figure out how to parse the following function call: "void fillDeck( Card * const wDeck, const...
18
by: hzmonte | last post by:
typedef int t_compare_func(const void *, const void *); struct node *tree_search(struct node *root, const void *keyy, t_compare_func *comp) { struct node *cur_item; int result; if (root ==...
20
by: Blah | last post by:
In MSDN documentation it states.. Remarks The constant declaration can declare multiple constants, for example: public const double x = 1.0, y = 2.0, z = 3.0; The static modifier is not...
9
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); } ...
4
by: Ben Petering | last post by:
Hi group, this is a 'best practice' type question (I want discussion of the issue - whys and why nots - similar to "casting the return value of malloc()", to cite an analogous case). Let's...
39
by: Leonardo Korndorfer | last post by:
Hi, I'm litle confused by the const modifier, particularly when use const char* or char*. Some dude over here said it should be const char when you dont modify it content inside the function, I...
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$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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...
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,...

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.