Thread: Dynamic class variable

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    Quote Originally Posted by laserlight View Post
    Is isB() really necessary? It pretty much means that the base class must know about one of its child classes. If you can get away without it, that would be better.
    The only reason isB() exists is so D objects will know whether x() points to a B or C. What would be a better way to test this?

    Quote Originally Posted by laserlight View Post
    It should be an A*.
    A pointer to a base class can also point to any derived classes as well without breaking things? If I'm understanding you correctly, and use A* as the data type for D::x and return type for D::x(), then the following should be possible, right?

    Code:
    B *b = new B();
    C *c = new C();
    D *d = new D();
    
    //  point x to b
    d->x ( b );
    
    cout << d->x->some_class_B_method () << endl;
    
    //  point x to c
    d->x ( c );
    
    cout << d->x->some_class_C_method () << endl;

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by drrcknlsn View Post
    The only reason isB() exists is so D objects will know whether x() points to a B or C. What would be a better way to test this?
    You can use dynamic_cast, but there is a slight performance cost using that.
    Instead of isB() you can have something like ClassType() which returns an enum value specifying the class type.

    Quote Originally Posted by drrcknlsn View Post
    A pointer to a base class can also point to any derived classes as well without breaking things? If I'm understanding you correctly, and use A* as the data type for D::x and return type for D::x(), then the following should be possible, right?

    Code:
    B *b = new B();
    C *c = new C();
    D *d = new D();
    
    //  point x to b
    d->x ( b );
    
    cout << d->x->some_class_B_method () << endl;
    
    //  point x to c
    d->x ( c );
    
    cout << d->x->some_class_C_method () << endl;
    No, if x is an A* pointer then it can only access A functions.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    Right. I didn't think it would work like that, which is why I was asking about void *.

    The only other option I can think of that satisfies my requirements is splitting D::x into two different variables, with accessors B *D::x_B() and C *D::x_C(). I didn't really want to do this, because it seems like bad design to have to test for a null pointer every time I want to use x.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. dynamic array of base class pointers
    By Corrington_j in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2003, 05:58 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM