Re: Passing an object pointer to function



In article <F1E47B10-0892-4320-9AB7-2B983AE088BA@xxxxxxxxxxxxx>, Robby
wrote...

> The above code works fine, however what if in main I wanted to create a
> pointer to an object on the heap using the following line:
>
> SimpleCat * pRags = new SimpleCat;
>
> and then passing only the pointer of the object to my function: Please
> consider the following code which does not complile?

[...]
// Pass a pointer to a SimpleCat object
// not a pointer to an int
void Function1(SimpleCat *pRags);

int main()
{
int a;
SimpleCat * pRags = new SimpleCat;
// pRages is already a pointer so you can omit the &
Function1(pRags);
// Use the -> operator
cout << Frisky->GetAge();
}

// Se above
void Function1(SimpleCat *pRags)
{
pRags->SetAge(50);
}

--
stefan
.



Relevant Pages

  • Re: "delete" causes prog to crash!
    ... use it to a pointer that points to the heap. ... and isn't a pointer supposed to hold the address of some data location. ... >> class codes ... >> int returnCode() const; ...
    (microsoft.public.vc.language)
  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: C# - getting binary data from .lib
    ... Use Marshal.AllocHGlobal to allocate the memory and pass this IntPtr to the ... int retCode = create(id, scale, ptrImage); ... You now control the pointer returned. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: "delete" causes prog to crash!
    ... int codes::returnCodeconst ... >> I have declared a pointer on the heap as: ... > an int from the heap. ...
    (microsoft.public.vc.language)
  • Re: Another spinoza challenge
    ... You should test against the int type's limits: ... typedef struct complex ... a pointer to an integer ... A macro is preferable because it is replaced by inline code, ...
    (comp.lang.c)