C++ Inherited functions returning self reference with derived type

I am trying to create a system where one base class has a bunch of functions, then derived classes can inherit it and add more functions. However, all of these functions return the this keyword and I need the return type in derived classes to be a pointer of their own class, not of the base class. What might be the best way of implementing this?

Preferably, the system would work such that functions can be called on a variable one after another in a single line. For example, testObject->SetPositionX(5)->SetPositionY(12)->SetRGB(24, 80, 50); or even testObject->AddChild((new Foo)->SetBorderWidth(5)->SetBorderStyle(BorderStyle::dotted)); The issue, however, is that if a function is called that’s defined in the base class instead of the derived class, it will return a pointer of the base class type and remove access to all of the derived class functions.

As a full demonstration,

#include <iostream>

class Base {
    public:
        Base* CommonFunc() {
            std::cout << "Common function part of Base and all derived classes" << std::endl;
            return this;
        }
};

class Foo: public Base {
    public:
        Foo* UniqueFunc() {
            std::cout << "Unique function of the Foo class" << std::endl;
            return this;
        }
};

int main() {
    Foo* exampleFoo = new Foo();
    exampleFoo->UniqueFunc()->UniqueFunc()->CommonFunc()/*->UniqueFunc()*/; // This line doesn't compile with the last function uncommented because CommonFunc() returns a Base* instead of a Foo* and UniqueFunc() is not part of the Base class.

    return 0;
}

/* Output:
Unique function of the Foo class
Unique function of the Foo class
Common function part of Base and all derived classes
[Can't call unique function again or else compile error] */

My solution so far has been to make the base functions virtual and rewrite their definition in every derived class. Not a large rewrite, they execute the same code by simply calling the inherited class in a single line like this Foo* Foo::CommonFunc() {Base::CommonFunc(); return this;}. In the full demonstration, something like this:

#include <iostream>

class Base {
    public:
        virtual Base* CommonFunc() {
            std::cout << "Common function part of Base and all derived classes" << std::endl;
            return this;
        }
};

class Foo: public Base {
    public:
        // Redefine inherited function with the proper return type
        Foo* CommonFunc() {
            std::cout << "Executed from Foo: ";
            Base::CommonFunc();
            return this;
        }
    
        Foo* UniqueFunc() {
            std::cout << "Unique function of the Foo class" << std::endl;
            return this;
        }
};

int main() {
    Foo* exampleFoo = new Foo();
    exampleFoo->UniqueFunc()->UniqueFunc()->CommonFunc()->UniqueFunc(); // This full line now works because Foo::CommonFunc() is redefined to return a Foo* instead of a Base*

    return 0;
}

/* Output:
Unique function of the Foo class
Unique function of the Foo class
Executed from Foo: Common function part of Base and all derived classes
Unique function of the Foo class */

This solution works and does fix the issue, but my project has grown such that the base class has 20 of these self returning functions, and there are 15 derived classes that each need to reroute those functions. If a new function is added to the base class, I need to add new code to every derived class which, while not too terrible, simply defeats the purpose of inheritance. If anyone can help think of a new solution, it would be greatly appreciated!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật