Curs 6 Flashcards
class A { public: int a1, a2, a3; };
class B { public: int b1, b2; };
class C : private A, private B { public: int c1, c2; };
void main(void)
{
C c;
A* a = &c;
}
11
no
derivarea e privata
error C2243: ‘type cast’: conversion from
‘C *’ to ‘A *’ exists, but is inaccessible
class A { public: int a1, a2, a3; };
class B { public: int b1, b2; };
class C : private A, private B { public: int c1, c2; };
void main(void)
{
C c;
A* a = reinterpret_cast<A*> (&c);
}
25
yes
This code will compile. It is important to understand the reinterpret_cast is
not bounded by class type or access modifier type.
int x = reinterpret_cast <int>(1000);</int>
26
eroare
int y = 100;
int x = reinterpret_cast <int>(y);</int>
26
eroare
int x = reinterpret_cast<int>(“test”);</int>
26
yes
char x = reinterpret_cast<char>(“test”);</char>
26
yes cu warning trunchiere date
float x = reinterpret_cast<float>(1.2f);</float>
27
eroare
double x = reinterpret_cast<double>(1.2);</double>
27
eroare
int number = 10;
reinterpret_cast<int &>(number) = 20;
27
yes
int number = 10;
reinterpret_cast<char &>(number) = 20;
27
yes
int x = static_cast<int>(1000);</int>
30
yes
char x = static_cast<char>(1000);</char>
30
yes
232=1000%256
char x = static_cast<char>(3.75);</char>
30
yes
3=int(3.75)
char x = static_cast<char>("test");</char>
30
eroare
const char * x = static_cast<const char *>(9);
30
eroare
class A { public: int a1, a2, a3; };
class B { public: int b1, b2; };
void main(void)
{
B b;
A* a = static_cast<A *>(&b);
}
31
eroare
class A { public: int a1, a2, a3; };
class B
{
public:
int b1, b2;
operator A* () { return new A(); }
};
void main(void)
{
B b;
A* a = static_cast<A *>(&b);
}
32
eroare
A* a = static_cast<A *>(&b); are parametru &b in loc de b
class A { public: int a1, a2, a3; };
class B
{
public:
int b1, b2;
operator A* () { return new A(); }
};
void main(void)
{
B b;
A* a = static_cast<A *>(b);
}
33
yes
int Add(int x, char y)
{
return x + y;
}
int Add(char x, int y)
{
return x + y;
}
void main(void)
{
int suma = static_cast<int(*) (int, char)> (Add)(100, 200);
}
35
yes
class A { public: int a1, a2, a3; };
class B
{
public:
int b1, b2;
};
class C : public A, public B { public: int c1, c2; };
void main(void)
{
B b;
C c = dynamic_cast<C>(&b);
}
38
eroare
‘dynamic_cast’: ‘B’ is not a polymorphic type
note: see declaration of ‘B’
class A { public: int a1, a2, a3; };
class B
{
public:
int b1, b2;
virtual void f() {};
};
class C : public A, public B { public: int c1, c2; };
void main(void)
{
B b;
C c = dynamic_cast<C>(&b);
}
39
yes
c este nullptr pt ca b este obiect de sine statator
class A { public: int a1, a2, a3; };
class B
{
public:
int b1, b2;
virtual void f() {};
};
class C : public A, public B { public: int c1, c2; };
void main(void)
{
C c;
B b = (B)&c;
C c2 = dynamic_cast<C>(b);
}
40
yes
class A {
public: int a1, a2, a3;
virtual void f2() {};
};
class B {
public: int b1, b2;
virtual void f() {};
};
class C : public A, public B { public: int c1, c2; };
void main(void) {
C c;
A a = (A)&c;
B b = dynamic_cast<B>(a);
}
41
This code also compiles.
While there is no relationship between A and B , since local variable “a”
points to an object of type C , that contains a “B” component, this conversion
is possible.
void main(void)
{
int x = 100;
const int * ptr = &x;
int * non_const_pointer = const_cast <int *>(ptr);
*non_const_pointer = 200;
printf(“%d”, x);
}
43
This code will compile and will convert ptr const pointer to a non-constant
pointer that can be used to change the value of “x”
As a result, the program will print “200” on the screen