Reputation: 4431
My case is the following: In my larger program I want to make an interface to grab frames from a imagesource. However, in the program I do not know in advance whether I want to grab frames from a camera or from a video file. But I would like to have one interface that handles video files and camera frames as identical. This in total is is taking much code to accomplish, thus I'll present a very short example.
this is the example of the interface:
class Input {
public :
virtual void
get_frame() = 0;
};
This is a derived class that should grab frames from a camera.
class CameraInput: public Input {
public :
void get_frame();
};
This is the class that should open a videofile and grab frames from the file.
class VideoInput: public Input {
public :
void get_frame();
};
Here are the definitions of the get_frame
from the Input derived classes. As you can see they "produce" a different type of video frame.
void
CameraInput::get_frame(){
std::cout << "camera-frame" << std::endl;
};
void
VideoInput::get_frame() {
std::cout << "video-frame" << std::endl;
};
This it the code that instantiates an object of type CameraInput
or VideoInput
.
Edit shared pointer added
typedef enum { VIDEO, CAMERA } input_type ;
std::shared_ptr<Input> framegrabber;
/*Does this have to be a pointer?*/
/*In my program I don't know this in advance.*/
input_type my_input = CAMERA;
switch ( my_input ){
case CAMERA:
framegrabber = std::shared_ptr<Input>(new CameraInput);
break;
case VIDEO:
framegrabber = std::shared_ptr<Input>(new VideoInput);
break;
default:
/*error handling here*/
break;
};
framegrabber->get_frame();
On stackoverflow I read many times in c++ answers, that one should never use pointers unless one really, really needs to. This is where my question comes from, is it possible and better to rewite the code above without using a Input*
or is this one of the case where it is mandatory to use pointers?
kind regards,
Upvotes: 0
Views: 427
Reputation: 19282
You could use a smart pointer (e.g. std::unique_ptr or std::shared_ptr) instead of the raw pointers.
If you want polymorphism, you must use pointers or references. There are far more details than you really want here
Upvotes: 0
Reputation: 26429
On stackoverflow I read many times in c++ answers, that one should never use pointers unless one really, really needs to.
When you read any advice, you should try to understand WHY this advice is given. If there's no good reason behind advice, there's no need to follow that advice.
Trying to eliminate all pointers from your program just because somebody told you "pointers are bad" is not a good idea.
Experienced programmers tell you to avoid raw pointers, because pointers mean manual memory management, which is error prone (forget to delete something == memory leak), plus you have to be aware of rule of three.
I also have impression that inexperienced programmers frequently give the same advice because they simply do not understand pointers completely (or are afraid of them?).
You CAN use references, but because you can't copy/reassign references that'll be less useful. Example:
#include <iostream>
class A{
public:
virtual int getVal() const = 0;
virtual ~A(){}
};
class B: public A{
public:
virtual int getVal() const { return 2; };
};
class C: public A{
public:
virtual int getVal() const { return 3; };
};
class D{
public:
const A& getInterface(bool which) const{
if (which)
return c;
return b;
}
protected:
B b;
C c;
};
int main(int argc, char** argv){
D d;
std::cout << d.getInterface(true).getVal() << std::endl;
std::cout << d.getInterface(false).getVal() << std::endl;
return 0;
}
As you can see, to return object by reference, you'll need to create it somewhere, and store it. If you want to "kill all pointers", then you'll need to create this object as a member variable of bigger class (or as a global variable).
With pointer, you could simply return shared_ptr<A>
.
In your code, the better idea would be to use smart pointer
class instead of raw pointers. That'll eliminate need for manual delete/memory management.
it possible and better to rewite the code above without using a Input*
Yes, it is possible, but there's no reason to do it, and this is (IMO) NOT better. My example illustrates how you could do it. Without pointers(of any kind - smart or raw) you'll also have to store BOTH CameraInput and VideoInput somewhere, while with pointers you can create them on demand.
Upvotes: 3