Curs 2 Flashcards
int i = 10;
int *p = &i;
6
yes
int i = 10;
int &refI = i;
6
yes
int i = 10;
int &refI = i;
6
yes
int i = 10;
int *p;
6
yes
int i = 10;
int &refI;
6
Compile error –
uninitialized reference
int i = 10;
int j = 20;
int *p = &i;
p = &j;
7
yes
int i = 10;
int j = 20;
int &refI = i;
&refI = j;
7
Compiler error – trying
to change a reference
that was already
initialized.
int i = 10;
int j = 20;
int p = &i;
p++;
(p) = 30;
8
yes
int i = 10;
int j = 20;
int &refI = i;
refI++;
(&refI)++;
8
eroare
ultimul rand
int i = 10;
char *p = (char *)&i;
10
yes
dar trebuie facut cast explicit!
int i = 10;
int &refI = i;
int & &ref_to_refI = refI;
10
eroare
int *p[100];
11
yes
int &ref[100];
11
no
int *p = &int(10);
11
yes
const int &refI = int(12);
11
yes
This code will not compile if the
“const” specifier is not used as it
refers to a constant numerical
value.
void Compute(int x, double y, char z)
Compute(true, 1.5f, ‘A’)
21
promotie
Compute(1, 1.5, ‘A’)
void Compute(int x, double y, char z)
Compute(3.5, 1.5, ‘A’)
22
conversie
Compute(3, 1.5, ‘A’)
void Compute(int x, const void* y, char z)
Compute(NULL, “C++”, ‘A’)
23
cast
Compute(0, (const void*)”C++”, ‘A’)
class Math
{
public:
int Inc(int v1);
float Inc(float v1);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
float Math::Inc(float v1)
{
return v1+1.0f;
}
void main()
{
Math m;
m.Inc(100);
}
25
int Math::Inc(int v1)
class Math
{
public:
int Inc(int v1);
float Inc(float v1);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
float Math::Inc(float v1)
{
return v1+1.0f;
}
void main()
{
Math m;
m.Inc(1.0f);
}
26
float Math::Inc(float v1)
class Math
{
public:
int Inc(int v1);
float Inc(float v1);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
float Math::Inc(float v1)
{
return v1+1.0f;
}
void main()
{
Math m;
m.Inc(‘a’);
}
27
int Math::Inc(int v1)
class Math
{
public:
int Inc(short v1);
float Inc(float v1);
};
int Math::Inc(short v1)
{
return v1 + 1;
}
float Math::Inc(float v1)
{
return v1+1.0f;
}
void main()
{
Math m;
m.Inc(‘a’);
}
28
ambiguu
class Math
{
public:
int Inc(int v1);
float Inc(float v1);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
float Math::Inc(float v1)
{
return v1+1.0f;
}
void main()
{
Math m;
m.Inc(1.0);
}
30
ambiguu
class Math
{
public:
int Inc(char v1) {
return v1 + 1;
}
};
void main()
{
Math m;
m.Inc(1.0);
}
31
compile
warning C4244: ‘argument’:
conversion from ‘double’ to
‘char’, possible loss of data
class Math
{
public:
int Inc(char* v1) { return 1; }
};
void main()
{
Math m;
m.Inc(1.0);
}
32
error C2664: ‘int Math::Inc(char *)’:
cannot convert argument 1 from
‘double’ to ‘char *’
class Math
{
public:
int Inc(char* v1);
};
int Math::Inc(char* v1)
{
return 1;
}
void main()
{
Math m;
double d = 1.0;
m.Inc(&d);
}
33
error C2664: ‘int Math::Inc(char *)’:
cannot convert argument 1 from
‘double *’ to ‘char *’
class Math
{
public:
int Inc(char* v1);
};
int Math::Inc(char* v1)
{
return 1;
}
void main()
{
Math m;
double d = 1.0;
m.Inc( (char *)&d );
}
34
yes
class Math
{
public:
int Inc(void* v1);
};
int Math::Inc(void* v1)
{
return 1;
}
void main()
{
Math m;
double d = 1.0;
m.Inc(&d);
}
35
any non-constant pointer can be converted to “void *”. The next
example will compile.
class Math
{
public:
int Inc(int v1);
int Inc(…);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
int Math::Inc(…)
{
return 1;
}
void main()
{
Math m;
m.Inc(123);
}
37
int Math::Inc(int v1)
class Math
{
public:
int Inc(int v1);
int Inc(…);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
int Math::Inc(…)
{
return 1;
}
void main()
{
Math m;
m.Inc(‘a’);
}
38
int Math::Inc(int v1)
class Math
{
public:
int Inc(int v1);
int Inc(…);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
int Math::Inc(…)
{
return 1;
}
void main()
{
Math m;
m.Inc(1.0);
}
39
int Math::Inc(int v1)
class Math
{
public:
int Inc(int v1);
int Inc(float v1);
int Inc(…);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
int Math::Inc(float v1)
{
return v1 + 1;
}
int Math::Inc(…)
{
return 1;
}
void main() {
Math m;
m.Inc(1.0);
}
40
error C2668: ‘Math::Inc’: ambiguous call to overloaded function
note: could be ‘int Math::Inc(float)’
note: or ‘int Math::Inc(int)’
note: while trying to match the argument list ‘(double)
class Math
{
public:
int Inc(int v1);
int Inc(…);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
int Math::Inc(…)
{
return 1;
}
void main()
{
Math m;
m.Inc(“test”);
}
41
int Inc(…);
class Math
{
public:
int Inc(int v1);
int Inc(…);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
int Math::Inc(…)
{
return 1;
}
void main()
{
Math m;
m.Inc(1.0,2);
}
42
int Math::Inc(…)