F
Filip Ruszkowski
Hi!
Take a quick look at the following code:
class Test {
private:
int val;
public:
Test(char *s)
{
val=1;
}
Test(int k)
{
val=2;
Test("aaa");
}
void print()
{
printf("%d\n", val);
}
};
int main(int argc, char* argv[])
{
Test *t;
t=new Test(2);
t->print();
delete t;
return 0;
}
The program stubbornly prints 2 every time I run it. I would have
thought that the Test(int) constructor should set val=2 and then the
Test(char *) constructor should set the value of val equal to 1. This
program should print 1. What is wrong?
Thanks,
Filip
Take a quick look at the following code:
class Test {
private:
int val;
public:
Test(char *s)
{
val=1;
}
Test(int k)
{
val=2;
Test("aaa");
}
void print()
{
printf("%d\n", val);
}
};
int main(int argc, char* argv[])
{
Test *t;
t=new Test(2);
t->print();
delete t;
return 0;
}
The program stubbornly prints 2 every time I run it. I would have
thought that the Test(int) constructor should set val=2 and then the
Test(char *) constructor should set the value of val equal to 1. This
program should print 1. What is wrong?
Thanks,
Filip