static_cast on objects c++ [duplicate] - c++

This question already has answers here:
Why doesn't polymorphism work without pointers/references?
(6 answers)
What is object slicing?
(18 answers)
Closed 3 months ago.
I am trying to understand why static_cast works different when it is performed on object than when it is performed on a pointer to object.
class A
{
int myA;
public:
A() { myA = 11; };
virtual void Do() { printf("\n A class is executed "); }
};
class B : public A
{
int myB;
public:
B(){ myB = 22; }
void Do() { printf("\n B class is executed "); }
};
When the following cast is performed, A::Do() is executed. Why?
Why B's virtual table is neglected?
A t_a;
B t_b;
(static_cast<A>(t_b)).Do(); //output: A class is executed
But when it is done via pointer, B::Do() is executed.
A* pA = new B;
pA->Do();
(static_cast<A*>(pA))->Do(); //output: B class is executed
Can you give me a full explanation (including memory layout) to understand the difference.

Related

Virtual Base Class and Diamond Problem in C++ [duplicate]

This question already has answers here:
How does virtual inheritance actually work?
(1 answer)
Internal mechanism of virtual inheritance
(1 answer)
How does virtual inheritance solve the "diamond" (multiple inheritance) ambiguity?
(5 answers)
How C++ virtual inheritance is implemented in compilers?
(5 answers)
Closed 11 months ago.
Consider the following code:-
#include<iostream>
using namespace std;
class A
{
public:
int a;
A()
{
a = 10;
cout<<"Address of a in A "<<&a<<endl;
}
};
class B : public virtual A {
public:
B()
{
cout<<"Address of a in B "<<&a<<endl;
}
};
class C : public virtual A {
public:
C()
{
cout<<"Address of a in C "<<&a<<endl;
}
};
class D : public B, public C {
public:
D()
{
cout<<"Address of a in D "<<&a<<endl;
}
};
int main()
{
A a;
B b;
C c;
D d;
return 0;
}
So, here when we create an object of D class, Constructors are called in this order A()->B()->C()->D().
Here A() is not called again before C() because we had made use of virtual keyword, which is preventing the constructor A() to be called again.
** Question: 1 But, I wanted to know what is happening in background in code when we are using the virtual keyword?
Question 2 And is variable a in class A and class B point to same memory location?
Question 3 And why this variable's memroy address differs in class C when A is not made virtual from the address of that variable in B?**

Why is my destructor called only once and not on the delete(a) call? [duplicate]

This question already has answers here:
When to use virtual destructors?
(20 answers)
Closed 3 years ago.
using namespace std;
class A {};
class B {};
class C : public A {
public:
C(){ b = new B();}
B* b;
~C(){
printf("in destructor\n");
delete b;
}
};
void doSomething(A&aref)
{
C c = (C &) aref;
c.b = new B();
int i;
}
int main()
{
A * a;
a = new C();
printf("Line 1\n");
doSomething(*a);
printf("Line 2\n");
delete(a);
return 0;
}
Output is:
Line 1
in destructor
Line 2
Tried removing the delete(a) and get same results.
Why don't I see the destructor called twice? I'd expect it to be called at the end of the doSomething function and at the delete call.
Why does the doSomething function call the destructor? And why does the delete(a) not cause an error?
You are missing a virtual destructor in A, hence the undefined behavior.
Default one for defined behavior.
class A {
public:
virtual ~A() = default;
};

calling a member function of a class by member function of another class? [duplicate]

This question already has answers here:
calling a member function of different class from another class
(2 answers)
calling a class method from another class
(1 answer)
Closed 7 years ago.
i have two classes A & B.i want to call a member function of A by member function of B.
class A {
public:
void memberofa();
}
class b:
class B {
public:
void memberofb();
}
now i need to call memberofa from inside memberofb.
Any suggestions and syntaxes will be helpful
Something like this?
class A {
public:
A() {};
void memberofa()
{
//you cant make object of B here because compiler doesn't see B yet
//if you do want to make Object of B here, define this function somewhere
//after definition of B class
printf("printing from member of A\n");
};
};
class B {
public:
B() {};
void memberofb()
{
printf("printing from member of B\n");
A objA;
objA.memberofa();
};
};
int main()
{
A a;
B b;
b.memberofb();
return 0;
}
B inherit from A
B contain A object
A::memberofa is static function
A is singleton class
A inherit from B, B has memberofa and it is virtual function.

Why does an overridden virtual function get called in the base class during construction? [duplicate]

This question already has answers here:
C++ virtual function from constructor [duplicate]
(7 answers)
Closed 7 years ago.
There is a c++ program:
# include <iostream>
using namespace std;
class base
{
public:
base()
{
cout<<"base"<<endl;
f();
}
virtual void f() {
cout<<"base f"<<endl;
}
};
class derive: public base
{
public:
derive()
{
cout<<"derive"<<endl;
f();
}
void f() {
cout<<"derive f"<<endl;
}
};
int main()
{
derive d;
return 1;
}
and it outputs:
base
base f
derive
derive f
I am wondering why base f appears?
I quess in base the constrctor expands to:
cout<<"base"<<endl;
this.f();
But this should point to derive so why base f is print out?
During construction, in the baseclass constructor, the actual type of the object is base, even though it lateron continues to become a derived. The same happens during destruction, btw, and you can also verify the type using typeid.

Why pointers to the same object have different values? [duplicate]

This question already has answers here:
More than 1 address for derived class object?
(2 answers)
Closed 8 years ago.
I've this piece of code:
#include <iostream>
class A
{
public:
A() : m_i(0) { }
protected:
int m_i;
};
class B
{
public:
B() : m_d(0.0) { }
protected:
double m_d;
};
class C : public A, public B
{
public:
C() : m_c('a') { }
private:
char m_c;
};
int main()
{
C d;
A *b1 = &d;
B *b2 = &d;
std::cout << (long)b1 << std::endl <<(long)b2<< std::endl;
}
when compiled and run it produces the following output:
140734705182320
140734705182328
It is not completely clear why different pointers to the same address (&d) have different values.
thanks in advance.
The memory layout of a C object will be something like:
A base_object_1;
B base_object_2;
char m_c;
The two base objects have different addresses; A will (typically) have the same address as the full object, but B will (typically) not. Certainly they can't have the same address as each other, unless at least one is empty.
So converting a pointer to the full object into a pointer to one of the base objects must change the pointer value in order to point to the correct address.