Constructors and class members

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Filip Ruszkowski

    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

    #2
    Re: Constructors and class members

    "Filip Ruszkowski" <filiprus@hotma il.com> wrote in message
    news:3ed27577.0 307240102.38767 dd9@posting.goo gle.com...[color=blue]
    > 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");[/color]

    This does not do what you think it does. See below.
    [color=blue]
    > }
    > 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?[/color]

    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)


    Comment

    • Robert Bauck Hamar

      #3
      Re: Constructors and class members

      *Filip Ruszkowski wrote:[color=blue]
      > 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?[/color]

      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

      Comment

      • Ron Natalie

        #4
        Re: Constructors and class members


        "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message news:3F205FAE.2 060403@jpl.nasa .gov...
        [color=blue]
        > You *can* call your copy constructor like any other function
        > that returns an object of type Test[/color]

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


        Comment

        Working...