#include <iostream>
class Mom
{
public:
Mom();
int var;
int* pnt;
};
Mom::Mom()
{
var = 0;
*pnt = 0;
}
void Increment(Mom mother)
{
mother.var++;
mother.*pnt++;
}
int main()
{
Mom asdf;
asdf.var = 3;
asdf.*pnt = 3;
Increment(asdf);
std::cout << asdf.var << std::endl;
std::cout << asdf.*pnt << std::endl;
return 0;
}
Dereferencing (sp?) member variables in C++
When I try to derefernce a memver varible, I get errors... I think I am using the right syntax. I am using MSVS 2005 which I havn't had very long, so it may be due to my inexperince with VS that I cant fix the problem, then again it may be me. Anyway, here is a semi-retarded example, but it shows my problem.
What am I doing wrong?
*pnt doesn't appear to be pointing AT anything. A pointer needs to be "pointed" at a normal variable first as in
int x;
int *ptr;
ptr=&x
*ptr=3 etc.. now okay.
int x;
int *ptr;
ptr=&x
*ptr=3 etc.. now okay.
Mom::pnt is never initialized to point to any senseful location.
E.g. an initialization w/ an external int:
(Being too late once more. ;)
E.g. an initialization w/ an external int:
Mom::Mom(int* whereToPnt){ pnt = whereToPnt; var = 0; *pnt = 0; // accesses aVarToPntTo in fact}int main(){ int aVarToPntTo; Mom asdf(&aVarToPntTo); ...}
(Being too late once more. ;)
Oh oops. I knew it would be something simple. However, after changing the source, I still get the same error:
\TESTER.cpp(20) : error C2065: 'pnt' : undeclared identifier
Here is the updated source.
\TESTER.cpp(20) : error C2065: 'pnt' : undeclared identifier
Here is the updated source.
#include <iostream>class Mom{public: Mom(int* pie); int var; int* pnt;};Mom::Mom(int* pie){ var = 0; pnt = pie;}void Increment(Mom mother){ mother.var++; mother.*pnt++;}int main(){ int pies = 0; Mom asdf(&pies); asdf.var = 3; asdf.*pnt = 3; Increment(asdf); std::cout << asdf.var << std::endl; std::cout << asdf.*pnt << std::endl; return 0;}
The syntax you want is
(*mother.pnt)++;...*asdf.pnt = 3;...std::cout << *asdf.pnt << std::endl;
Ah that fixed it, and now that I fixed the first problem, it doesn't crash at runtime with the correct syntax (although I couldv'e sworn it worked this way too). Alright, thanks for the help.
Quote:Original post by Brother Bob(*mother.pnt)++;
I think you can also use
(mother->pnt)++;
but don't quote me on this :)
Quote:Original post by Fred304Quote:Original post by Brother Bob(*mother.pnt)++;
I think you can also use(mother->pnt)++;
but don't quote me on this :)
Nope, your code is equivalent to (*mother).pnt++;
Quote:Original post by Fred304
I think you can also use(mother->pnt)++;
but don't quote me on this :)
You can but it is totally different functunality.
Increase whatever ptr is pointing to by 1.
(*ptr)++;
Increase ptr by 1.
(ptr)++;
EDIT: I didn't notice that you try to dereference 'mother'. It still won't work.
Since we're using classes and C++ anyway, why not
and not need to mess about with pointers at all?
class xyz{public: xyz(int &a) : ref(a) { } int &ref;};int main(int,const char**){ int x; xyz z(x); z.ref=23;}
and not need to mess about with pointers at all?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement