Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6240859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:38:57+00:00 2026-05-24T11:38:57+00:00

Sorry for the larger amount of the source code. There three abstract classes P,

  • 0

Sorry for the larger amount of the source code. There three abstract classes P, L, PL. The third class PL is derived from classes P and L using the virtual inheritance:

template <typename T>  //Abstract class
class P
{

    public:
            virtual ~P() = 0;
    virtual P <T> *clone() const  = 0;
};

template <typename T>
P<T>::~P() {}

template <typename T>
P<T> * P <T>:: clone() const { return new P <T> ( *this ); }

template <typename T>  //Abstract class
class L
{
    public:
            virtual ~L() = 0;
    virtual L <T> *clone() const = 0;
};

template <typename T>
L<T>::~L() {}

template <typename T>
L<T> *L <T> ::clone() const { return new L <T> ( *this );}

template <typename T>
class PL: virtual public P <T>, virtual public L <T>  //Abstract class
{
    public:
            PL() : P <T>(), L<T>() {}
    virtual ~PL() = 0;
    virtual PL <T> *clone() const  = 0;
};

template <typename T>
PL<T>::~PL() {}

template <typename T>
PL<T> * PL <T> :: clone() const { return new PL <T> ( *this );}

Every class has its own implementation of the clone method.

Two next classes PA, PC are derived from class P using the virtual inheritance:

template <typename T>
class PC : virtual public P <T>
{
    public:
            PC() : P <T> () {}
            virtual ~PC() {}
            virtual PC <T> *clone() const {return new PC <T> ( *this );}
};

template <typename T>
class PA : virtual public P <T>
{
    public:
            PA() : P <T> () {}
            virtual ~PA() {}
            virtual PA <T> *clone() const {return new PA <T> ( *this );}
};

The last two classes PCL and PAL are dirived using the virtual inheritance from PC and PL , PA and PL.

template <typename T>
class PCL : public PC <T>, public PL <T>
{
    public:
            PCL() : P <T> (), PC <T> (), PL <T> ()  {}
            virtual ~PCL() {}
            virtual PCL <T> *clone() const {return new PCL <T> ( *this );}
};

template <typename T>
class PAL : public PA <T>, public PL <T>
{
    public:
            PAL() : P <T> (), PA <T> (), PL <T> () {}
            virtual ~PAL() {}
            virtual PAL <T> *clone() const {return new PAL <T> ( *this );}

};

There is the diagram of the class dependencies:

.......... P .... L.....
........../|\..../......
........./.|.\../.......
......../..|..\/........
.......PC..PA..PL.......
.......|...|.../|.......
.......|...|../.|.......
.......|...PAL..|.......
.......|........|.......
.......PCL_____/........  

Please, do not discuss this proposal :-))). I have the following 3 questions:

1) Was this class dependency rewritten in C++ correctly (first of all the placemement of “virtual”)?

2) I am not sure what is wrong, see the code, please:

int main(int argc, _TCHAR* argv[])
{
PCL <double> * pcl = new PCL <double>(); //Object of abstract class not allowed
PAL <double> * pal = new PAL <double>(); //Object of abstract class not allowed
PL <double> *pl1 = pcl->clone(); //Polymorphism
PL <double> *pl2 = pal->clone(); //Polymorphism
return 0;
} 

It is not possible to create new objects of PAL / PCL classes, both classes are marked as abstract. But they are not abstract. Where is the problem?

3) Is it possible to use polymorphism together with clone() method? See the code above, please…

Thanks for your help…


UPDATED QUESTION

I corrected the code. But the following error using VS 2010 compiler appear:

template <typename T>  //Abstract class
class P
{
    public:
    virtual ~P() = 0;
    virtual P <T> *clone() const  = 0;
};

template <typename T>
P<T>::~P() {}

template <typename T>
P<T> * P <T>:: clone() const { return new P <T> ( *this ); }


template <typename T>  //Abstract class
class L
{
    public:
    virtual ~L() = 0;
    virtual L <T> *clone() const = 0;
};

template <typename T>
L<T>::~L() {}

template <typename T>
L<T> *L <T> ::clone() const { return new L <T> ( *this );}


template <typename T>
class PL: virtual public P <T>, virtual public L <T>  //Abstract class
{
    public:
            PL() : P <T>(), L<T>() {}
    virtual ~PL() = 0;
    virtual PL <T> *clone() const  = 0; 
};

template <typename T>
PL<T>::~PL() {}

template <typename T>
PL<T> * PL <T> :: clone() const { return new PL <T> ( *this );}


