views:

55

answers:

1

Hi! :)

I have the following problem:

//A.h

class A
{
  //...
  // this is the important part, i have to call this in the proper way
  // in A::SetNewValue(), but i don't know how to do that
  protected:
    void SetValue(const int* i);
  //...

  public:
    // ??
    void SetNewValue(const int* p);
}

the cpp:

//A.cpp
//??
A::SetNewValue(const int* p)
{
  // ??
  this->SetValue(&p);    
}

and...

//...
// and later in another file...
//...
A a = new A();

int a_value = 4;
int* p;
p=&value;

// ??
a->SetNewValue(p);

The problem explained: class A is a built-in class in a framework. I have no way to modify protected A::SetValue() to public, and I can't reach it from 'outside'. So i've decided to write another function A::SetNewValue() to call A::SetValue, but I don't know how to pass pointers and references in function parameters. I've always got erros like: can't convert from * to &, const * to *, and so on...

How can i do this in a proper way? Is this even possible?

Thank you very much for your effort, and for your help.

Edit: Code above is a sample.. I've tried passing parameters in several ways

+1  A: 

SetValue takes a pointer just like SetNewValue so you can pass the pointer value straight through:

void A::SetNewValue(const int* p)
{
    SetValue(p);    
}

I also fixed the missing void return type in your function definition.

You should be able to call it with a pointer to int or const int because you can always add a const at the top level when passing pointer.:

A a;

int a_value = 4;

a.SetNewValue(&a_value);

I fixed your a variable to be just default constructed; I think that trying initialize a non-pointer a with a pointer to A (from new A) was probably a mistake.

I changed -> to . as a is not a pointer.

Charles Bailey
Thank you! It worked.
Zsolt
Actually... it did not work... the program crashed, but the editor gave me a green go. :SI can't make SeValue public, because class A was inherited from another class which is part of a framework. The main problem is, that I need to set a value with a protected function, but the most biggest problem is, that i call A->SetValue() in another class called B and i need to pass B to the function.inside one of B's function:A->SetNewValue(this);
Zsolt
The Editor gave me a green go (at last, thanks for your help). But when I run the program, it crashes with no error. :S
Zsolt
@Zsolt: Unfortunately, I can't see enough of your code to guess at what the crash could be. Trying to pass `this` to `SetNewValue` looks wrong because `this` will be a pointer to `B`, not a pointer to `int`. Can you provide a complete compilable code sample that exhibits your problem?
Charles Bailey
The truth is: my above code was not from my source files. I did not understand how to pass pointers through functions, so i just wrote an example. I have to pass class pointers and references in my real code, so after a while, i got really confused and my code did not worked. This was a point in my confusion when i decided to ask someone here.I wouldn't like to share my code right now, because i'm making it for a competition. I hope you won't have hard feelings about me. And again, thanks for your help.
Zsolt
@Zsolt: Of course no hard feelings, I was just pointing out that we can only help you with what you share.
Charles Bailey