Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Virtual Constructors

Virtual Constructors

Scheduled Pinned Locked Moved C / C++ / MFC
question
12 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rinu_Raj
    wrote on last edited by
    #1

    Do any one know about Virtual Constructors ? I heared about it some where but am not at all clear about the fact. If any one knows it please say abt the same .. Rinu Raj:-)

    S L N D 4 Replies Last reply
    0
    • R Rinu_Raj

      Do any one know about Virtual Constructors ? I heared about it some where but am not at all clear about the fact. If any one knows it please say abt the same .. Rinu Raj:-)

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      C++ does not allow constructors to be virtual. The term "Virtual Constructors" refers to virtual methods of a factory classes that creates other classes. An example: // Assume we have many implementations of a classes derived from CWorkerBase. // They are named CWorkerBase_CompanyName.   class CFactoryBase { public:     virtual CWorkerBase* Create() = 0; }; class CFactory_Microsoft : public CFactoryBase { public:     virtual CWorkerBase* Create()     {        return new CWorkerBase_Mircrosoft();     } }; class CFactory_Borland : public CFactoryBase { public:     virtual CWorkerBase* Create()     {        return new CWorkerBase_Borland();     } }; In this context the Create methods are sometimes called virtual constructors. Steve

      S R E 3 Replies Last reply
      0
      • R Rinu_Raj

        Do any one know about Virtual Constructors ? I heared about it some where but am not at all clear about the fact. If any one knows it please say abt the same .. Rinu Raj:-)

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Did you hear it as one of the question when being interviewed? S o h a i l K a d i w a l a

        N 1 Reply Last reply
        0
        • R Rinu_Raj

          Do any one know about Virtual Constructors ? I heared about it some where but am not at all clear about the fact. If any one knows it please say abt the same .. Rinu Raj:-)

          N Offline
          N Offline
          Nishad S
          wrote on last edited by
          #4

          I tried to compile the below code get the error...

          class a
          {
          virtual a()
          {
          }
          };

          error C2633: 'a' : 'inline' is the only legal storage class for constructors - NS -

          D 1 Reply Last reply
          0
          • L Lost User

            Did you hear it as one of the question when being interviewed? S o h a i l K a d i w a l a

            N Offline
            N Offline
            Nishad S
            wrote on last edited by
            #5

            Nice question :) - NS -

            1 Reply Last reply
            0
            • S Stephen Hewitt

              C++ does not allow constructors to be virtual. The term "Virtual Constructors" refers to virtual methods of a factory classes that creates other classes. An example: // Assume we have many implementations of a classes derived from CWorkerBase. // They are named CWorkerBase_CompanyName.   class CFactoryBase { public:     virtual CWorkerBase* Create() = 0; }; class CFactory_Microsoft : public CFactoryBase { public:     virtual CWorkerBase* Create()     {        return new CWorkerBase_Mircrosoft();     } }; class CFactory_Borland : public CFactoryBase { public:     virtual CWorkerBase* Create()     {        return new CWorkerBase_Borland();     } }; In this context the Create methods are sometimes called virtual constructors. Steve

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              Ironic - The one correct answer gets a 1 vote. Well don't take my word for it: see "The C++ Programming Language", Special Edition by Bjarne Stroustrup, Section 12.4.4 on page 323. Steve

              R 1 Reply Last reply
              0
              • S Stephen Hewitt

                C++ does not allow constructors to be virtual. The term "Virtual Constructors" refers to virtual methods of a factory classes that creates other classes. An example: // Assume we have many implementations of a classes derived from CWorkerBase. // They are named CWorkerBase_CompanyName.   class CFactoryBase { public:     virtual CWorkerBase* Create() = 0; }; class CFactory_Microsoft : public CFactoryBase { public:     virtual CWorkerBase* Create()     {        return new CWorkerBase_Mircrosoft();     } }; class CFactory_Borland : public CFactoryBase { public:     virtual CWorkerBase* Create()     {        return new CWorkerBase_Borland();     } }; In this context the Create methods are sometimes called virtual constructors. Steve

                R Offline
                R Offline
                Rinu_Raj
                wrote on last edited by
                #7

                Thank u this is the answer i was looking for

                1 Reply Last reply
                0
                • S Stephen Hewitt

                  Ironic - The one correct answer gets a 1 vote. Well don't take my word for it: see "The C++ Programming Language", Special Edition by Bjarne Stroustrup, Section 12.4.4 on page 323. Steve

                  R Offline
                  R Offline
                  Roger Stoltz
                  wrote on last edited by
                  #8

                  You are SO right Stephen, in both of your posts. Virtual Constructors is a design pattern AKA "Factory Method" that also can be read about in the famous book "Design Patterns" written by GoF (Gamma, Helm, Johnson and Vlissides), which has been the "design pattern bible" for more than 10 years. As an example: every time you create a COM object you use a factory AKA Virtual Constructor. Common knowledge? :~ Strange votings again, reminds me of my post two weeks ago in The Lounge[^] I'll put it up as much as I can... -- Rog


                  It's suppose to be hard, otherwise anybody could do it!

                  M 1 Reply Last reply
                  0
                  • R Rinu_Raj

                    Do any one know about Virtual Constructors ? I heared about it some where but am not at all clear about the fact. If any one knows it please say abt the same .. Rinu Raj:-)

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Rinu_Raj wrote:

                    Do any one know about Virtual Constructors ?

                    See here.


                    "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                    "There is no death, only a change of worlds." - Native American Proverb

                    1 Reply Last reply
                    0
                    • N Nishad S

                      I tried to compile the below code get the error...

                      class a
                      {
                      virtual a()
                      {
                      }
                      };

                      error C2633: 'a' : 'inline' is the only legal storage class for constructors - NS -

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      NS17 wrote:

                      error C2633: 'a' : 'inline' is the only legal storage class for constructors

                      Because the constructor cannot be virtual.


                      "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                      "There is no death, only a change of worlds." - Native American Proverb

                      1 Reply Last reply
                      0
                      • S Stephen Hewitt

                        C++ does not allow constructors to be virtual. The term "Virtual Constructors" refers to virtual methods of a factory classes that creates other classes. An example: // Assume we have many implementations of a classes derived from CWorkerBase. // They are named CWorkerBase_CompanyName.   class CFactoryBase { public:     virtual CWorkerBase* Create() = 0; }; class CFactory_Microsoft : public CFactoryBase { public:     virtual CWorkerBase* Create()     {        return new CWorkerBase_Mircrosoft();     } }; class CFactory_Borland : public CFactoryBase { public:     virtual CWorkerBase* Create()     {        return new CWorkerBase_Borland();     } }; In this context the Create methods are sometimes called virtual constructors. Steve

                        E Offline
                        E Offline
                        Eytukan
                        wrote on last edited by
                        #11

                        I think the voter'd have wanted to give a top '1' vote. just like Rank 1. :-D


                        --[V]--

                        [My Current Status]

                        1 Reply Last reply
                        0
                        • R Roger Stoltz

                          You are SO right Stephen, in both of your posts. Virtual Constructors is a design pattern AKA "Factory Method" that also can be read about in the famous book "Design Patterns" written by GoF (Gamma, Helm, Johnson and Vlissides), which has been the "design pattern bible" for more than 10 years. As an example: every time you create a COM object you use a factory AKA Virtual Constructor. Common knowledge? :~ Strange votings again, reminds me of my post two weeks ago in The Lounge[^] I'll put it up as much as I can... -- Rog


                          It's suppose to be hard, otherwise anybody could do it!

                          M Offline
                          M Offline
                          Maxwell Chen
                          wrote on last edited by
                          #12

                          Roger Stoltz wrote:

                          Strange votings again, reminds me of my post two weeks ago in The Lounge[^]

                          :confused::confused::confused: Which post? The link you provided is changing all the time. Nothing among the posts is expanded (selected). It shifts the frame window (Msgs [N] ~ [N+49]) each time when someone post a new post.


                          Maxwell Chen

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups