new Object() and &Object()

This is a discussion on new Object() and &Object() within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by tabstop "polymorphic, dynamic binding" is the same as "override the base class function and call the derived ...

  1. #16
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by tabstop View Post
    "polymorphic, dynamic binding" is the same as "override the base class function and call the derived classes function", at least in the sense of "even though we're holding a base class pointer here". Without virtual, you'd always get Base class functions whenever you called from a Base class pointer, regardless of what was actually being pointed to.


    You told the compiler "this is a pointer to Base", and it was unable to find any reason to disbelieve you.
    thanks tabstop..as always you find a way to make things clear.
    nice one man.
    You ended that sentence with a preposition...Bastard!

  2. #17
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by tabstop View Post
    Since in this case there is no object there to return any information, the program continues on its way using the type you told it was there.
    And to clarify the function exists even though the object is not allocated, because the function is one for every class used by all objects. Like:
    Code:
    void Virtual(Base* this)
    {
        cout << "....";
    }
    in your case this will point to nowhere but since it is not used the program might not crush. If you were to use "num" inside the function (try it) then you have more chances to crush it.

  3. #18
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    is it possible to pass "this"?
    it is implicitly passed...
    i will try your idea now

    the function is one for every class used by all objects
    is it that not if the functions are of the objects of the same class?
    so they share one function..is that what you mean?

    EDIT:
    i used num..
    just print what it held..it didn't crash
    same with assigning...i guess i need more chances..
    Last edited by Eman; 01-30-2011 at 03:49 PM.
    You ended that sentence with a preposition...Bastard!

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Eman View Post
    is it possible to pass "this"? it is implicitly passed...
    Um, it's possible, if you are calling a function that requires a pointer-to-object and want to pass it a pointer to the current object. You don't use it for member functions.

    is it that not if the functions are of the objects of the same class?
    so they share one function..is that what you mean?

    EDIT:
    i used num..
    just print what it held..it didn't crash
    same with assigning...i guess i need more chances..
    I think his point was that the code doesn't live inside the object; the code lives as a function with the rest of the code; so it's not as though you were trying to run code at an invalid memory address, just looking at invalid memory. (And since we are using a once-valid pointer, it is likely---though not certain---that looking at (or fetching a value from) this memory is not going to cause a segfault. It will give us bogus data, but it probably won't crash.) If we had actually considered this a derived object and tried to look up the vtable (or whatever is used for virtual function dispatch), then bad things might happen if the memory had ended up being overwritten.

  5. #20
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    I think his point was that the code doesn't live inside the object; the code lives as a function with the rest of the code; so it's not as though you were trying to run code at an invalid memory address, just looking at invalid memory.
    oh...I have no idea what you mean by "the code doesn't.."

    once-valid pointer?
    I thought we agreed that it doesn't point to anything..only at run time?
    unless it points to a valid address, but it doesn't create a static bind to a function call.
    so b[0] did point to something.
    You ended that sentence with a preposition...Bastard!

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Eman View Post
    oh...I have no idea what you mean by "the code doesn't.."
    Your objects live somewhere in memory. But nowhere in that chunk of memory is the instructions for the function. Those instructions live elsewhere (with the rest of the program, not with all the variables).

    Quote Originally Posted by Eman View Post
    once-valid pointer?
    I thought we agreed that it doesn't point to anything..only at run time?
    It pointed at something for approximately 0.0002 seconds (between the temporary object being constructed and then being destructed at the end of the statement). But the pointer still retains it's value, and still holds the address where that temporary object was constructed.

  7. #22
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by tabstop View Post
    Your objects live somewhere in memory. But nowhere in that chunk of memory is the instructions for the function. Those instructions live elsewhere (with the rest of the program, not with all the variables).


    It pointed at something for approximately 0.0002 seconds (between the temporary object being constructed and then being destructed at the end of the statement). But the pointer still retains it's value, and still holds the address where that temporary object was constructed.
    ah yeah. I understand you now.
    sweet, thanks much fellas .
    You ended that sentence with a preposition...Bastard!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

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