Constructors and class members

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
 
J

Jakob Bieling

Filip Ruszkowski said:
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");

This does not do what you think it does. See below.
}
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?

You cannot 'call' a constructor. Every 'call' to a constructor creates a
new object. Above, you create a temporary object of type Test using the
Test(char*) c'tor. It is just a temporary and does not affect the current
object. This is why you will always see a '2' printed.

hth
 
R

Robert Bauck Hamar

*Filip Ruszkowski said:
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?

You are wrong. Test::Test(int) first assigns the value 2 to val, and
then it creates a temporary Test, which is not used.

Try this, and see that the pointers are not equal.
--------------
#include <iostream>

class Test {
public:
Test(char*)
{ std::cout << "char*: " << this << '\n'; }

Test(int)
{ std::cout << "int : " << this << '\n'; Test("a"); }
};

int main()
{
Test t(5);
}
 
F

Filip Ruszkowski

Thanks for your answers!

Is there any way of calling a constructor from another constructor. I
understand that Test("a") is not doing this.

Thanks,
Filip
 
F

Filip Ruszkowski

Thanks for your answers!

Is there any way of calling a constructor from another constructor. I
understand that Test("a") is not doing this.

Thanks,
Filip
 
E

E. Robert Tisdale

Filip said:
Take a quick look at the following code:

class Test {
private:
int val;
public:
Test(char *s): val (1) { }
Test(int k): val(2) {

// You probably meant to write:
*this = Test("aaa");
}
void print(void) {
printf("%d\n", val);
}
};

int main(int argc, char* argv[]) {
Test 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?

You *can* call your copy constructor like any other function
that returns an object of type Test
but unless you assign the return value to an object (*this for example)
it will be lost when the thread of execution exits the current scope.
 
R

Ron Natalie

E. Robert Tisdale said:
You *can* call your copy constructor like any other function
that returns an object of type Test

You can't call constructors.
Constructors don't have return values.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top