template <typename T>
class PC : virtual public P <T>
{
    protected:
            T pc;
    public:
            PC() : P <T> () {}
    virtual ~PC() {}
            virtual PC <T> *clone() const {return new PC <T> ( *this );}
};

template <typename T>
class PA : virtual public P <T>
{
    public:
            PA() : P <T> () {}
    virtual ~PA() {}
            virtual PA <T> *clone() const {return new PA <T> ( *this );}
};

template <typename T>
class PCL : public PC <T>, public PL <T>
{
public:
            PCL() : P <T> (), PC <T> (), PL <T> () {}
            virtual ~PCL() {}
            virtual PCL <T> *clone() const {return new PCL <T> ( *this   );}
}; //Error using VS 2010: Error 1   error C2250: 'PCL<T>' : ambiguous inheritance of 'PC<T> *P<T>::clone(void) const'


template <typename T>
class PAL : public PA <T>, public PL <T>
{
public:
            PAL() : P <T> (), PA <T> (), PL <T> ()  {}
            virtual ~PAL() {}
            virtual PAL <T> *clone() const {return new PAL <T> ( *this );}
}; //Error VS 2010: Error   1   error C2250: 'PAL<T>' : ambiguous inheritance of 'PA<T> *P<T>::clone(void) const'


int main(int argc, char* argv[])
{
PCL <double> * pcl = new PCL <double>();
PAL <double> * pal = new PAL <double>();
PL <double> *pl1 = pcl->clone();
PL <double> *pl2 = pal->clone();
return 0;
}

Maybe i overlooked something… But g++ compiles this code OK.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:38:58+00:00Added an answer on May 24, 2026 at 11:38 am

    This compiles for me using VC10:

    template <typename T>  //Abstract class
    class P
    {
    public:
        virtual ~P() = 0;
        virtual P <T> *clone() const  = 0;
    };
    
    template <typename T>
    P<T>::~P() {}
    
    
    template <typename T>  //Abstract class
    class L
    {
    public:
        virtual ~L() = 0;
        virtual L <T> *clone() const = 0;
    };
    
    template <typename T>
    L<T>::~L() {}
    
    
    template <typename T>
    class PL: virtual public P <T>, virtual public L <T>  //Abstract class
    {
    public:
        PL() : P <T>(), L<T>() {}
        virtual ~PL() = 0;
    //  virtual PL <T> *clone() const  = 0; // REMOVED!
    };
    
    template <typename T>
    PL<T>::~PL() {}
    
    
    template <typename T>
    class PC : virtual public P <T>
    {
    protected:
        T pc;
    public:
        PC() : P <T> () {}
        virtual ~PC() {}
        virtual PC <T> *clone() const {return new PC <T> ( *this );}
    };
    
    template <typename T>
    class PA : virtual public P <T>
    {
    public:
        PA() : P <T> () {}
        virtual ~PA() {}
        virtual PA <T> *clone() const {return new PA <T> ( *this );}
    };
    
    template <typename T>
    class PCL : public PC <T>, public PL <T>
    {
    public:
        PCL() : P <T> (), PC <T> (), PL <T> () {}
        virtual ~PCL() {}
        virtual PCL <T> *clone() const {return new PCL <T> ( *this   );}
    };
    
    
    template <typename T>
    class PAL : public PA <T>, public PL <T>
    {
    public:
        PAL() : P <T> (), PA <T> (), PL <T> ()  {}
        virtual ~PAL() {}
        virtual PAL <T> *clone() const {return new PAL <T> ( *this );}
    };
    
    
    int main()
    {
        PCL <double> * pcl = new PCL <double>();
        PAL <double> * pal = new PAL <double>();
        PL <double> *pl1 = pcl->clone();
        PL <double> *pl2 = pal->clone();
        return 0;
    }
    

    Note that I had to remove PL <T> *PL <T>::clone() to get VC to accept the code. The problem with that is that, if you have a PL<T>, and call it’s clone(), it will statically return an L<T>*, rather than a PL<T>*, even though the dynamic type is of a class derived from PL<T>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the Windows developers out there, this solution is for Macs only. This
I'm importing some data from a CSV file, and numbers that are larger than
I'm trying to send a large amount of data (more than 50 MB) using
Following on from Jeff and Joel's discussions of plugin architectures. Plugins in C++ (using
I'm working on a web application using a pretty heavy amount of javascript. On
Hello everyone :) I'm sorry for the long code listing, but I'm not sure
I am trying to display an image from a URL, which may be larger
Sorry for the basic question - I'm a .NET developer and don't have much
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry, I'm new to SVN and I looked around a little for this. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.