A pointer to base class

A pointer to base class, if assigned to a derived class, only points to the base part right? So you can only use the base part of the derived class with that pointer and no methods from the derived class?

I assume this is the same for references?
Last edited on
Also one small side question, if in the function definition I do not give my parameter names, only their types, they are in accessible in the function to me right? Cause I cannot call them.
A pointer to base class, if assigned to a derived class, only points to the base part right? So you can only use the base part of the derived class with that pointer and no methods from the derived class?

I assume this is the same for references?

Yup. You can however cast that pointer to a pointer of the derived class type (if the instance pointed to indeed is a valid instance of the derived class), and then you can access the derived class's functions and variables.

Also one small side question, if in the function definition I do not give my parameter names, only their types, they are in accessible in the function to me right? Cause I cannot call them.

Yup too. They are unidentifiable, there is no way to use them.

You should try to avoid using casts with object pointers if you can. If you are doing this, then consider revising your design.

Why would assign a pointer to a base class to a derived class? Remember that a pointer to a derived class is a valid pointer to a base class.

By the way I replied to your other post - more info there.

Edit:

With references, they always refer to an object - I am not sure what you are trying to say with that?
Last edited on
I read your reply - gonna read up on design patterns right now :)

With references, they always refer to an object - I am not sure what you are trying to say with that?

http://ideone.com/OaXaIQ
@Fransje

If you can, get hold of the Design Patterns book by Gamma, Helm, Johnson & Vlissides - it gives a complete picture, while the stuff on the net doesn't. The examples on the net are good for trying to visualise different real world situations where various Design Patterns might be needed.

HTH
TheIdeasMan wrote:
You should try to avoid using casts with object pointers if you can. If you are doing this, then consider revising your design.
What about polymorphism.
naraku9333 wrote:
What about polymorphism.


I had a description of polymorphism happening in this post:

http://www.cplusplus.com/forum/general/102407/#msg550881


Is that what you meant?

If not, I am sure you know much more about this than me, could you describe a quick example of what you do with casting & why?

At the moment I am under the impression that casting from one class to another (even if they are related by inheritance) is bad news, along with RTTI. Maybe that is an extreme view? Or is it like a lot things: that are valid / appropriate in some situations, but not in others?

I look forward to your answer, but it maybe a while before I reply - it's 04:45AM at this end, so I am going to stack ZZZZZZZzzzzzz...... :)
I am sure you know much more about this than me
I wouldn't say that.
could you describe a quick example of what you do with casting & why?
I was thinking a situation where you need to verify if a base object is really of a specific derived type you can dynamic_cast<> it.
http://en.cppreference.com/w/cpp/language/dynamic_cast

At the moment I am under the impression that casting from one class to another (even if they are related by inheritance) is bad news, along with RTTI.
It's expensive (especially dynamic_cast), so it would be bad in speed/time critical situations. I know static_cast<> is faster/less expensive, but don't know how much or if it is recommended in a time sensitive situation either.
Last edited on
Topic archived. No new replies allowed.