Thread: Simulation of virtual Constructor!

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    40

    Simulation of virtual Constructor!

    Hi Guys,

    I have a quick doubt on virtual contructor!

    Though, We cannot write virtual constructor in C++. But I read it we can achieve the effect of virtual contructor in C++ deisgn pattern called "Factory Design pattern".

    Can somebody explain me with a nice and easy example of virtual contructor with factory design pattern?

    Your help will be highly appreciated!

    Thanks,
    Vaibhav

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Code:
    #include <iostream>
    class foo
    {
      public:
      foo()
      {
        std::cout << "foo";  
      }
    };
    
    class bar : public foo
    {
      public:
      bar():
        foo()
      {
        std::cout<<"bar";
      }
    };
    
    class factory
    {
        public:
           virtual foo& make()
          {
            return *new foo();
          }
    };
    class barFactory : public factory
    {
      public:
           virtual bar& make()
          {
            return *new bar();
          }
    };
    i suppose you could do something like the above. i don't see the point though.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    40
    How would we call the same from main?
    Please elaborate

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    m37h0d's example is a little unconventional in that it returns a reference to a dynamically allocated object. A canonical example of a "virtual constructor" would be with a virtual clone() function, allowing one to clone an object of the derived class type without knowing the actual derived class type (i.e., only the base class type is known).

    Quote Originally Posted by vaibhavs17
    How would we call the same from main?
    What would your attempt be?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    40
    Thanks Laser Light!
    Could you please explain with an example?
    Thanks
    Vaibhav

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by vaibhavs17
    Could you please explain with an example?
    As you wish:
    Code:
    #include <ostream>
    #include <memory>
    #include <iostream>
    
    class X
    {
    public:
        virtual ~X() {}
    
        virtual X* clone() const = 0;
    
        virtual void identify(std::ostream& out) const = 0;
    };
    
    class Y : public X
    {
    public:
        explicit Y(int n) : n(n) {}
    
        virtual Y* clone() const
        {
            return new Y(*this);
        }
    
        virtual void identify(std::ostream& out) const
        {
            out << "I am of type Y with n = " << n << std::endl;
        }
    private:
        int n;
    };
    
    class Z : public X
    {
    public:
        explicit Z(int n) : n(n + n) {}
    
        virtual Z* clone() const
        {
            return new Z(*this);
        }
    
        virtual void identify(std::ostream& out) const
        {
            out << "I am of type Z with n = " << n << std::endl;
        }
    private:
        int n;
    };
    
    X* foo(int n)
    {
        if (n % 2 == 0)
        {
            return new Y(n);
        }
        else
        {
            return new Z(n);
        }
    }
    
    int main(int argc, char* argv[])
    {
        std::auto_ptr<X> p1(foo(argc));
        std::auto_ptr<X> p2(p1->clone());
        p2->identify(std::cout);
    }
    Notice that p2 in the global main function points to a copy of the object pointed to by p1, but all we know is that the type of object that p1 points to is a subtype of X.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Virtual base class constructor
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2008, 02:18 AM
  3. virtual base class constructor
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 03-24-2008, 12:43 AM
  4. Virtual Base Class & Constructor :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 03:14 PM
  5. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM