in Education by
Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 13 years ago. Improve this question I am currently suffering a brain fart. I've done this before but I can't remember the exact syntax and I can't look at the code I wrote because I was working at another company at the time. I have this arrangement: class P { // stuff }; class PW : public P { // more stuff }; class PR : public P { // more stuff }; class C { public: P GetP() const { return p; } private: P p; }; // ... P p = c.GetP( ); // valid PW p = c.GetP( ); // invalid PR p = c.GetP( ); // invalid // ... Now I would like to make P interchangeable with PW and PR (and thus PW and PR can be interchanged). I could probably get away with casts but this code change has occurred quite a few times in this module alone. I am pretty sure it is a operator but for the life of me I can't remember what. How do I make P interchangeable with PW and PR with minimal amount of code? Update: To give a bit more clarification. P stands for Project and the R and W stands for Reader and Writer respectively. All the Reader has is the code for loading - no variables, and the writer has code for simply Writing. It needs to be separate because the Reading and Writing sections has various manager classes and dialogs which is none of Projects real concern which is the manipulation of project files. Update: I also need to be able to call the methods of P and PW. So if P has a method a() and PW as a method call b() then I could : PW p = c.GetP(); p.a(); p.b(); It's basically to make the conversion transparent. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
If you want to get this part to compile: // ... P p = c.GetP( ); // valid PW p = c.GetP( ); // invalid PR p = c.GetP( ); // invalid // ... You need to be able to construct/convert a P into a PW or a PR. You need to do something like this: class PW : public P { PW(const P &); // more stuff }; class PR : public P { PR(const P &); // more stuff }; Or did you mean something more like: class P { operator PW() const; operator PR() const; // stuff };

Related questions

0 votes
    I am working on a C++ program, which uses cURL library, which is written in plain C. When I try ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    I am writing a program that uses the XGetPixel() method. However, upon using it I get an error ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    This is the document of the function cv::fisheye::initUndistortRectifyMap I don't know what the R is. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    All compilers I could get my hands on agree that this is fine: template auto foo(Check, T...) - ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    Let's say that I declare a C struct called foo, which has an int field called bar and a char * ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I want to be able to zoom in my QtCharts using the mouse. The best way to zoom using the mouse ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 6, 2022 in Education by JackTerrance
0 votes
    I work on graphics applications and have been using shared & unique pointers essentially because it handles memory ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I have a QGraphicsItem element (subclassesed from QGraphicsItem) that has as child a QGraphicsTextItem. The problem ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    I've been trying for some time now to get a screen-space pixel (provided by a deferred HLSL ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    // merge.cpp // merges two containers into a third #include #include //for merge() using namespace std; int src1[] = { 2, 3, 4, ... , src1+5, src2, src2+3, dest); for(int j=0; j...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I'm trying to create two dimensional std::vector, that will hold objects of this class I inherited. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    According to my understanding of QDateEdit's API documentation it creates a widget for editing dates. The ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    I need to render a vector-only SVG without antialiasing in Qt; specifically, I don't want the resulting ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    According to my understanding of QDateEdit's API documentation it creates a widget for editing dates. The ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 11, 2022 in Education by JackTerrance
...