Passing Pointers
-
Hi everyone, I am 15 and would like to start programming. I have read all summer about VC++ and started to code in my spare time. I have a really simple question about pointers. I just don't know how to do it. How would I pass a pointer to a class to another class in the same project? I have class1 and want class2 to have access to class1's stuff. I hear that I just need a pointer to class1 in class2. can someone help me?
-
Hi everyone, I am 15 and would like to start programming. I have read all summer about VC++ and started to code in my spare time. I have a really simple question about pointers. I just don't know how to do it. How would I pass a pointer to a class to another class in the same project? I have class1 and want class2 to have access to class1's stuff. I hear that I just need a pointer to class1 in class2. can someone help me?
Hello, You can do this: In class 1 add the next variable:
Class1
{
Class2 * m_pClass2;
// more stuff
};Class2
{
// stuff of class 2
};if you also want to have acess to private and protected members add the following to Class1:
// stuff
friend class Class2;
// more stuffhope this helps :)
A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.
-
Hi everyone, I am 15 and would like to start programming. I have read all summer about VC++ and started to code in my spare time. I have a really simple question about pointers. I just don't know how to do it. How would I pass a pointer to a class to another class in the same project? I have class1 and want class2 to have access to class1's stuff. I hear that I just need a pointer to class1 in class2. can someone help me?
Class1 instanceOfClass1; Class2 instanceOfClass2; // "m_ptrToClass1Object" is a public member of class Class2 instanceOfClass2.m_ptrToClass1Object = &instanceOfClass1;
/ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com -
Hello, You can do this: In class 1 add the next variable:
Class1
{
Class2 * m_pClass2;
// more stuff
};Class2
{
// stuff of class 2
};if you also want to have acess to private and protected members add the following to Class1:
// stuff
friend class Class2;
// more stuffhope this helps :)
A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.
-
Class1 instanceOfClass1; Class2 instanceOfClass2; // "m_ptrToClass1Object" is a public member of class Class2 instanceOfClass2.m_ptrToClass1Object = &instanceOfClass1;
/ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com -
Hi, thanks for the help. Does this work if the 2 classes are in different .cpp files? where exactly would this friend stuff go? thanks again
The classes should be in header files! exemple:
// the .h (header) file
class a
{
public:
void somefunc();
};// in the .cpp (implementation) file
void a::somefunc()
{
// code
}But it will work if the 2 classes reside in other files! If you make a class fiend of an other class, That class has access to the private and protected members of that class: exemple:
class a
{
private:
int nPrivateInt;
public:
int nPublicInt;
};class b
{
public:
friend class a;
void somefunc()
{
// do something
a InstanceOfa;
InstanceOfa.nPrivateInt = 0; // this is possible because a is a friend!
}
};// somewhere in the code
a InstanceOfA;
InstanceOfA.nPrivateInt = 0; // not possible because nPrivateInt is private!
InstanceOfA.nPublicInt = 0; // possible because nPublicInt is public!Hope this makes thing clear!
A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.
-
Hi everyone, I am 15 and would like to start programming. I have read all summer about VC++ and started to code in my spare time. I have a really simple question about pointers. I just don't know how to do it. How would I pass a pointer to a class to another class in the same project? I have class1 and want class2 to have access to class1's stuff. I hear that I just need a pointer to class1 in class2. can someone help me?
Suppose you have the following:
class ClassB { public: void ActionB(); }; class ClassA { public: ClassB* m_pB; void ActionA(); };
Then you can access ClassB from ClassA using the following:ClassA a; //create an instance of each ClassB b; a.ActionA(); //call methods from A directly a.m_pB = &b; //pass a pointer to the instance of B to A a.m_pB->ActionB(); //call B through A using the stored pointer
This is just one of many ways of doing this. The situation dictates what you actually implement. You need to learn about objects, instances, pointers, call by value, call by reference and lots of other concepts. I suggest you get yourself a good C++ book. Lorenz Prem Microsoft Corporation