Constructors and class members

Discussion in 'C++' started by Filip Ruszkowski, Jul 24, 2003.

  1. 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
     
    Filip Ruszkowski, Jul 24, 2003
    #1
    1. Advertising

  2. "Filip Ruszkowski" <> wrote in message
    news:...
    > 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)
     
    Jakob Bieling, Jul 24, 2003
    #2
    1. Advertising

  3. *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
     
    Robert Bauck Hamar, Jul 24, 2003
    #3
  4. 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 <> wrote in message news:<bfo91t$msm$>...
    > 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, Jul 24, 2003
    #4
  5. 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 <> wrote in message news:<bfo91t$msm$>...
    > 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, Jul 24, 2003
    #5
  6. 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.
     
    E. Robert Tisdale, Jul 24, 2003
    #6
  7. Filip Ruszkowski

    Ron Natalie Guest

    "E. Robert Tisdale" <> wrote in message news:...

    > 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.
     
    Ron Natalie, Jul 25, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CoolPint
    Replies:
    8
    Views:
    1,014
    Jeff Schwab
    Dec 14, 2003
  2. Dave Rudolf
    Replies:
    12
    Views:
    8,360
    Martijn Lievaart
    Feb 6, 2004
  3. Jeremy Smith
    Replies:
    2
    Views:
    609
    Jeremy Smith
    Aug 3, 2006
  4. Peng Yu
    Replies:
    5
    Views:
    406
    Juha Nieminen
    Sep 19, 2008
  5. Pallav singh
    Replies:
    4
    Views:
    300
    James Kanze
    Dec 7, 2010
Loading...

Share This Page