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. Can I Call virutal function from a default Constructor in c++

Can I Call virutal function from a default Constructor in c++

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
5 Posts 4 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.
  • P Offline
    P Offline
    phijophlip
    wrote on last edited by
    #1

    Hello Friend, Can I call virtual fucnction from default constructor ? Kindly see the C++ code given below #include using namespace std; class A { public: A() { show(); } virtual void show() { cout <<"virutal function inside the default constructor" << endl; } }; int main() { A obj; return 0; } What is the techinal advantage and disadvantages of call virutal function from the base class defualt constructor ? Kindly mention the place where this condition is used in c++ . Kindly help. -PHIJO MATHEW PHILIP. :) :):):)

    W C 2 Replies Last reply
    0
    • P phijophlip

      Hello Friend, Can I call virtual fucnction from default constructor ? Kindly see the C++ code given below #include using namespace std; class A { public: A() { show(); } virtual void show() { cout <<"virutal function inside the default constructor" << endl; } }; int main() { A obj; return 0; } What is the techinal advantage and disadvantages of call virutal function from the base class defualt constructor ? Kindly mention the place where this condition is used in c++ . Kindly help. -PHIJO MATHEW PHILIP. :) :):):)

      W Offline
      W Offline
      Waldermort
      wrote on last edited by
      #2

      The short answer is yes, but I would advise against it. A virtual method will propegate down to the derived class. But when you call the derived class' c'tor it will initialize the base class first. So, in the derived method, if you are relying on any members being initialized, they won't be. This may lead to hard to spot problems later in your code. Pseudo code: call derived c'tor call base c'tor initialize base members call virtual method use **un**-initialized derived member initialize derived members

      B 1 Reply Last reply
      0
      • P phijophlip

        Hello Friend, Can I call virtual fucnction from default constructor ? Kindly see the C++ code given below #include using namespace std; class A { public: A() { show(); } virtual void show() { cout <<"virutal function inside the default constructor" << endl; } }; int main() { A obj; return 0; } What is the techinal advantage and disadvantages of call virutal function from the base class defualt constructor ? Kindly mention the place where this condition is used in c++ . Kindly help. -PHIJO MATHEW PHILIP. :) :):):)

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        See here[^]


        Cédric Moonen Software developer
        Charting control [v1.1]

        1 Reply Last reply
        0
        • W Waldermort

          The short answer is yes, but I would advise against it. A virtual method will propegate down to the derived class. But when you call the derived class' c'tor it will initialize the base class first. So, in the derived method, if you are relying on any members being initialized, they won't be. This may lead to hard to spot problems later in your code. Pseudo code: call derived c'tor call base c'tor initialize base members call virtual method use **un**-initialized derived member initialize derived members

          B Offline
          B Offline
          Blake Miller
          wrote on last edited by
          #4

          Unless it is qualifid with the current class name, correct? You can 'force' the virtual function of a specific, known baser class, to be invoked as long as you are at least derived from said baser class. That would be safe.

          W 1 Reply Last reply
          0
          • B Blake Miller

            Unless it is qualifid with the current class name, correct? You can 'force' the virtual function of a specific, known baser class, to be invoked as long as you are at least derived from said baser class. That would be safe.

            W Offline
            W Offline
            Waldermort
            wrote on last edited by
            #5

            Anything is safe as long as you know what you are doing. In the case of the poster though, it's not wise to baffle him with technicalities while he is just starting on the basics. Later, after he has got the basics under his thumb, is when he will start breaking the rules and pushing his code as far as it will give :rolleyes:

            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