Problem: using this pointer

  • Thread starter Stefan Tappertzhofen
  • Start date
S

Stefan Tappertzhofen

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
 
J

Jakob Bieling

Stefan Tappertzhofen said:
class MyClass1
{
public:
double dFoo(MyClass2* , double);
};
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?

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
 
M

Moonlit

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(this, dAnyTime);
-- Or dynamically

#include <memory>
using namespace std;

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



Regards, Ron AF Greve

http://moonlit.xs4all.nl
 
S

Stefan Tappertzhofen

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

Gavin Deane

Stefan said:
Hello NG,

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

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.
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

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!!!
class MyClass1
{
public:
double dFoo(MyClass2* , double);

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);
};

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);

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.

Gavin Deane
 
M

Michiel.Salters

Stefan said:
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!
}

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
 
J

Jakob Bieling

Stefan Tappertzhofen said:
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);

I suppose you are missing 'static' here .. otherwise your code would
compile.
private:
double dxyz;
};

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

Do not use 'this->' unless you absolutely have to. In my opinion, it
just makes reading the codes more difficult.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top