dereferencing a pointer to a pointer

This is a discussion on dereferencing a pointer to a pointer within the C++ Programming forums, part of the General Programming Boards category; Ok so lets say you have a base class called Object . you also have another class that derives from ...

  1. #1
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195

    dereferencing a pointer to a pointer

    Ok so lets say you have a base class called Object.

    you also have another class that derives from class Object called subObject

    you lastly have a pointer to a void called pVoid, which i think you could make by doing this:
    Code:
    void* pVoid
    now if you had:
    Code:
    Object* pObject = new subObject;
    and did this:
    Code:
    pVoid = dynamic_cast<(pObject*)>(pVoid);
    would pVoid be a pointer to the new subObject, in the sense that you could do:
    Code:
    pVoid->(memberfunction of subObject)();
    NOTE: both classes are normal, not virtual
    Last edited by loopshot; 12-01-2005 at 11:37 AM.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Why don't you try it?
    Code:
    class Base
    {
    public:
    	Base() {}
    
    	void Print()
    	{
    		std::cout << "In Base.Print" << std::endl;
    	}
    };
    
    class Derived : public Base
    {
    public:
    	Derived() {}
    
    	void DerivedPrint()
    	{
    		std::cout << "In Derived.DerivedPrint" << std::endl;
    	}
    };
    
    int main()
    {
    	Base* baseObj = new Derived();
    	void* pVoid = baseObj;
    	dynamic_cast<Base*>(pVoid)->DerivedPrint();
    
    	return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Can you give better examples, those are not correct. pVoid is uninitialized, so if you cast it it will still be uninitialized. When you cast, you put the type in the angle brackets, so it should be dynamic_cast<Object*>. Did you mean to point pVoid at pObject first, and then cast to Object* like in pianorain's example?

    If that is the case, and any time you have an Object* pointer, you cannot call subObject member functions directly. If you had a virtual method in the Object class that was implemented in the subObject class, then you could call that from the Object* pointer. You cannot call any of them from a void*. If you use a dynamic_cast on a void*, I'm not sure if it would work at all.
    Last edited by Daved; 12-01-2005 at 10:50 AM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    pVoid is uninitialized, so if you cast it it will still be uninitialized.
    A void pointer can point to any data type. However, you cannot dereference a void pointer. In your example, you have two errors in that regard:

    1) Your void pointer never points to anything.

    2) The operator -> is shorthand for the combination of the dereference operator followed by the member access operator, so pVoid->memberFunction is equivalent to (*pVoid).memberFunction. That dereferences a void pointer which is illegal.

    As for a dynamic_cast, you can only use it when you are dealing with polymorphic class types, which means the base class has to contain at least one virtual function. Dynamic casts are used to cast down the class hierarchy, from a base class to a derived class; or across a class hierarchy. However, you are trying to use a dynamic cast to cast to a base class.
    Last edited by 7stud; 12-01-2005 at 06:56 PM.

  5. #5
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    Thanks for you the good reply stud7, unlike daved.

    by the way you can make a cast up to a base class if the derived class is directly derived

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> you can make a cast up to a base class if the derived class is directly derived

    It is not necessary to use a cast when converting from the derived class to the base class, the conversion is implicit (as in your second code snippet).

  7. #7
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    that may be but you can still cast up, which was the point of my last post

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Thanks for you the good reply stud7, unlike daved.
    lol.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    by the way you can make a cast up to a base class if the derived class is directly derived
    You can cast up to a base class if the derived class is indirectly derived as well. The cast is usually done implicitly:

    Base* pBase = pIndirectlyDerived;

    but it can be done explicitly:

    Base* pBase = static_cast<Base*>(pIndirectlyDerived)

    However, in neither case is that a dynamic_cast.
    Last edited by 7stud; 12-02-2005 at 01:41 PM.

  10. #10
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    another wonderful post by stud7m that completly trumps daved's i might add.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  5. Dereferencing Pointer
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-01-2002, 06:52 AM

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