Thread: void class pointer in class definition -design alternatives?

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    13

    void class pointer in class definition -design alternatives?

    I have a design related question.
    Say I have 2 classes, class A and class B
    I havea third class classMain, where I want to use wither classA or classB at runtime based on some parameters.

    I am planning to create a void pointer (as below) in the classMain and then point it either classA or classB object at runtime.

    Code:
    classMain
    {
        ...
        ....
         void * classptr;
    }
    Are there any other alternatives to this approach?
    Thanks in advance.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I am planning to create a void pointer (as below) in the classMain and then point it either classA or classB object at runtime.
    er.. Is that actually possible(pointing a void* to a class) in C++ ?

    You could point it towards objects of those classes.
    If the objects are small in size and you do not have memory constrains, I think it'd be simpler and better to put each of the objects in the class and use them as necessary.
    Last edited by manasij7479; 08-06-2011 at 09:12 AM.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    thanks for the response.

    yes, it will point to the object of the class.
    i should have named it as "void * objptr;"

    Yes, having the objects is one solution; or better have the pointers and allocated mem only if neeed.

    Code:
    classMain
    {
        ...
        ....
         class A* objAptr;
         class B* objBptr;
    }

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You'd better omit the 'class' words in the declarations of the pointers..

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nappaji
    I havea third class classMain, where I want to use wither classA or classB at runtime based on some parameters.
    I wonder if inheritance and polymorphism would be appropriate here. What are these parameters, and what do classA and classB have in common?

    Quote Originally Posted by manasij7479
    Is that actually possible(pointing a void* to a class) in C++ ?
    It is legal for a void* to point to an object of non-POD class type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    It is legal for a void* to point to an object of non-POD class type.
    I thought(at first) he wanted to point the void* to the class instead of the object and then use it to make objects of the required type.
    Last edited by manasij7479; 08-06-2011 at 11:51 AM.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    The classes dont have much in common, hence did not consider inheritance. Moreover only one of them will be used at any instance.
    void * is to point to objects, not class.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nappaji
    The classes dont have much in common, hence did not consider inheritance.
    Then why do you want classMain to select one of them at run time? It sounds like you have two separate paths of control, and this classMain is either redundant, or perhaps should be two different classes.

    Quote Originally Posted by nappaji
    Moreover only one of them will be used at any instance.
    That has no bearing on whether inheritance and polymorphism is appropriate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 40
    Last Post: 06-02-2010, 10:08 AM
  2. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  3. Replies: 5
    Last Post: 04-17-2008, 02:31 AM
  4. finding derived class type of a pointer to a base class
    By LinuxCoder in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2006, 11:08 AM
  5. Replies: 7
    Last Post: 05-26-2005, 10:48 AM