void class pointer in class definition -design alternatives?

This is a discussion on void class pointer in class definition -design alternatives? within the C++ Programming forums, part of the General Programming Boards category; I have a design related question. Say I have 2 classes, class A and class B I havea third class ...

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

    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    11
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    You'd better omit the 'class' words in the declarations of the pointers..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,974
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    11
    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
    20,974
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21