##macro - difference between VC and GC
Jun 2, 2012 at 5:54am UTC
Following code compiles and runs fine in MSVC, but fails in GC. I'll appreciate the help in a)understanding the issue and b)how to make it work for both compilers.
http://codepad.org/hZSQY0QC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
class classA
{
public :
classA() {}
};
#define PROP(propname) \
classA& ##propname() {return _##propname;} \
const classA& const_##propname() {return _##propname;}
class classB
{
public :
classB() {};
PROP (prop1);
private :
classA _prop1;
};
int _tmain(int argc, char * argv[])
{
classB b;
return 0;
}
Jun 2, 2012 at 6:39am UTC
there is a difference in the operation of the ## macro
between GCC and MSVC which can confuse you - but in this case
it should not be an issue.
The macro should look like this:
1 2 3
#define PROP(propname) \
classA& propname() {return _##propname;} \
const classA& const_##propname() {return _##propname;}
Jun 2, 2012 at 7:17am UTC
Why do you want this macro? To save one line of code?
If you must have a macro for this, might as well go the whole hog:
1 2 3 4 5 6 7
#define DEFINE_PROPERTY( TYPE, NAME ) \
private: TYPE _property___##NAME ; \
public : \
TYPE & NAME() { return _property___##NAME ; } \
const TYPE & NAME() const { return _property___##NAME ; } \
const TYPE & const_##NAME() const { return _property___##NAME ; } \
void NAME( const TYPE& _value___ ) { _property___##NAME = _value___ ; }
And then:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <string>
struct A
{
DEFINE_PROPERTY( int , value ) ;
DEFINE_PROPERTY( std::string, name ) ;
};
int main()
{
A a ;
a.value(99) ;
a.name("toran" ) ;
std::cout << a.value() << ' ' << a.const_name() << '\n' ;
a.value() += 100 ;
a.name()[0] = 'T' ;
std::cout << a.const_value() << ' ' << a.name() << '\n' ;
}
Topic archived. No new replies allowed.