Click to See Complete Forum and Search --> : void pointer to class
Wilka
April 3rd, 1999, 01:14 PM
Is it possible to have a void pointer to a class?
If it is how I can stop the compiler telling me “left of '->Function’ must point to class/struct/union”?
Alek
April 3rd, 1999, 04:35 PM
As the compiler is tell you some of this case maybe you must to replace
-> with . or you are mismatched the types.
From your question is not clear what you want to do.
Classes or objects of classes are not pointers and you do not have right to typecasting objects with pointers. You must have a pointer near any object, not
whole object.
I hope these some words are helpful for you.
Wilka
April 3rd, 1999, 04:50 PM
Your right I didn’t make myself very clear. What I have is 6 different classes all derived from the same class. All 6 classes have the same (public) functions, they just do different things with them.
Each class needs to use custom dialog, but this dialog box needs to have a way to call some function in the class that’s using it. So that when a button is clicked it does the right thing for that class.
The dialog box has a void pointer as a member variable-
void* pCallingClass;
and before calling the DoModal() function on the dialog box, I set the pointer to the class-
dlg.pCallingClass = this;
but I can’t call and functions of the class via the pointer cos if I do
pCallingClass->SomeFunction();
I get-
error C2227: left of '->SomeFunction' must point to class/struct/union
any suggestions?
Jerry Coffin
April 3rd, 1999, 07:48 PM
Use a pointer to the base class, and make sure the functions are declared to be virtual. E.g.
class base {
// This will just define names for the functions
public:
virtual void function1() = 0;
virtual void function2() = 0;
};
class derived1 : public base {
public:
void function1() { std::cout << "d1:f1\n"; }
void function2() { std::cout << "d1:f2\n"; }
};
class derived2 : public base {
public:
void function1() { std::cout << "d2:f1\n"; }
void function2() { std::cout << "d2:f2\n"; }
};
function use(base &obj) {
obj.function1();
obj.function2();
}
int main() {
derived1 d1;
derived2 d2;
use(d1);
use(d2);
return 0;
}
The universe is a figment of its own imagination.
Wilka
April 4th, 1999, 09:08 AM
Thanks, it works great now
Alek
April 4th, 1999, 05:30 PM
Did you tried (CallingClass *)pCallingClass->SomeFunction() ?
Wilka
April 4th, 1999, 06:20 PM
I couldn't do (CallingClass *)pCallingClass->SomeFunction() since there are 6 possible classes that could show the dialog box.
April 5th, 1999, 03:31 PM
Your example does not need to use a void pointer. One of the strengths of C++
and object-oriented programming is a concept called "polymorphism".
Use virtual functions declared in the base class. With functions declared as
virtual, you can invoke function calls using the base class, and the derived
function will be called automatically. Also, make sure that you understand the
concept of the virtual destructor, since many coders make the mistake of not
using them when they need to.
Regards,
Paul
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.