Better way than casting??

This is a discussion on Better way than casting?? within the C++ Programming forums, part of the General Programming Boards category; Hi, Class B inherits from class A class D inherits from class A D has a function say, fun_D() B ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    41

    Better way than casting??

    Hi,

    Class B inherits from class A
    class D inherits from class A

    D has a function say, fun_D()

    B has a function say, fun_B(A* Dtype)
    Code:
    void main()
    {
    	
    	D *pD = new D();
    	B *pB = new B();
    	pB->fun_B(pD);
    
    }
    I want to call fun_D() from B. But I don't want to include fun_D in A.


    The best way I could think was,

    Code:
    void B::fun_B(A *Dtype)
    {
    
    	D *lDtype = static_cast <D*>(Dtype);
    	
    	lDtype->fun_D();
    }
    But I am not happy with this casting. Is there any better way to do this?

    Best Regards,
    Shal

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    If it doesn't completely bother you, you can take your foo_function out of the class implementation, and have it work on a const reference to a B object.

    And because of scope, there shouldn't be any conflicts if D uses it's own foo_function while B uses the global one.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Is there any better way to do this?

    In many (but not all) cases, yes. The way to get rid of the cast is to identify why fun_B has an A* parameter while it only expects D* arguments to be passed to it. Why not just have fun_B take a D* instead?

    If the reason is that sometimes you will be passing a D, and other times you will be passing A or other classes derived from A that are not D's, then you should be using dynamic_cast to verify that the pointer is in fact a D, not an A or some other derived class. That won't fix your design problem though.

    To fix the design problem, you have to separate the use of D specific functions to places where only a D can be used. If fun_D makes sense as part of the interface to A, but B objects will do nothing and D objects will do something, then you could add fun_D as a virtual function in A and you would no longer need the cast. That is closer to good design, although it might not be the best solution especially if the fun_D interface makes no sense anywhere but for D objects.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    On an unrelated note, main returns int
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You're doing a down-cast (down the inheritance hiearchy) and thus dynamic_cast would be better suited as it's not guaranteed that the A* is a D* (in the general case).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting
    By morvick in forum C++ Programming
    Replies: 2
    Last Post: 06-18-2007, 12:06 AM
  2. Casting Question (I think)
    By fayte in forum C Programming
    Replies: 6
    Last Post: 03-08-2006, 05:31 PM
  3. casting the system exstracted date into seperate ints
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-30-2005, 01:17 AM
  4. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 03:16 PM
  5. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 06:01 AM

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