简体   繁体   中英

C++ Unable to assign base class “this” pointer the derived class object inside base class constructor

Hi I am learning C++ and was trying to construct something where base class *this pointer is assigned the derived class object using the following construction.My question is that, is it possible in C++ in this way? Because, when i try to compile it, i get the error


error: expected type-specifier before 'Derived_1'


If something like this is possible, then what is the correct way to do that. My objective to do this is to automatically get the relative derived class implementation through base class, based on the DISPLAY Type, to avoid if-else kind of clauses in main. Any help will be much appreciated.

#ifndef _MAIN_HPP_
#define _MAIN_HPP_

#include <iostream>

enum class DISPLAY {
    DERIVED_1 = 1,
    DERIVED_2 = 2
};

class Base {
    private:
        int variable = 0;
    public:
        Base(){std::cout<<"Base Class Empty Constructor"<<std::endl;}
        Base(DISPLAY Type) {
            std::cout<<"Base Class Constructor Derived_"<<static_cast<int>(Type)<<std::endl;
            switch(Type) 
            {
                case(DISPLAY::DERIVED_1): {
                    *this = new Derived_1();
                }break;
                case(DISPLAY::DERIVED_2): {
                    *this = new Derived_2();
                }break;
            }
        }
        virtual ~Base() {std::cout<<"This is Base Class Destructor"<<std::endl;}
        virtual int Sum(int x, int y) {return x+y;};
};

class Derived_1 : public Base {
    public:
        Derived_1(){std::cout<<"This is Derived_1 Class Constructor"<<std::endl;}
        virtual ~Derived_1() {std::cout<<"This is Derived_1 Class Destructor"<<std::endl;}
        int Sum(int x, int y) {return (x*2)+(y*3);}
};

class Derived_2 : public Base {
    public:
        Derived_2(){std::cout<<"This is Derived_2 Class Constructor"<<std::endl;}
        virtual ~Derived_2() {std::cout<<"This is Derived_2 Class Destructor"<<std::endl;}
        int Sum(int x, int y) {return (x*1)+(y*2);}
};
#endif

#include "main.hpp"

using namespace std;

int main(int argc, char **argv)
{
    Base *base = new Base(DISPLAY::DERIVED_1);
    cout<<"SUM:"<<base->Sum(2, 10)<<endl;
    return 0;
}

Error Summary:

/home/junaid/workspace/Learn_Cpp/main.hpp: In constructor ‘Base::Base(DISPLAY)’:
/home/junaid/workspace/Learn_Cpp/main.hpp:21:33: error: expected type-specifier before ‘Derived_1’
   21 |                     *this = new Derived_1();
      |                                 ^~~~~~~~~
/home/junaid/workspace/Learn_Cpp/main.hpp:24:33: error: expected type-specifier before ‘Derived_2’
   24 |                     *this = new Derived_2();
      |                                 ^~~~~~~~~

Build finished with error(s).
The terminal process terminated with exit code: -1.

You absolutely can't do what you're doing. You can't do this:

                *this = new Derived_1();

I don't even want to think about all the things wrong with this. What you want instead is to implement the factory pattern (which you can google for).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM