Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Constructors and class members (http://www.velocityreviews.com/forums/t275995-constructors-and-class-members.html)

Filip Ruszkowski 07-24-2003 09:02 AM

Constructors and class members
 
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

Jakob Bieling 07-24-2003 09:11 AM

Re: Constructors and class members
 
"Filip Ruszkowski" <filiprus@hotmail.com> wrote in message
news:3ed27577.0307240102.38767dd9@posting.google.c om...
> 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
--
jb

(replace y with x if you want to reply by e-mail)



Robert Bauck Hamar 07-24-2003 09:31 AM

Re: Constructors and class members
 
*Filip Ruszkowski wrote:
> 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);
}

--------------

--
Robert Bauck Hamar

Filip Ruszkowski 07-24-2003 10:12 PM

Constructors and class members
 
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

Robert Bauck Hamar <hamrob_b@yahoo.no> wrote in message news:<bfo91t$msm$1@maud.ifi.uio.no>...
> 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);
> }
>
> --------------


Filip Ruszkowski 07-24-2003 10:12 PM

Constructors and class members
 
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

Robert Bauck Hamar <hamrob_b@yahoo.no> wrote in message news:<bfo91t$msm$1@maud.ifi.uio.no>...
> 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);
> }
>
> --------------


E. Robert Tisdale 07-24-2003 10:37 PM

Re: Constructors and class members
 
Filip Ruszkowski wrote:

> 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.


Ron Natalie 07-25-2003 02:18 PM

Re: Constructors and class members
 

"E. Robert Tisdale" <E.Robert.Tisdale@jpl.nasa.gov> wrote in message news:3F205FAE.2060403@jpl.nasa.gov...

> 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.




All times are GMT. The time now is 01:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.