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

Const Qualifier question

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 some typedef
structure and offcourse x is a pointer to it. Needless to say my
intention for writing such functions in C was to protect the accident
write to x's content. So far so good. While porting to C++, I've mada
DATA_TYPE as class and I want to retain the sanity of existing C
function that has Const qualifier. However am having some trouble,
which is illustrated in the sample code below :

#include <iostream>
typedef struct _temp {
int a;
char b;
short int c;
}TEMP_STRUCT; // Define some temporary structure for illustration
purpose

class data_type
{
public :
void *address;
data_type(void);
~data_type();
void init_data_type();
void * getAddress() ;
};

data_type::data_type()
{
address = new TEMP_STRUCT;
}
inline void * data_type::getAddress()
{
return((void *)address);
}
inline void data_type::init_data_type()
{
address->a=1;
address->b='a';
address->c=1;
};

void foo(const data_type *);

#define GET_ADDRESS(object) ((object)->getAddress())

int main(int argc, char *argv[])
{
data_type temp_object;
temp_object.init_data_type();
foo(&temp_object);
}
void foo(const data_type *temp_object)
{
using namespace std;

if (((TEMP_STRUCT *)(GET_ADDRESS(temp_object)))->a)
cout << "Test Passed";
//Error error: passing `const
//data_type' as `this' argument of `void* data_type::getAddress
//()' discards qualifiers
}

In C functions like foo() were called with "const TEMP_STRUCT *"
argument therefore compiler took care of any accident write to its
content. However in C++ this argument is changed to a class, which
fetches the address to TEMP_STRUCT, however, I still want the class to
return the address in such a way that the content of address can't be
modified inside the function. However, above sample code doesn't work
because foo is called with const qualifier, therefore complier
complains, if I try to access any of its member function. However if
get_address is defined as " void * getAddress() const", the compiler
error goes away, however, const qualifier inside foo also looses its
meaning i.e. you can write to the content of data type.
My question is how can this class return address whose content can't
be modified and at the same time I dont have to change the prototype
of my existing C functions.
Am sorry, if this question doesn't belong here or the information I've
provided is not sufficient.
Thanks for your time,
Mahesh
Jul 22 '05 #1
4 1954
"Mahesh Tomar" <to*****@msn.com> wrote in message
news:46**************************@posting.google.c om...

See below.
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 some typedef
structure and offcourse x is a pointer to it. Needless to say my
intention for writing such functions in C was to protect the accident
write to x's content. So far so good. While porting to C++, I've mada
DATA_TYPE as class and I want to retain the sanity of existing C
function that has Const qualifier. However am having some trouble,
which is illustrated in the sample code below :

#include <iostream>
typedef struct _temp {
int a;
char b;
short int c;
}TEMP_STRUCT; // Define some temporary structure for illustration
purpose

class data_type
{
public :
void *address;
data_type(void);
~data_type();
void init_data_type();
void * getAddress() ;
void *getAddress() const;
};

data_type::data_type()
{
address = new TEMP_STRUCT;
}
inline void * data_type::getAddress()
inline void * data_type::getAddress() const
{
return((void *)address);
}
inline void data_type::init_data_type()
{
address->a=1;
address->b='a';
address->c=1;
};

void foo(const data_type *);

#define GET_ADDRESS(object) ((object)->getAddress())

int main(int argc, char *argv[])
{
data_type temp_object;
temp_object.init_data_type();
foo(&temp_object);
}
void foo(const data_type *temp_object)
{
using namespace std;

if (((TEMP_STRUCT *)(GET_ADDRESS(temp_object)))->a)
cout << "Test Passed";
//Error error: passing `const
//data_type' as `this' argument of `void* data_type::getAddress
//()' discards qualifiers
}


Look up 'const member function'.

