upcasting

Why does C style cast allow you to convert to a private base class? [duplicate]

蓝咒 提交于 2019-11-27 18:18:10
问题 This question already has an answer here: Can I cast a derived class to a private base class, using C-style cast? 3 answers Say we have this code class A { public: A() : x(1) {} virtual ~A() {} int x; }; class B { public: B() : y(2) {} virtual ~B() {} void g() { cout << "B::" << y << endl; } int y; }; class C : private A, private B { public: void f() { B* p = static_cast<B*>( this ); p->g(); } }; int main() { C c; ((B*)&c)->g(); return 0; } The C style cast in the main function cannot be

Can I cast a derived class to a private base class, using C-style cast?

六眼飞鱼酱① 提交于 2019-11-27 05:15:01
Can I do this? class A { ... }; class B : private A { const A &foo() const { return *((const A *)this); } }; Can I take a subclass that inherits privately from a base class and cast it to a public version of its base class? Can I do this without it having virtual methods? My guess is yes, but I wanted to make sure it's safe / portable. Yes you can: §5.4/7 of the standard: ... the following static_cast and reinterpret_cast operations (optionally followed by a const_cast operation) may be performed using the cast notation of explicit type conversion, even if the base class type is not accessible

downcast and upcast

╄→гoц情女王★ 提交于 2019-11-26 21:49:31
I am new to C# (and OOP ). When I have some code like the following: class Employee { // some code } class Manager : Employee { //some code } Question 1 : If I have other code that does this: Manager mgr = new Manager(); Employee emp = (Employee)mgr; Here Employee is a Manager , but when I cast it like that to an Employee it means I am upcasting it? Question 2 : When I have several Employee class objects and some but not all of them are Manager 's, how can I downcast them where possible? RCIX That is correct. When you do that you are casting it it into an employee object, so that means you

Why do we assign a parent reference to the child object in Java?

Deadly 提交于 2019-11-26 15:39:43
I am asking a quite simple question, but I am bit confused in this. Suppose I have a class Parent : public class Parent { int name; } And have another class Child.java : public class Child extends Parent{ int salary; } And finally my Main.java class public class Main { public static void main(String[] args) { Parent parent = new Child(); parent.name= "abcd"; } } If I make a child object like Child child = new Child(): Then child object can access both name and salary variables. My question is: Parent parent = new Child(); gives the access of only name variable of Parent class. So what is the

Can I cast a derived class to a private base class, using C-style cast?

…衆ロ難τιáo~ 提交于 2019-11-26 11:28:55
问题 Can I do this? class A { ... }; class B : private A { const A &foo() const { return *((const A *)this); } }; Can I take a subclass that inherits privately from a base class and cast it to a public version of its base class? Can I do this without it having virtual methods? My guess is yes, but I wanted to make sure it\'s safe / portable. 回答1: Yes you can: §5.4/7 of the standard: ... the following static_cast and reinterpret_cast operations (optionally followed by a const_cast operation) may be

downcast and upcast

大憨熊 提交于 2019-11-26 08:04:02
问题 I am new to C# (and OOP ). When I have some code like the following: class Employee { // some code } class Manager : Employee { //some code } Question 1 : If I have other code that does this: Manager mgr = new Manager(); Employee emp = (Employee)mgr; Here Employee is a Manager , but when I cast it like that to an Employee it means I am upcasting it? Question 2 : When I have several Employee class objects and some but not all of them are Manager \'s, how can I downcast them where possible? 回答1

Why do we assign a parent reference to the child object in Java?

帅比萌擦擦* 提交于 2019-11-26 04:05:51
问题 I am asking a quite simple question, but I am bit confused in this. Suppose I have a class Parent : public class Parent { int name; } And have another class Child.java : public class Child extends Parent{ int salary; } And finally my Main.java class public class Main { public static void main(String[] args) { Parent parent = new Child(); parent.name= \"abcd\"; } } If I make a child object like Child child = new Child(): Then child object can access both name and salary variables. My question

What is the difference between up-casting and down-casting with respect to class variable

浪尽此生 提交于 2019-11-25 23:32:57
问题 What is the difference between up-casting and down-casting with respect to class variable? For example in the following program class Animal contains only one method but Dog class contains two methods, then how we cast the Dog variable to the Animal Variable. If casting is done then how can we call the Dog\'s another method with Animal\'s variable. class Animal { public void callme() { System.out.println(\"In callme of Animal\"); } } class Dog extends Animal { public void callme() { System