• Advertisement
Sign in to follow this  

Dereferencing (sp?) member variables in C++

This topic is 4348 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.
#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;
}

What am I doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
*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.

Share this post


Link to post
Share on other sites
Mom::pnt is never initialized to point to any senseful location.

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. ;)

Share this post


Link to post
Share on other sites
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.

#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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Brother Bob

(*mother.pnt)++;

I think you can also use

(mother->pnt)++;

but don't quote me on this :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Fred304
Quote:
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++;



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Since we're using classes and C++ anyway, why not


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?

Share this post


Link to post
Share on other sites
Since we're using classes and C++ anyway, why not


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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement