4

If one accesses the fields of derived class object after assignment this object to variable of base class type in java, i get the expected behaviour, meaning print field's value of the derived class . In c++ value of the field, belonging to the base class is printed.

In Java, 6 is printed as expected:

class Ideone
{

    static class A {
        static int a = 7;
    }
    static class B extends A {
        static int b = 6;
    }
    public static void main (String[] args) throws java.lang.Exception
    {

        A a = new B();
        System.out.println(((B)a).b);
    }
}

In C++, 7 is printed:

#include <iostream>
using namespace std;

class Base {
    int i = 7;
public:

    Base(){
        std::cout << "Constructor base" << std::endl;
    }
    ~Base(){
        std::cout << "Destructor base" << std::endl;
    }
    Base& operator=(const Base& a){
        std::cout << "Assignment base" << std::endl;
    }
};

class Derived : public Base{
public:
    int j = 6;
};

int main ( int argc, char **argv ) {
    Base b;
    Derived p;
    cout << p.j << endl;
    b = p;
    Derived pcasted = static_cast<Derived&>(b);
    cout << pcasted.j << endl;
    return 0;
}

Is it possible to achieve in c++ similar type of behaviour (printing 6).

rok
  • 9,403
  • 17
  • 70
  • 126

4 Answers4

5

Is it possible to achieve in c++ similar type of behaviour (printing 6).

It sure is, as long as you do the same thing as you do in Java.

You must remember, that even though Java has similar syntax, it is not identical. In Java, the statement A a = new B(); creates a reference of base type, bound to a derived type. ((B)a) then casts the type of the reference down to derived.

In c++, Base b; is not a reference variable. Copy-assigning a derived object to this variable will copy the base-sub-object of the derived object. This language feature is known as slicing. Simply use a reference to have similar semantics to your Java code:

Base& b = p;

If you access an object of concrete type Base, through a Derived& reference, you invoke undefined behaviour.

P.S. your assignment operator does not return anything, despite the return type being non-void. Not returning has undefined behaviour.

eerorika
  • 232,697
  • 12
  • 197
  • 326
3

In the C++ code, b = p; just slicing copied p to b, b is still Base in fact. Then static_cast<Derived&>(b) will fail and lead to UB.

You could use reference or pointer, letting b point to Derived in fact. Then it'll be fine to down-cast it to Derived.

int main ( int argc, char **argv ) {

    Derived p;
    Base& b = p;
    cout << static_cast<Derived&>(b).j << endl;
    return 0;
}

LIVE

Community
  • 1
  • 1
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
2

Your question might be meaningful if your C++ code was even close to being equivalent to your Java code. It is not. Even if they were written in the same language, it is hardly surprising that two non-equivalent sections of code will produce different results.

A main() of the form

int main ( int argc, char **argv )
{
    Base *b = new Derived;

    std::cout << ((Derived *)p)->j << endl;
    return 0;
}

is (approximately) equivalent to the Java code, allowing for the fact that Java blends concepts of those things in C++ called pointers and references. It will give you the result you expect.

As will changing the statement std::cout << ((Derived *)p)->j << endl to the equivalent (in C++) std::cout << (*((Derived *)p)).j << endl which is probably a more literal interpretation of the Java code.

Actually your main() function yields undefined behaviour anyway, so all bets are off. The line

Derived pcasted = static_cast<Derived&>(b);

converts a reference to b into a reference to Derived. Since b is not of type Derived, the assignment of pcasted treats b as if it is a Derived when it is not. That gives undefined behaviour.

Peter
  • 35,646
  • 4
  • 32
  • 74
1

Code differences:

  • static and non static member variables
  • Stack object vs heap (reference) object
  • Object slicing in C++ of stack-object, and simple type cast in Java.

Literally comparing Apples with Oranges!

Ajay
  • 18,086
  • 12
  • 59
  • 105