[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

replace an object with a pointer in the function parameter ...

Hi Experts,

    I have a function like :

   myImg myFunA(myImg &Img1, myImg &Img2) {}

   now, instead of pass Img1, Img2, I would like to pass a pointer of the image to the memory. (Therefore, it can be more general ... in the future, perhaps it can work for yourImg as well)

   so ... can anyone tell me how to do it or give me an example ???
   (may I use  char * ???)

  many thanks !!!
0
meow00
Asked:
meow00
  • 3
  • 3
2 Solutions
 
itsmeandnobodyelseCommented:
You would need a common baseclass - say Img - both for myImg and yourImg. Then myFunA could have baseclass pointers as arguments:

  class Img
  {
  };
  class myImg : public Img
  {
  };
  class Img : public Img
  {
  };

  Img* myFunA(Img* pImg1, Img* pImg2) {}

You would need virttal functions to operate on baseclass pointer.

Regards, Alex

0
 
AxterCommented:
You can have use a common  based class, or have be a template function.
In either case, I recommend you use pass by reference, as in your original code.

BaseClass &myFunA(BaseClass &Img1, BaseClass &Img2) {}

template<class T>
T& myFunA(T& Img1, T& Img2) {}

The return type could be pass by value or reference, depending on your requirements.

0
 
balderCommented:
when saying " I would like to pass a pointer of the image to the memory."

did you imply that instead of passing a pointer to an object defining an image you would like to transfer a memory pointer for raw image data,
 if so, you can create functions like

typedef unsigned char* image_data_ptr;

image_data_ptr myTIFFfun(image_data_ptr Img1, image_data_ptr Img2);
image_data_ptr myGIFfun(image_data_ptr Img1, image_data_ptr Img2)

(but, this is not in the spirit of C++ :))
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
itsmeandnobodyelseCommented:
>>>> I recommend you use pass by reference

I would share that recommendation in case you don't need images of different derived classes in a container or array cause you couldn't store baseclass references to an array.

    Baseclass& array[100];   // that doesn't compile cause references don't have a nil value

However, when using virtual functionality, you normally have a set (array/list/map) of baseclass pointers to operate on. Then, it doen't make much sense always to dereference pointers in order to get the required reference arguments. It's ok to use a const pointer instead.

Regards, Alex
0
 
AxterCommented:
>>I would share that recommendation in case you don't need images of different derived classes in a container or array cause you couldn't store baseclass references to an array.

No.

You can't pass an array of different type derive objects using just a base pointer.

Img* myFunA(Img* pImg1, Img* pImg2) {}

The above pointer will not work for an array of different size derived objects.
So using a base pointer for an array of different derived objects should not be done.
You can however >>I would share that recommendation in case you don't need images of different derived classes in a container or array cause you couldn't store baseclass references to an array.

No.

You can't pass an array of different type derive objects using just a base pointer.

Img* myFunA(Img* pImg1, Img* pImg2) {}

The above pointer will not work for an array of different size derived objects.
So using a base pointer for an array of different derived objects should not be done.
You can have a container that has an array of pointers like vector<Img*> which which could contain different size derived objects.

But then it would be better to pass such a container by referrence.

void myFunA( vector<Img*> &Img1,  vector<Img*>& Img2) {}

In either case, passing by reference would be the better choice.

0
 
AxterCommented:
So how, got a copy and paste problem with above post.

Should just be the following:

>>I would share that recommendation in case you don't need images of different derived classes in a container or array cause you couldn't store baseclass references to an array.

No.

You can't pass an array of different type derive objects using just a base pointer.

Img* myFunA(Img* pImg1, Img* pImg2) {}

The above pointer will not work for an array of different size derived objects.
So using a base pointer for an array of different derived objects should not be done.
You can have a container that has an array of pointers like vector<Img*> which which could contain different size derived objects.

But then it would be better to pass such a container by referrence.

void myFunA( vector<Img*> &Img1,  vector<Img*>& Img2) {}

In either case, passing by reference would be the better choice.

0
 
itsmeandnobodyelseCommented:
>>>> You can't pass an array of different type derive objects using just a base pointer.

My intention is an array (or any other container) of base pointers and *not* a base pointer pointing at the first baseclass object (???). You would need dereferencing when passing *one* of these pointers (in the container) to a function that has only (single) references as arguments. In that case passing by reference isn't suitable, cause you have to dereference and to check for NULL pointers.


>>>> ....in case you don't need images of different derived classes in a container or array
>>>> cause you couldn't store baseclass references to an array.


Axter, you may have problems sometimes to understand my poor English, and I apologize for that. But, the statement above neither refers to a baseclass pointer pointing to an array nor does it talk of passing an array to a function. So, your 'no' responses were as hard to understand as my comment.

Regards, Alex






0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now