Consider the following classes and pInt declaration:
class A { ... };
class B : public A
{
void function()
{
int A::*pInt;
// use pInt
}
};
Where does pInt belongs, is it local to B::function() or it's a member of A?
Consider the following classes and pInt declaration:
class A { ... };
class B : public A
{
void function()
{
int A::*pInt;
// use pInt
}
};
Where does pInt belongs, is it local to B::function() or it's a member of A?
pInt belongs to B::function() . That's to say, its scope is limited in B::function() .
Though it's a pointer to member of A.
7 hours ago, Alberth said:I don't think that compiles at all.
It compiles on VC++ 2017 but it's anyone's guess what it actually does.
3 hours ago, Gnollrunner said:It compiles on VC++ 2017 but it's anyone's guess what it actually does.
It's a pointer to class A int field.
class A {
public:
int foo;
};
class B : public A
{
public:
void function()
{
// set pointer to class field
int A::*pInt = &A::foo;
// use pointer to modify some field
A a;
a.*pInt = 5;
}
};
4 minutes ago, Zaoshi Kaba said:It's a pointer to class A int field.
Cool, I messed with it a bit and got as far as doing the same assignment you did but I I didn't figure out putting the * after the a. and not before. In any case I'll almost for sure never need it. It's pretty esoteric, but at least I know what it is now.
@Zaoshi Kaba thanks for the code, but why would I have to do this. Can't I just use "int *pInt2 = &A::foo;" instead of giving it "A::"?
Oh, I see, than I could not use "a.*pInt = 5;". Now I'm confused even more. ".*pInt" is not defined in A but we still use a dot "." for it??
So I guess, only B::function() thinks A has a member pInt, if I'm right.
So what's the use of all this?
34 minutes ago, ryt said:@Zaoshi Kaba thanks for the code, but why would I have to do this. Can't I just use "int *pInt2 = &A::foo;" instead of giving it "A::"?
Oh, I see, than I could not use "a.*pInt = 5;". Now I'm confused even more. ".*pInt" is not defined in A but we still use a dot "." for it??
So I guess, only B::function() thinks A has a member pInt, if I'm right.So what's the use of all this?
Errr sorry. We kind of hijacked your thread. You probably didn't mean to to code what you coded. I was just so surprised it actually compiled that I wanted to know why. The way you declared pInt, it's simply a local variable of function. It's of a very odd type, one that most programmers would never use, but a local variable none the less. Putting the type int in front of it in the function makes it local to that function. Perhaps you meant to put it in class A which would make it a member of A which you could access from function since B inherits from A as follows:
class A {
protected:
int *pInt;
};
class B : public A
{
public:
void function()
{
pInt = new int();
}
};
I'm not really sure what you are tying to do, but you stumbled across a seldom used feature of the language.
7 hours ago, Gnollrunner said:Errr sorry. We kind of hijacked your thread. You probably didn't mean to to code what you coded. I was just so surprised it actually compiled that I wanted to know why. The way you declared pInt, it's simply a local variable of function. It's of a very odd type, one that most programmers would never use, but a local variable none the less. Putting the type int in front of it in the function makes it local to that function. Perhaps you meant to put it in class A which would make it a member of A which you could access from function since B inherits from A as follows:
I'm not really sure what you are tying to do, but you stumbled across a seldom used feature of the language.
No worries, the thread is not hijacked, it's actually what I wanted. This is not my code, I'm reading a book and the author used this to explain something else though I did not understand this part and that's why I'm asking.
Declaration of "int A::*pInt;" in B::function() was intentional.
So, does putting "A::" in front of it makes it only "bound" to objects of A, so the pointer can access only A members? I'm still confused about it and using the pointer with a "." as it's not declared in original A class.
38 minutes ago, ryt said:So, does putting "A::" in front of it makes it only "bound" to objects of A, so the pointer can access only A members?
Apparently so. I'm not sure why anyone would need that level of protection but there you have it. You can use a plain old int * to do the same thing, the only difference being it can point to any int, and not just one which is a member of class A.