Put data member into array

I

Immortal Nephi

I always think about pointer to member function array. Now, I think
about pointer to data member array. Compare both. Let me know what
you think. The data members are like colors. The data member array
needs to access data members directly. If you change the color, then
data member array receive new colors. My code is ideal for font
image. The colors are like foreground and background.

You can see my code for curiousity.

class Test
{
public:
Test() : a( 1 ), b( 2 ), c( 3 )
{
}

~Test() {}

void dosomething()
{
( this->*pF[ 0 ] )();
const int d = this->*rgData[ 0 ];
const int d2 = this->*rgData[ 1 ];
const int d3 = this->*rgData[ 2 ];

a += 2;
b += 2;
c += 2;

const int d4 = this->*rgData[ 0 ];
const int d5 = this->*rgData[ 1 ];
const int d6 = this->*rgData[ 2 ];
}

private:
void RunA() {}
void RunB() {}
void RunC() {}

static void ( Test::* const pF[ 3 ] )();

int a;
int b;
int c;
static const int Test::*rgData[ 3 ];
};

void ( Test::* const Test::pF[ 3 ] )() = { &Test::RunA, &Test::RunB,
&Test::RunC };
const int Test::* Test::rgData[ 3 ] = { &Test::a, &Test::b,
&Test::c };

int main()
{
Test t;
t.dosomething();

return 0;
}
 
V

Victor Bazarov

Immortal said:
I always think about pointer to member function array.

You do? Interesting. I can't remember *ever* having any topic that I
would "always think about".
> Now, I think
about pointer to data member array. Compare both. Let me know what
you think.

I think you shouldn't have those arrays static. It's rather strange to
have them static and have non-static data along with that and have those
data changed by a non-static member function. Another thought is, why
arrays? Why can't you just have three members that are named "*_a",
"*_b", etc.? It's not like you iterate over those somehow... And if
you would need to change one of those funcitons' type, it would be
easier if they aren't in an array...
> The data members are like colors. The data member array
needs to access data members directly. If you change the color, then
data member array receive new colors. My code is ideal for font
image. The colors are like foreground and background.

You can see my code for curiousity.

class Test
{
public:
Test() : a( 1 ), b( 2 ), c( 3 )
{
}

~Test() {}

void dosomething()
{
( this->*pF[ 0 ] )();
const int d = this->*rgData[ 0 ];
const int d2 = this->*rgData[ 1 ];
const int d3 = this->*rgData[ 2 ];

a += 2;
b += 2;
c += 2;

Non-static member changing static data: weird.
const int d4 = this->*rgData[ 0 ];
const int d5 = this->*rgData[ 1 ];
const int d6 = this->*rgData[ 2 ];
}

private:
void RunA() {}
void RunB() {}
void RunC() {}

static void ( Test::* const pF[ 3 ] )();

int a;
int b;
int c;
static const int Test::*rgData[ 3 ];
};

void ( Test::* const Test::pF[ 3 ] )() = { &Test::RunA, &Test::RunB,
&Test::RunC };
const int Test::* Test::rgData[ 3 ] = { &Test::a, &Test::b,
&Test::c };

int main()
{
Test t;
t.dosomething();

return 0;
}

V
 
R

red floyd

You do?  Interesting.  I can't remember *ever* having any topic that I
would "always think about".

Victor, give him a break. His primary language is clearly not
English.
Or at least one of the western dialects (I'm not sure about what
happens
in India).

Plus, his questions are coming with source code, and they're on-topic.
What more do you want?
 
V

Victor Bazarov

red said:
Victor, give him a break. His primary language is clearly not
English.

Neither is mine. Give me a break too.
Or at least one of the western dialects (I'm not sure about what
happens
in India).

Plus, his questions are coming with source code, and they're on-topic.
What more do you want?

Nothing. I guess I need a disclaimer in my sig that in every paragraph
in every reply of mine (except when a Standard is quoted), a smiley is
assumed. Would that work?

V
 
R

red floyd

Neither is mine.  Give me a break too.



Nothing.  I guess I need a disclaimer in my sig that in every paragraph
in every reply of mine (except when a Standard is quoted), a smiley is
assumed.  Would that work?

Probably :)

Seriously, though, Nephi's first language is obviously not English,
and
I'd bet that he may have missed the humor. Hell, my first language
*is*
English, and I missed it the first time around.

No offense intended, though. Seriously.
 
I

Immortal Nephi

Victor,

Please forgive me. You already know me. He is right. English is not
my primary language. You have seen my posts couple weeks ago.

Well, I did experiment to find out weird pointer to data member array.

Put "this->pF[ 0 ] = &Test::RunA;" in Test() constructor. Pointer to
member function array is already declared static const. C++ Compiler
always reports an error like "you cannot assign to a variable that is
const."

Also, pointer to data member array is already declared static const.
Try to put "this->rgData[ 0 ] = &Test::a;" in Test() constructor
again. You can try to compile and see what happened. C++ Compiler is
supposed to report an error like "you cannot assign to a variable that
is const.", but it does not!!

Strange... Perhaps, C++ Compiler does not have the implementation to
add error report about const over pointer to data member array.

I use Microsoft Visual C++ .NET 2008. You can try to do your
experiment. Try Intel C++ Compiler, Linux C++ Compiler, and other C++
Compiler. Let me know. Smile :)
 
V

Victor Bazarov

Immortal said:
[..]
Well, I did experiment to find out weird pointer to data member array.

Put "this->pF[ 0 ] = &Test::RunA;" in Test() constructor. Pointer to
member function array is already declared static const. C++ Compiler
always reports an error like "you cannot assign to a variable that is
const."

Also, pointer to data member array is already declared static const.
Try to put "this->rgData[ 0 ] = &Test::a;" in Test() constructor
again. You can try to compile and see what happened. C++ Compiler is
supposed to report an error like "you cannot assign to a variable that
is const.", but it does not!!

OK, here is your code:

class Test
{
private:
static void ( Test::* const pF[ 3 ] )();
static const int Test::*rgData[ 3 ];
};

Let's look closely, shall we?

What's the declaration of 'pF'? It's (going to the right because there
is a bracket) an array of three (bracket closes, no other bracket, right
parenthesis, so going to the left) *constant* pointers to members of
Test (parentheses close, trying to go to the right again) that is a
function that takes no arguments (semicolon, so going to the left again)
and returns void, and that member is static.

Now, what's the declaration of rgData? It's (going to the right because
there is a bracket) an array of three (bracket closes, no other bracket,
semicolon, so going to the left) pointers to member of Test that is an
int constant, and the whole member is static.
Strange...

Why? An element of 'pF' is a CONSTANT pointer to member of Test, and an
element of 'rgData' is not a constant. 'rgData' is a pointer to
const int member, yes, but it's not a constant pointer, so you can
assign to 'rgData' elements but you can't assign to 'pF' elements.

If you want to make 'rgData' constant pointers to members of Test, you
need to add 'const' *right before the name of the variable*, like you
did with 'pF':

static const int Test::* const rgData[3];
> Perhaps, C++ Compiler does not have the implementation to
add error report about const over pointer to data member array.

No, it's not the compiler. It's you.
I use Microsoft Visual C++ .NET 2008. You can try to do your
experiment. Try Intel C++ Compiler, Linux C++ Compiler, and other C++
Compiler. Let me know. Smile :)

See above. Hope it helps.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top