CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 1999
    Posts
    57

    void pointer to class

    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”?



  2. #2
    Join Date
    Apr 1999
    Posts
    24

    Re: void pointer to class

    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.



  3. #3
    Join Date
    May 1999
    Posts
    57

    Re: void pointer to class

    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?



  4. #4
    Join Date
    May 1999
    Posts
    123

    Re: void pointer to class

    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.

  5. #5
    Join Date
    May 1999
    Posts
    57

    Re: void pointer to class

    Thanks, it works great now


  6. #6
    Join Date
    Apr 1999
    Posts
    24

    Re: void pointer to class

    Did you tried (CallingClass *)pCallingClass->SomeFunction() ?



  7. #7
    Join Date
    May 1999
    Posts
    57

    Re: void pointer to class

    I couldn't do (CallingClass *)pCallingClass->SomeFunction() since there are 6 possible classes that could show the dialog box.


  8. #8
    Guest

    Re: void pointer to class

    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured