Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Constructors and class members

Reply
Thread Tools

Constructors and class members

 
 
Filip Ruszkowski
Guest
Posts: n/a
 
      07-24-2003
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
 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      07-24-2003
"Filip Ruszkowski" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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)


 
Reply With Quote
 
 
 
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      07-24-2003
*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
 
Reply With Quote
 
Filip Ruszkowski
Guest
Posts: n/a
 
      07-24-2003
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 <(E-Mail Removed)> wrote in message news:<bfo91t$msm$(E-Mail Removed)>...
> 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);
> }
>
> --------------

 
Reply With Quote
 
Filip Ruszkowski
Guest
Posts: n/a
 
      07-24-2003
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 <(E-Mail Removed)> wrote in message news:<bfo91t$msm$(E-Mail Removed)>...
> 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);
> }
>
> --------------

 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      07-24-2003
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.

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      07-25-2003

"E. Robert Tisdale" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Default Constructors and initialization of members of class Pallav singh C++ 4 12-07-2010 09:14 AM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM
Can nested class members access private members of nesting class? CoolPint C++ 8 12-14-2003 02:30 PM



Advertisments