Like Tree1Likes
  • 1 Post By anon

Copy Constructor + Polymorphism

This is a discussion on Copy Constructor + Polymorphism within the C++ Programming forums, part of the General Programming Boards category; I have a class, page_directory, that implements a base class, address_space. I need to be able to clone an address_space& ...

  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587

    Copy Constructor + Polymorphism

    I have a class, page_directory, that implements a base class, address_space. I need to be able to clone an address_space& that is really a to a page_directory&. The problem is the page_directory specific data that needs to be copied.

    They aren't functional yet, but you can get the idea from what I have finished.
    Code:
    /**
     * x86_32 page directory implementation
     */
    class page_directory : public address_space
    {
        public:
        page_directory();
        page_directory(const page_directory&);
        
        void map(virtual_address, physical_address, bool writeable = true);
        void unmap(virtual_address);
        
        private:
        page_directory_entry _page_directory[0x400];
    };
    
    /**
     * Generic address space
     */
    class address_space
    {
        public:
        address_space();
        address_space(const address_space&);
        
        virtual void map(virtual_address, physical_address, bool writeable = true) = 0;
        virtual void unmap(virtual_address) = 0;
    };
    Any ideas?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You'll probably need to implement a virtual clone method for each derived class (and perhaps make the copy constructor private since it might make accidental slicing too easy).

    Code:
    virtual address_space* clone() const { return new address_space(*this); }
    
    virtual page_directory* clone() const { return new page_directory(*this); }
    This way polymorphism selects the correct object to create. The return type can be covariant, so you don't need downcasts if you want to store the result in a pointer to correct derived class.
    Last edited by anon; 04-09-2011 at 08:29 AM.
    User Name: likes this.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Yeah, I was coming to the realization that that was the only way. It's kinda ugly, but I'm starting to come to terms with the fact that Godel didn't just f*** up math, he f***ed up everything.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

    Is there anything in C++0x that may provide an alternative?
    Last edited by User Name:; 04-09-2011 at 08:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help fast
    By blackice in forum C Programming
    Replies: 2
    Last Post: 01-31-2011, 01:19 PM
  2. Copy Constructors and Assignment Operator overloading.
    By leeor_net in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2009, 10:26 PM
  3. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 02:54 PM
  4. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-22-2005, 12:55 AM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21