-Mike
Jul 22 '05 #2
Mike,
Thanks for your time. However, I too have mentioned the solution in
my original e-mail where I stated "if member function,get_address, is
defined as " void * getAddress() const", the compiler error goes away,
however, const qualifier inside foo also looses its meaning i.e. you
can write to the content of data type."
Putting a const qualifier at the end of function will make the error
go away, however, if I do something like this
void foo(const data_type *temp_object)
{
using namespace std;

// This statement below will modify the content of temp_object and
that is
// not the intention.
((TEMP_STRUCT *)(GET_ADDRESS(temp_object)))->a = 2;
cout << ((TEMP_STRUCT *)(GET_ADDRESS(temp_object)))->a;
}
If you see above, the intent of foo () function which was suppose to
protect any accidental write to the content of temp_object is lost. In
short I want to maintain the sanity of such functions (that am porting
to C++).
Any insight into this will be really appreciated.

Thanks,
Mahesh

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<1J*****************@newsread1.news.pas.earth link.net>...
"Mahesh Tomar" <to*****@msn.com> wrote in message
news:46**************************@posting.google.c om...

See below.
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 some typedef
structure and offcourse x is a pointer to it. Needless to say my
intention for writing such functions in C was to protect the accident
write to x's content. So far so good. While porting to C++, I've mada
DATA_TYPE as class and I want to retain the sanity of existing C
function that has Const qualifier. However am having some trouble,
which is illustrated in the sample code below :

#include <iostream>
typedef struct _temp {
int a;
char b;
short int c;
}TEMP_STRUCT; // Define some temporary structure for illustration
purpose

class data_type
{
public :
void *address;
data_type(void);
~data_type();
void init_data_type();
void * getAddress() ;


void *getAddress() const;
};

data_type::data_type()
{
address = new TEMP_STRUCT;
}
inline void * data_type::getAddress()


inline void * data_type::getAddress() const
{
return((void *)address);
}
inline void data_type::init_data_type()
{
address->a=1;
address->b='a';
address->c=1;
};

void foo(const data_type *);

#define GET_ADDRESS(object) ((object)->getAddress())

int main(int argc, char *argv[])
{
data_type temp_object;
temp_object.init_data_type();
foo(&temp_object);
}
void foo(const data_type *temp_object)
{
using namespace std;

if (((TEMP_STRUCT *)(GET_ADDRESS(temp_object)))->a)
cout << "Test Passed";
//Error error: passing `const
//data_type' as `this' argument of `void* data_type::getAddress
//()' discards qualifiers
}


Look up 'const member function'.

-Mike

Jul 22 '05 #3
Thanks,
read.
is difficult to
top-post, it
Please.

Append your replies to the end or intersperse.

Mahesh Tomar wrote:
[Re-arranged]
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<1J*****************@newsread1.news.pas.earth link.net>...
"Mahesh Tomar" <to*****@msn.com> wrote in message
news:46**************************@posting.google .com...

See below.

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 some typedef
structure and offcourse x is a pointer to it. Needless to say my
intention for writing such functions in C was to protect the accident
write to x's content. So far so good. While porting to C++, I've mada
DATA_TYPE as class and I want to retain the sanity of existing C
function that has Const qualifier. However am having some trouble,
which is illustrated in the sample code below :

#include <iostream>
typedef struct _temp {
int a;
char b;
short int c;
}TEMP_STRUCT; // Define some temporary structure for illustration
purpose

class data_type
{
public :
void *address;
data_type(void);
~data_type();
void init_data_type();
void * getAddress() ;


void *getAddress() const;

};

data_type::data_type()
{
address = new TEMP_STRUCT;
}
inline void * data_type::getAddress()


inline void * data_type::getAddress() const

{
return((void *)address);
}
inline void data_type::init_data_type()
{
address->a=1;
address->b='a';
address->c=1;
};

void foo(const data_type *);

#define GET_ADDRESS(object) ((object)->getAddress())

int main(int argc, char *argv[])
{
data_type temp_object;
temp_object.init_data_type();
foo(&temp_object);
}
void foo(const data_type *temp_object)
{
using namespace std;

if (((TEMP_STRUCT *)(GET_ADDRESS(temp_object)))->a)
cout << "Test Passed";
//Error error: passing `const
//data_type' as `this' argument of `void* data_type::getAddress
//()' discards qualifiers
}


Look up 'const member function'.

-Mike


