Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Dwiel

This Pointers

This topic is 6153 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

Is it possible and if so, how can I pass a this pointer to a non-member function outside of the class. I learn well by example, and better by experimenting, so if it is possible, a 10 line code snippit would be awsome, even though most people including my self generally avaiod posting code I would really apreciate it if you could post a small ex. Thanx ahead Zach - Tazzel3D

Share this post


Link to post
Share on other sites
Advertisement

this sample is a bit stupid, but it works:


class MyClass
{
void something();
};


int globalFunction(MyClass* class)
{
return 666;
}

void MyClass::something()
{
int someValue = globalFunction(this);
}

Share this post


Link to post
Share on other sites
It took me about 15 minuets looking trough the help files of MSVC++ to figure out how to do that much, but when I posted the message, I assumed that if you could pass the this pointer to a global function, you would have access to all of its private members and variables. I descovered I was wrong. I did find though if I included the def. in the class def. as a friend I was able to access the private members and variables. Is the only way for a global function to access private members and vars in a class?

Thanx ahead!

P.S.
actually what I learned from the help was I could pass
func((*this)), to a function w/ definition of
void func(MyClass classvar);

Your way is easier to read and less confusing, so thanx alot, I guess I should have figured out that much.

Share this post


Link to post
Share on other sites
As the names suggest, private and protected members are not accessible from anything outside of the class, unless those anythings are defined as friends. How come your 15 minutes in the help files didn''t answer that?

Share this post


Link to post
Share on other sites
The whole objective of access specifiers (public, private, protected - and in C++ Builder, published) is to limit access of external entities to member functions and variables. Rather than declaring friend classes (most of the time), write access functions:
  
class SomeClass
{
private:
float someFloatVar;
int someIntVar;

public:
SomeClass(){} // constructor is public, so any function/code can create objects

// non-referenced access functions:

int getIntVar( void ) { return someIntVar; }
void setIntVar( int i ) { someIntVar = i; }
// referenced access functions:

float &FloatVar( void ) { return someFloatVar; }
};

// in main...

SomeClass sc;
sc.setIntVar( 6 );
int i = sc.getIntVar();
sc.FloatVar() = 1.25f;
float f = sc.FloatVar();


Since FloatVar() returns a reference - the address of the variable - it qualifies as an l-value, ie, can be used on either side of assignments (Warning: not all l-values can be used on both sides (are r-values)!

I suspect you might be trying to access the private members of a class you didn''t write. Give it up. You can''t declare your class to be a friend class - a class must give access by declaring its friends - and subclassing doesn''t provide access either.

Share this post


Link to post
Share on other sites
Thanx All.
I was writing one of my first classes, that will help me make some basic card games, and I wanted to try to get a function to be available outside of the class, but use the private variables. I kinda figured that there wasn''t anyway to do it without friends, but I guess there is no problems with friends. Thanx again for those who help newbies like myself!

Tazzel3D ~ Zach

Share this post


Link to post
Share on other sites
Why not just make the function public? A public function can still use the private variables of its own class. Or maybe you want a static function?


Edited by - Sandman on July 19, 2001 10:29:40 AM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!