Problem: using this pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefan Tappertzhofen

    #1

    Problem: using this pointer

    Hello NG,

    I've got a problem with function parameters. Here is my first class:

    class MyClass1
    {
    public:
    double dFoo(MyClass2* , double);
    };

    The function dFoo is implementated as:

    double MyClass1::dFoo( MyClass2* ptCls2, double dTimer)
    {
    // (...)
    }

    My problem is that I have to call the function MyClass1::dFoo in class
    MyClass2:

    void MyClass2::vBaa( void)
    {
    double dXYZ= MyClass1::dFoo( this, dAnyTime);
    }

    But this doesn't work with Visual Studio 2005. Is there anybody who can help
    me?

    Regards,

    Stefan

    --
    Stefan Tappertzhofen


  • Moonlit

    #2
    Re: Problem: using this pointer

    Hi,

    It doesn't compile with visual C++ (or any regular C++ compiler) because
    your code is wrong.

    The way you call it can only be used when the function is declared static
    (i.e. it doesn't actually use the this pointer).

    --

    You should either
    - declare dFoo static (i.e. doesn't have access to object data)
    static double dFoo(MyClass2* , double);

    - Allocate MyClass1 on the stack
    // Note the dot and the class name
    MyClass1 MyClass;
    double dXYZ= MyClass.dFoo(th is, dAnyTime);
    -- Or dynamically

    #include <memory>
    using namespace std;

    auto_ptr<MyClas s1> MyClass( new MyClass1 );
    double dXYZ= MyClass->dFoo(this, dAnyTime);



    Regards, Ron AF Greve



    "Stefan Tappertzhofen" <stefan.tappert zhofen@rwth-aachen.de> wrote in
    message news:4926nbFlce qaU1@news.dfnci s.de...[color=blue]
    > Hello NG,
    >
    > I've got a problem with function parameters. Here is my first class:
    >
    > class MyClass1
    > {
    > public:
    > double dFoo(MyClass2* , double);
    > };
    >
    > The function dFoo is implementated as:
    >
    > double MyClass1::dFoo( MyClass2* ptCls2, double dTimer)
    > {
    > // (...)
    > }
    >
    > My problem is that I have to call the function MyClass1::dFoo in class
    > MyClass2:
    >
    > void MyClass2::vBaa( void)
    > {
    > double dXYZ= MyClass1::dFoo( this, dAnyTime);
    > }
    >
    > But this doesn't work with Visual Studio 2005. Is there anybody who can
    > help me?
    >
    > Regards,
    >
    > Stefan
    >
    > --
    > Stefan Tappertzhofen
    >
    >[/color]


    Comment

    • Jakob Bieling

      #3
      Re: Problem: using this pointer

      Stefan Tappertzhofen <stefan.tappert zhofen@rwth-aachen.de> wrote:
      [color=blue]
      > class MyClass1
      > {
      > public:
      > double dFoo(MyClass2* , double);
      > };[/color]
      [color=blue]
      > double MyClass1::dFoo( MyClass2* ptCls2, double dTimer)
      > {
      > // (...)
      > }
      >
      > My problem is that I have to call the function MyClass1::dFoo in class
      > MyClass2:
      >
      > void MyClass2::vBaa( void)
      > {
      > double dXYZ= MyClass1::dFoo( this, dAnyTime);
      > }
      >
      > But this doesn't work with Visual Studio 2005. Is there anybody who
      > can help me?[/color]

      If a function is a member of a class, it means that this function
      will do something with or to an object of that type. This implies that
      whenever the member function is called, there will be an object that the
      function will do something with/to. In your case, there is no object.
      That is why it does not work.

      To make this work, you can either make sure that you provide an
      object and call your member function on that object. This could look
      like this, for example:

      void MyClass2::vBaa( MyClass1& p)
      {
      double dXYZ= p.dFoo (this, dAnyTime);
      }

      Another way is to make dFoo a static member function of MyClass1.
      Then it will not need an object to work with and your code for vBar will
      work as you have it there:

      class MyClass1
      {
      public:
      static double dFoo(MyClass2* , double);
      };



      hth
      --
      jb

      (reply address in rot13, unscramble first)


      Comment

      • Stefan Tappertzhofen

        #4
        Re: Problem: using this pointer

        Hello,

        I can't change the way MyClass2::vBaa( ) as it is requieret. I tried the way
        with static befor but when I use it I can't get Access to any variables of
        Class MyClass1:

        class MyClass1
        {
        public:
        double dFoo(MyClass2* , double);
        private:
        double dxyz;
        };

        double MyClass1::dFoo( MyClass2* ptCls2, double dTimer)
        {
        this->dxyz = dTimer; // Doesnt work!
        }


        ST.

        --
        Stefan Tappertzhofen


        Comment

        • Gavin Deane

          #5
          Re: Problem: using this pointer


          Stefan Tappertzhofen wrote:[color=blue]
          > Hello NG,
          >
          > I've got a problem with function parameters. Here is my first class:[/color]

          It doesn't look like an error with function parameters. It looks like
          an error with the way you are calling a member function. But you
          haven't provided minimal compileable code (as per the FAQ) and you
          haven't given the error message from your compiler.


          I've had to guess a bit, which might mean I've guessed wrong and I'm
          wasting my time and not helping you. Have a look at what I've written
          and if I've missed the point, post again following the guidelines in
          the FAQ.

          And as for those Hungarian warts all over the place, my eyes are
          burning!!!
          [color=blue]
          > class MyClass1
          > {
          > public:
          > double dFoo(MyClass2* , double);[/color]

          This is a non-static member function, so it is called by invoking it on
          an object of type MyClass1. If dFoo is logically part of MyClass1 but
          doesn't need access to any of the internals (member data or other
          non-static member-functions) then you can (and should) make it static

          static double dFoo(MyClass2* , double);
          [color=blue]
          > };
          >
          > The function dFoo is implementated as:
          >
          > double MyClass1::dFoo( MyClass2* ptCls2, double dTimer)
          > {
          > // (...)
          > }
          >
          > My problem is that I have to call the function MyClass1::dFoo in class
          > MyClass2:
          >
          > void MyClass2::vBaa( void)
          > {
          > double dXYZ= MyClass1::dFoo( this, dAnyTime);[/color]

          Here you try and call dFoo, but where is the object of MyClass1 type?
          Either create a MyClass1 object, or make dFoo a static member function
          as I showed above.
          [color=blue]
          > }[/color]

          Gavin Deane

          Comment

          • Michiel.Salters@tomtom.com

            #6
            Re: Problem: using this pointer

            Stefan Tappertzhofen wrote:[color=blue]
            > Hello,
            >
            > I can't change the way MyClass2::vBaa( ) as it is requieret. I tried the way
            > with static befor but when I use it I can't get Access to any variables of
            > Class MyClass1:
            >
            > class MyClass1
            > {
            > public:
            > double dFoo(MyClass2* , double);
            > private:
            > double dxyz;
            > };
            >
            > double MyClass1::dFoo( MyClass2* ptCls2, double dTimer)
            > {
            > this->dxyz = dTimer; // Doesnt work!
            > }[/color]

            Well, that's obvious. In a non-static dFoo, this-> points to the
            MyClass object
            you used (in vBaa). If dFoo is static, you don't need a MyClass object
            in vBaa,
            and as a result there is no this-> pointer.

            HTH,
            Michiel Salters

            Comment

            • Jakob Bieling

              #7
              Re: Problem: using this pointer

              Stefan Tappertzhofen <stefan.tappert zhofen@rwth-aachen.de> wrote:[color=blue]
              > Hello,
              >
              > I can't change the way MyClass2::vBaa( ) as it is requieret. I tried
              > the way with static befor but when I use it I can't get Access to any
              > variables of Class MyClass1:
              >
              > class MyClass1
              > {
              > public:
              > double dFoo(MyClass2* , double);[/color]

              I suppose you are missing 'static' here .. otherwise your code would
              compile.
              [color=blue]
              > private:
              > double dxyz;
              > };
              >
              > double MyClass1::dFoo( MyClass2* ptCls2, double dTimer)
              > {
              > this->dxyz = dTimer; // Doesnt work![/color]

              Do not use 'this->' unless you absolutely have to. In my opinion, it
              just makes reading the codes more difficult.
              [color=blue]
              > }[/color]

              Re-read the part of my reply about why it does not work. If you
              cannot change vBar, then dFoo *must* be a static member of MyClass1 and
              you need to find other ways to pass the object that contains the 'dxyz'
              member you want to modify.

              hth
              --
              jb

              (reply address in rot13, unscramble first)


              Comment

              Working...