Mike,
Thanks for your time. However, I too have mentioned the solution in
my original e-mail where I stated "if member function,get_address, is
defined as " void * getAddress() const", the compiler error goes away,
however, const qualifier inside foo also looses its meaning i.e. you
can write to the content of data type."
Putting a const qualifier at the end of function will make the error
go away, however, if I do something like this
void foo(const data_type *temp_object)
{
using namespace std;

// This statement below will modify the content of temp_object and
that is
// not the intention.
((TEMP_STRUCT *)(GET_ADDRESS(temp_object)))->a = 2;
cout << ((TEMP_STRUCT *)(GET_ADDRESS(temp_object)))->a;
}
If you see above, the intent of foo () function which was suppose to
protect any accidental write to the content of temp_object is lost. In
short I want to maintain the sanity of such functions (that am porting
to C++).
Any insight into this will be really appreciated.

Thanks,
Mahesh


Have a look at:
http://www.parashift.com/c++-faq-lit...rrectness.html

See also section 18.11

Also, do yourself a favor and get rid of the macro.
const data_type *temp_object;
((TEMP_STRUCT *)(GET_ADDRESS(temp_object)))->a = 2;

translates to:
1. ((TEMP_STRUCT *) (temp_object->get_address()))->a = 2;
2. ((struct _temp *) (temp_object->get_address()))->a = 2;

The expression:
(temp_object->get_address())
returns a type of "void *", which you are casting to
a type of "struct _temp *".

As we look in the definition of class "data_type" above,
we find in the constructor, that "address" is
initialized as:
address = new _temp; /* simplified */

So, you have a casting circle:
1. Member address in class "data_type" is set to a new "_temp".
2. In function foo, the the address is fetched from
an instance of "data_type" and casted to a type "_temp".

Your program could be simplified by reducing the casts:
void foo (_temp * const const_temp_pointer)
{
if (const_temp_pointer->a)
{
cout << "Test Passed." << endl;
}
}

int main(void)
{
_temp temp_object;
foo(&temp_object);
}

or:
int main(void)
{
_temp * p_temp_object = new _temp;
foo(p_temp_object);
}
Did I miss something?
Are you trying to create some kind of smart pointer class?
{Smart pointers are also in the FAQ.}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #4
to*****@msn.com (Mahesh Tomar) wrote:
class data_type
{
public :
void *address;
data_type(void);
~data_type();
void init_data_type();
void * getAddress() ;
};
In C functions like foo() were called with "const TEMP_STRUCT *"
argument therefore compiler took care of any accident write to its
content. However in C++ this argument is changed to a class, which
fetches the address to TEMP_STRUCT, however, I still want the class to
return the address in such a way that the content of address can't be
modified inside the function. However, above sample code doesn't work
because foo is called with const qualifier, therefore complier
complains, if I try to access any of its member function. However if
get_address is defined as " void * getAddress() const", the compiler
error goes away, however, const qualifier inside foo also looses its
meaning i.e. you can write to the content of data type.


const void * getAddress() const;

(as well as your existing one). Then const objects will call this
one and return a const void *, and non-const objects will call
the existing one.
BTW using 'void *' is bad because of type safety issues, you
should consider using a template:

template<typename T>
class data_type
{
public:
T *address;
data_type(): address(new T) {}
~data_type() { delete address; }
void init_data_type();
T * getAddress() { return address; }
T const *getAddress() const { return address; }
};

Your init_data_type function would not have worked anyway
(you can't dereference a pointer to void). So:

data_type<TEMP_STRUCT>::init_data_type()
{
address->a=1;
address->b='a';
address->c=1;
}
Jul 22 '05 #5

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

Similar topics

2
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...
6
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
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...
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
2
by: subramanian100in | last post by:
Consider the following program: #include <stdio.h> void myfn(const int **a) { static int i, j, k; a = &i; a = &j;
5
by: amvoiepd | last post by:
Hi, My question is about how to use const properly. I have two examples describing my problem. First, let's say I have a linked list and from it I want to find some special node. I write the...
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...
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
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: 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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...
0
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...

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.