M
Mahesh Tomar
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
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