why a macro defined as member function cant access the private data.

Hi All,

why a macro defined as member function cant access the private data.

class A
{
int x;
public:
#define func(x,y) x=y; //why error
}
Defines are not the part of the lanuage in any way. It is just a method to tell preprocessor (not the compiler!) to replace all entries of func(x, y) by x=y. You can replace them manually and see what it would look like. Only after replacing everything, file would be sent to the compiler.
Macros are expanded before compilation starts so they doesn't care about scope or anything. It doesn't matter where you define it. It's just dumb text replace feature.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

class A
{
int x;
public:
#define func(x,y) x=y;
};

int main()
{
	int a = 1;
	int b = 2;
	func(a, b); // This will be replaced by a=b; before the compilation starts.
	std::cout << a << ' ' << b << std::endl;
}
2 2
Last edited on
Topic archived. No new replies allowed.