Virtual constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • plmanikandan@gmail.com

    Virtual constructor

    Hi,
    I have doubts reg virtual constructor
    what is virtual constructor?
    Is c++ supports virtual constructor?
    Can anybody explain me about virtual constructor?


    Regards,
    Mani

  • Phlip

    #2
    Re: Virtual constructor

    plmanikandan wrote:
    [color=blue]
    > Can anybody explain me about virtual constructor?[/color]

    Raw C++ cannot call a constructor virtually (because a constructor is not
    really a function). There's probably some common design pattern.

    How about you use Google to survey the topic and report back here?

    --
    Phlip
    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


    Comment

    • al pacino

      #3
      Re: Virtual constructor

      hii,
      there is no such thing as virtual constructor in c++, it doesn't make
      sense.
      a constructor IS a function(can be defined explicitly , compiler calls
      it when an object of a
      class is CREATED.)

      A virtual destructor is required to resolve the ambiguity,when a
      derived class object
      is deleted via a base class pointer(a 'derived class' IS A 'base
      class') since compiler
      has to decide what to delete derived object or base object.
      thus declareing the destructor virtual resolves the ambugity.

      concept of virtual constructor doesnt' make sense to me.
      please correct me if nething is out of place here.
      thanks

      Comment

      • Alf P. Steinbach

        #4
        Re: Virtual constructor

        * plmanikandan@gm ail.com:[color=blue]
        >
        > I have doubts reg virtual constructor[/color]

        Note: "doubt" means you are unsure whether something is true or not, or
        whether a person is truthful or reliable, or not.

        Your usage above is meaningless and probably due to a bad dictionary.

        [color=blue]
        > what is virtual constructor?[/color]

        See the FAQ. Use the FAQ's Google search (it's on the main page of the
        FAQ) to find the phrase "virtual constructor" in the FAQ. Apart from
        that meaning, which has only a peripheral connection to the words
        "virtual" and "constructo r" (it's more like a random phrase that for
        some historical reason has acquired a given meaning), the FAQ also
        discusses another more natural meaning; search the FAQ for the phrase
        "Dynamic Binding During Initialization" to find that discussion.

        [color=blue]
        > Is c++ supports virtual constructor?[/color]

        Yes.

        [color=blue]
        > Can anybody explain me about virtual constructor?[/color]

        Yes, see the FAQ.

        Hth.,

        - Alf

        --
        A: Because it messes up the order in which people normally read text.
        Q: Why is it such a bad thing?
        A: Top-posting.
        Q: What is the most annoying thing on usenet and in e-mail?

        Comment

        • Sunil Varma

          #5
          Re: Virtual constructor


          plmanikandan@gm ail.com wrote:[color=blue]
          > Hi,
          > I have doubts reg virtual constructor
          > what is virtual constructor?
          > Is c++ supports virtual constructor?
          > Can anybody explain me about virtual constructor?
          >
          >
          > Regards,
          > Mani[/color]

          When you make a function virtual that function is binded after the
          object has been created.
          But if you take a constructor it is called at the time of creation of
          the object.
          Constructor cannot be binded after the object is created.

          Comment

          • al pacino

            #6
            Re: Virtual constructor

            dude how can the constructors can be virtual ??
            here is the reference frm bjarne stroustrup's tech. faq:


            Alf P. Steinbach wrote:[color=blue]
            > * plmanikandan@gm ail.com:[color=green]
            > >
            > > I have doubts reg virtual constructor[/color]
            >
            > Note: "doubt" means you are unsure whether something is true or not, or
            > whether a person is truthful or reliable, or not.
            >
            > Your usage above is meaningless and probably due to a bad dictionary.
            >
            >[color=green]
            > > what is virtual constructor?[/color]
            >
            > See the FAQ. Use the FAQ's Google search (it's on the main page of the
            > FAQ) to find the phrase "virtual constructor" in the FAQ. Apart from
            > that meaning, which has only a peripheral connection to the words
            > "virtual" and "constructo r" (it's more like a random phrase that for
            > some historical reason has acquired a given meaning), the FAQ also
            > discusses another more natural meaning; search the FAQ for the phrase
            > "Dynamic Binding During Initialization" to find that discussion.
            >
            >[color=green]
            > > Is c++ supports virtual constructor?[/color]
            >
            > Yes.
            >
            >[color=green]
            > > Can anybody explain me about virtual constructor?[/color]
            >
            > Yes, see the FAQ.
            >
            > Hth.,
            >
            > - Alf
            >
            > --
            > A: Because it messes up the order in which people normally read text.
            > Q: Why is it such a bad thing?
            > A: Top-posting.
            > Q: What is the most annoying thing on usenet and in e-mail?[/color]

            Comment

            • Alf P. Steinbach

              #7
              Re: Virtual constructor

              * al pacino:[color=blue]
              > [top-posting]
              > [quoting extranous material]
              > [quoting signature][/color]

              Please don't top-post. Please don't quote irrelevant stuff. See the
              FAQ and Shiva's monthly "Welcome"-posting in this group.

              --
              A: Because it messes up the order in which people normally read text.
              Q: Why is it such a bad thing?
              A: Top-posting.
              Q: What is the most annoying thing on usenet and in e-mail?

              Comment

              • Jim Langston

                #8
                Re: Virtual constructor

                "Alf P. Steinbach" <alfps@start.no > wrote in message
                news:47n6cqFgae dvU1@individual .net...[color=blue]
                >* plmanikandan@gm ail.com:[color=green]
                >>
                >> I have doubts reg virtual constructor[/color]
                >
                > Note: "doubt" means you are unsure whether something is true or not, or
                > whether a person is truthful or reliable, or not.
                >
                > Your usage above is meaningless and probably due to a bad dictionary.[/color]

                Actually, it's probably because he's Norweigian and English is not his
                primary language.
                [color=blue][color=green]
                >> what is virtual constructor?[/color]
                >
                > See the FAQ. Use the FAQ's Google search (it's on the main page of the
                > FAQ) to find the phrase "virtual constructor" in the FAQ. Apart from that
                > meaning, which has only a peripheral connection to the words "virtual" and
                > "constructo r" (it's more like a random phrase that for some historical
                > reason has acquired a given meaning), the FAQ also discusses another more
                > natural meaning; search the FAQ for the phrase "Dynamic Binding During
                > Initialization" to find that discussion.
                >[color=green]
                >> Is c++ supports virtual constructor?[/color]
                >
                > Yes.[/color]

                No. From the FAQ 20.8 in part:

                An idiom that allows you to do something that C++ doesn't directly support.
                [color=blue][color=green]
                >> Can anybody explain me about virtual constructor?[/color]
                >
                > Yes, see the FAQ.[/color]

                Yes, see the FAQ 20.8 You can't have a virtual constructor, but you can
                have something that mimics what a virtual constructor would do.

                Again, FAQ 20.8 in a bigger part:
                You can get the effect of a virtual constructor by a virtual clone() member
                function (for copy constructing), or a virtual create() member function (for
                the default constructor).

                Here is the link to the FAQ 20.8:



                Comment

                • Kai-Uwe Bux

                  #9
                  Re: Virtual constructor

                  al pacino wrote:
                  [color=blue]
                  > dude how can the constructors can be virtual ??[/color]

                  Easy. What is commonly called a "virtual constructor" just happens not to be
                  a constructor (like the Holy Roman Empire was neither roman nor holy and it
                  is debatable whether it was an empire).
                  [color=blue]
                  > here is the reference frm bjarne stroustrup's tech. faq:
                  > http://public.research.att.com/~bs/b...l#virtual-ctor[/color]

                  Did you read what he wrote there? I quote from *that* page:

                  Techniques for using an indirection when you ask to create an object
                  are often referred to as "Virtual constructors". For example, see
                  TC++PL3 15.6.2.
                  [color=blue]
                  > Alf P. Steinbach wrote:[color=green]
                  >> * plmanikandan@gm ail.com:[/color][/color]
                  [snip]

                  And please, don't top-post.


                  Best

                  Kai-Uwe Bux

                  Comment

                  • Alf P. Steinbach

                    #10
                    Re: Virtual constructor

                    * Jim Langston:[color=blue]
                    > "Alf P. Steinbach" <alfps@start.no > wrote in message
                    > news:47n6cqFgae dvU1@individual .net...[color=green]
                    >> * plmanikandan@gm ail.com:[color=darkred]
                    >>> I have doubts reg virtual constructor[/color]
                    >> Note: "doubt" means you are unsure whether something is true or not, or
                    >> whether a person is truthful or reliable, or not.
                    >>
                    >> Your usage above is meaningless and probably due to a bad dictionary.[/color]
                    >
                    > Actually, it's probably because he's Norweigian and English is not his
                    > primary language.
                    >[color=green][color=darkred]
                    >>> what is virtual constructor?[/color]
                    >> See the FAQ. Use the FAQ's Google search (it's on the main page of the
                    >> FAQ) to find the phrase "virtual constructor" in the FAQ. Apart from that
                    >> meaning, which has only a peripheral connection to the words "virtual" and
                    >> "constructo r" (it's more like a random phrase that for some historical
                    >> reason has acquired a given meaning), the FAQ also discusses another more
                    >> natural meaning; search the FAQ for the phrase "Dynamic Binding During
                    >> Initialization" to find that discussion.
                    >>[color=darkred]
                    >>> Is c++ supports virtual constructor?[/color]
                    >> Yes.[/color]
                    >
                    > No. From the FAQ 20.8 in part:
                    >
                    > An idiom that allows you to do something that C++ doesn't directly support.
                    >[color=green][color=darkred]
                    >>> Can anybody explain me about virtual constructor?[/color]
                    >> Yes, see the FAQ.[/color]
                    >
                    > Yes, see the FAQ 20.8 You can't have a virtual constructor, but you can
                    > have something that mimics what a virtual constructor would do.
                    >
                    > Again, FAQ 20.8 in a bigger part:
                    > You can get the effect of a virtual constructor by a virtual clone() member
                    > function (for copy constructing), or a virtual create() member function (for
                    > the default constructor).
                    >
                    > Here is the link to the FAQ 20.8:
                    > http://www.parashift.com/c++-faq-lit....html#faq-20.8[/color]

                    I'm not sure what point (if any) you were trying to make? Confuse the
                    newbie, and remove his or her learning experience in searching the FAQ?
                    Look how smart I am, I managed to find it in the FAQ?

                    Anyway, your unqualified "no" above is incorrect. C++ does indeed
                    support all that's needed to implement what's known as a virtual
                    constructor, including virtual member functions and function result
                    covariance. Note that this does not contradict the FAQ's statement.

                    Hth.,

                    - Alf

                    --
                    A: Because it messes up the order in which people normally read text.
                    Q: Why is it such a bad thing?
                    A: Top-posting.
                    Q: What is the most annoying thing on usenet and in e-mail?

                    Comment

                    • Jim Langston

                      #11
                      Re: Virtual constructor

                      "Alf P. Steinbach" <alfps@start.no > wrote in message
                      news:47nd9lFgi4 hhU1@individual .net...[color=blue]
                      >* Jim Langston:[color=green]
                      >> "Alf P. Steinbach" <alfps@start.no > wrote in message
                      >> news:47n6cqFgae dvU1@individual .net...[color=darkred]
                      >>> * plmanikandan@gm ail.com:
                      >>>> I have doubts reg virtual constructor
                      >>> Note: "doubt" means you are unsure whether something is true or not, or
                      >>> whether a person is truthful or reliable, or not.
                      >>>
                      >>> Your usage above is meaningless and probably due to a bad dictionary.[/color]
                      >>
                      >> Actually, it's probably because he's Norweigian and English is not his
                      >> primary language.
                      >>[color=darkred]
                      >>>> what is virtual constructor?
                      >>> See the FAQ. Use the FAQ's Google search (it's on the main page of the
                      >>> FAQ) to find the phrase "virtual constructor" in the FAQ. Apart from
                      >>> that meaning, which has only a peripheral connection to the words
                      >>> "virtual" and "constructo r" (it's more like a random phrase that for
                      >>> some historical reason has acquired a given meaning), the FAQ also
                      >>> discusses another more natural meaning; search the FAQ for the phrase
                      >>> "Dynamic Binding During Initialization" to find that discussion.
                      >>>
                      >>>> Is c++ supports virtual constructor?
                      >>> Yes.[/color]
                      >>
                      >> No. From the FAQ 20.8 in part:
                      >>
                      >> An idiom that allows you to do something that C++ doesn't directly
                      >> support.
                      >>[color=darkred]
                      >>>> Can anybody explain me about virtual constructor?
                      >>> Yes, see the FAQ.[/color]
                      >>
                      >> Yes, see the FAQ 20.8 You can't have a virtual constructor, but you can
                      >> have something that mimics what a virtual constructor would do.
                      >>
                      >> Again, FAQ 20.8 in a bigger part:
                      >> You can get the effect of a virtual constructor by a virtual clone()
                      >> member function (for copy constructing), or a virtual create() member
                      >> function (for the default constructor).
                      >>
                      >> Here is the link to the FAQ 20.8:
                      >> http://www.parashift.com/c++-faq-lit....html#faq-20.8[/color]
                      >
                      > I'm not sure what point (if any) you were trying to make? Confuse the
                      > newbie, and remove his or her learning experience in searching the FAQ?
                      > Look how smart I am, I managed to find it in the FAQ?
                      >
                      > Anyway, your unqualified "no" above is incorrect. C++ does indeed support
                      > all that's needed to implement what's known as a virtual constructor,
                      > including virtual member functions and function result covariance. Note
                      > that this does not contradict the FAQ's statement.[/color]

                      I read the OP's question as: I do not know if C++ would allow a virtual
                      constructor, but if it did, what would be it's purpose? What would making a
                      constructor virtual accomplish?

                      Which is why the two part answer. C++ directly does not support a virtual
                      constructor. Which is an answer of No. But, you could do what a virtual
                      constructor would supposedly do by using a virtual clone() or create()
                      member function which is what people think a virtual constructor would do if
                      C++ in fact supported it.

                      As far as learning experience in searching in the FAQ, I'm sure that 95% of
                      the questions asked in this newsgroup could be answered by STFW, if not 100%
                      of them.


                      Comment

                      • al pacino

                        #12
                        Re: Virtual constructor

                        hi alf ,

                        of course you can simulate 'virtual ' constructor, but there is no
                        'direct' support(from compiler) like it is for virtual destructor.

                        Comment

                        • Alf P. Steinbach

                          #13
                          Re: Virtual constructor

                          * Jim Langston:[color=blue]
                          > "Alf P. Steinbach" <alfps@start.no > wrote in message
                          > news:47nd9lFgi4 hhU1@individual .net...[color=green]
                          >> * Jim Langston:[color=darkred]
                          >>> "Alf P. Steinbach" <alfps@start.no > wrote in message
                          >>> news:47n6cqFgae dvU1@individual .net...
                          >>>> * plmanikandan@gm ail.com:
                          >>>>> I have doubts reg virtual constructor
                          >>>> Note: "doubt" means you are unsure whether something is true or not, or
                          >>>> whether a person is truthful or reliable, or not.
                          >>>>
                          >>>> Your usage above is meaningless and probably due to a bad dictionary.
                          >>> Actually, it's probably because he's Norweigian and English is not his
                          >>> primary language.
                          >>>
                          >>>>> what is virtual constructor?
                          >>>> See the FAQ. Use the FAQ's Google search (it's on the main page of the
                          >>>> FAQ) to find the phrase "virtual constructor" in the FAQ. Apart from
                          >>>> that meaning, which has only a peripheral connection to the words
                          >>>> "virtual" and "constructo r" (it's more like a random phrase that for
                          >>>> some historical reason has acquired a given meaning), the FAQ also
                          >>>> discusses another more natural meaning; search the FAQ for the phrase
                          >>>> "Dynamic Binding During Initialization" to find that discussion.
                          >>>>
                          >>>>> Is c++ supports virtual constructor?
                          >>>> Yes.
                          >>> No. From the FAQ 20.8 in part:
                          >>>
                          >>> An idiom that allows you to do something that C++ doesn't directly
                          >>> support.
                          >>>
                          >>>>> Can anybody explain me about virtual constructor?
                          >>>> Yes, see the FAQ.
                          >>> Yes, see the FAQ 20.8 You can't have a virtual constructor, but you can
                          >>> have something that mimics what a virtual constructor would do.
                          >>>
                          >>> Again, FAQ 20.8 in a bigger part:
                          >>> You can get the effect of a virtual constructor by a virtual clone()
                          >>> member function (for copy constructing), or a virtual create() member
                          >>> function (for the default constructor).
                          >>>
                          >>> Here is the link to the FAQ 20.8:
                          >>> http://www.parashift.com/c++-faq-lit....html#faq-20.8[/color]
                          >> I'm not sure what point (if any) you were trying to make? Confuse the
                          >> newbie, and remove his or her learning experience in searching the FAQ?
                          >> Look how smart I am, I managed to find it in the FAQ?
                          >>
                          >> Anyway, your unqualified "no" above is incorrect. C++ does indeed support
                          >> all that's needed to implement what's known as a virtual constructor,
                          >> including virtual member functions and function result covariance. Note
                          >> that this does not contradict the FAQ's statement.[/color]
                          >
                          > I read the OP's question as: I do not know if C++ would allow a virtual
                          > constructor, but if it did, what would be it's purpose? What would making a
                          > constructor virtual accomplish?[/color]

                          Don't inform me. Instead, bring your interpretation with your response,
                          if it has any bearing on the response. Anyway, I think it's a Good Idea
                          to assume a question is meaningful, like the established meaning for the
                          actual words used by the OP, rather than something meaningless.

                          [color=blue]
                          > Which is why the two part answer. C++ directly does not support a virtual
                          > constructor. Which is an answer of No. But, you could do what a virtual
                          > constructor would supposedly do by using a virtual clone() or create()
                          > member function which is what people think a virtual constructor would do if
                          > C++ in fact supported it.[/color]

                          Also here. There are several possible meanings for "virtual
                          constructor" apart from the established meaning, most notably the one of
                          doing virtual construction (where part of the construction is specified
                          by a derived class; directly supported in Java and C# by breaking those
                          languages' type safety, and possible to simulate in C++, see the FAQ).
                          But yours isn't one: a constructor can't do the job of a clone function.

                          [color=blue]
                          > As far as learning experience in searching in the FAQ, I'm sure that 95% of
                          > the questions asked in this newsgroup could be answered by STFW, if not 100%
                          > of them.[/color]

                          Does that mean "Searching The Fucking Web"? Language, Jim. Also,
                          that's highly misleading: nobody tried to send anyone on a wild-goose
                          chase, as you imply, but instead the OP was directed on a highly focused
                          search guaranteed to yield a useful result.

                          But you then stepped in with a totally unncessary "clear up things"
                          posting which in addition to depriving the OP the chance to learn to
                          something, was factually incorrect, and as you now clarify, relied on a
                          meaningless interpretation of the question rather than assuming some
                          meaning in there.


                          --
                          A: Because it messes up the order in which people normally read text.
                          Q: Why is it such a bad thing?
                          A: Top-posting.
                          Q: What is the most annoying thing on usenet and in e-mail?

                          Comment

                          • Alf P. Steinbach

                            #14
                            Re: Virtual constructor

                            * al pacino:[color=blue]
                            > hi alf ,
                            >
                            > of course you can simulate 'virtual ' constructor, but there is no
                            > 'direct' support(from compiler) like it is for virtual destructor.[/color]

                            It /is/ possible to quote using Google groups, but I think you have to
                            press some button or some such (please check that out).

                            Anyway, there's no need to "simulate" a "virtual" constructor.

                            The phrase "virtual constructor" has an established meaning, and can be
                            expressed directly in C++ without any simulation at all; see the FAQ.

                            --
                            A: Because it messes up the order in which people normally read text.
                            Q: Why is it such a bad thing?
                            A: Top-posting.
                            Q: What is the most annoying thing on usenet and in e-mail?

                            Comment

                            • Rolf Magnus

                              #15
                              Re: Virtual constructor

                              Alf P. Steinbach wrote:
                              [color=blue][color=green]
                              >> I read the OP's question as: I do not know if C++ would allow a virtual
                              >> constructor, but if it did, what would be it's purpose? What would
                              >> making a constructor virtual accomplish?[/color]
                              >
                              > Don't inform me. Instead, bring your interpretation with your response,
                              > if it has any bearing on the response. Anyway, I think it's a Good Idea
                              > to assume a question is meaningful, like the established meaning for the
                              > actual words used by the OP, rather than something meaningless.
                              >
                              >[color=green]
                              >> Which is why the two part answer. C++ directly does not support a
                              >> virtual constructor. Which is an answer of No. But, you could do what a
                              >> virtual constructor would supposedly do by using a virtual clone() or
                              >> create() member function which is what people think a virtual constructor
                              >> would do if C++ in fact supported it.[/color]
                              >
                              > Also here. There are several possible meanings for "virtual constructor"
                              > apart from the established meaning,[/color]

                              Those other meanings are based on incorrect interpretations of the word
                              "virtual". The GoF book calls it "factory method", which is a much better
                              name for it.
                              [color=blue]
                              > most notably the one of doing virtual construction (where part of the
                              > construction is specified by a derived class; directly supported in Java
                              > and C# by breaking those languages' type safety, and possible to simulate
                              > in C++, see the FAQ).
                              > But yours isn't one: a constructor can't do the job of a clone function.[/color]

                              A constructor can't do the job of a "virtual constructor" either. What the
                              FAQ mentions is not a constructor or something that does the job of a
                              constructor. The clone() function would even be closer to something like a
                              virtual constructor than a factory, because it can at least take the
                              dynamic type of an existing object, as virtual functions do. A factory
                              needs some other means to decide with class to instantiate.
                              [color=blue][color=green]
                              >> As far as learning experience in searching in the FAQ, I'm sure that 95%
                              >> of the questions asked in this newsgroup could be answered by STFW, if
                              >> not 100% of them.[/color]
                              >
                              > Does that mean "Searching The Fucking Web"? Language, Jim.[/color]

                              You were the one who used the word.
                              [color=blue]
                              > Also, that's highly misleading: nobody tried to send anyone on a
                              > wild-goose chase, as you imply, but instead the OP was directed on a
                              > highly focused search guaranteed to yield a useful result.
                              >
                              > But you then stepped in with a totally unncessary "clear up things"
                              > posting[/color]

                              Clearing things up is not unnecessary, especially not in a technical forum.
                              [color=blue]
                              > which in addition to depriving the OP the chance to learn to something,
                              > was factually incorrect,[/color]

                              It was correct.
                              [color=blue]
                              > and as you now clarify, relied on a meaningless interpretation of the
                              > question rather than assuming some meaning in there.[/color]

                              The C++ standard's definition of the word "virtual" is not at all
                              meaningless in a C++ newsgroup.

                              Comment

                              Working...