|
The pointer of derived class returned by new can be type cast to the pointer of its base class.
Is this true or false?
I know dynamic_cast can be used to cast downside. Generally, how to cast a pointer of derived class to a pointer of its base class?
Started by skydoor on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
On the one hand,....
Thus_as_a_b_ptr = my_d_ptr;
Casting a pointer to a derived to a pointer to base should be implicit.
Conversion from a pointer to a derived class to a pointer to a base class is implicit.
|
|
I have this code to represent bank:
class Bank { friend class InvestmentMethod; std::vector<BaseBankAccount*> accounts; public: //...
BaseBankAccount is an abstract class for all accounts in a bank:
class BaseBankAccount { public: BaseBankAccount...
Started by chester89 on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
If the case is the former, then downcasting, while ugly, may specific subtypes differently, and... .
I am also assuming that everything is virtual in your base class and you just omitted (the interface of the base class).
With a dynamic_cast.
|
|
Using this example coming from wikipedia, in which DrawSquare() calls DrawLine(),
could anyone explain me what the ebp and esp are in this context?
From what I see, I'd say the stack pointer points always to the top of the stack, and the base pointer ...
Started by devoured elysium on
, 8 posts
by 8 people.
Answer Snippets (Read the full thread at stackoverflow):
The base pointer points to the memory location of the instruction that's being into program memory sequentially....
Frame's base pointer, which enables stack walking in a debugger and viewing other frames local full one I'm unsure of).
|
Ask your Facebook Friends
|
Class Base { public: virtual void foo() {} }; class Derived: public Base { public: virtual void foo() {} }; int main() { Base *pBase = NULL; Base objBase; Derived objDerived; pBase = &objDerived; pBase->foo(); /*Here Derived class foo will be called...
Started by coolcake on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
PBase->Base::foo()
You can do it through scope resolution operator ::
Something like this:
pBase->Base::foo()
Both responses above are correct...But be careful, if you need to do that, maybe.
|
|
I'm programming C++ with normal pointers, but I have heard about libraries like Boost that implement smart pointers and I have seen that in Ogre3D rendering engine there is a deep use of shared pointers. What's exactly the difference between the three...
Started by tunnuz on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
It is a stack-allocated object that wraps....
In general, only use shared that cover many types; I'll assume you meant scoped pointer which uses the RAII pattern.
Of the pointer exist, and only clean up the memory when the count reaches zero.
|
|
I want to know the runtime type of a base class pointer, I know you can use dynamic_cast. is there any better way?
Started by Ahmed Said on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
You should never need to know about the unbounded set of types that can be derived from a base type..
|
|
I have inherited a large c++ code base and I have a task to avoid any null pointer exceptions that can happen in the code base. Are there are static analysis tools available, I am thinking lint, that you have used successfully.
What other things do you...
Answer Snippets (Read the full thread at stackoverflow):
I once worked on a code base where the habit was upon entry into a function, to first check for any null not apply and your code base ....
A dynamic runtime tool like Valgrind (free)
If you are mainly maintaining the code base, One.
|
|
( Objective C ) How would I call a base class function using a derived pointer where foo is overridden in the derived class. Essentially the equivalent of this C++ code
base* b_ptr = 0 ; derived* d_ptr = new derived() ; d->base::foo() ;
I would think...
Started by Steve on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
This from inside the class, using the super keyword
- (int)doSomething { NSLog(@"calling doSomething on base]; // call doSomething on base class struct objc_super b = { .receiver = d, .class = class_getSuperclass([d.
|
|
Do you have to pass delete the same pointer that was returned by new, or can you pass it a pointer to one of the classes base types? For example:
class Base { public: virtual ~Base(); ... }; class IFoo { public: virtual ~IFoo() {} virtual void DoSomething...
Started by Mark Ransom on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
You call operator delete on the base class pointer, it uses dynamic dispatch to figure out howYes, it will work, if and only if the base class destructor is virtual, which you have done for the Base base class but not....
|
|
I have a template class that I've subclassed with a pointer to it (Decorator pattern). I added a getBase() call to return the pointer to the base class for any further subclasses. However, when I use that getBase() and call the base classes only method...
Answer Snippets (Read the full thread at stackoverflow):
It should be returning the correct type of the base class:
template() { return base_ ; } private....
As to why the compiler is whining about is returning the wrong type .
base (due to inheritance) and a fake one via that pointer.
|