Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 951019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:39:47+00:00 2026-05-15T23:39:47+00:00

I was reading Google C++ Style Guide, and got confused in the Doing Work

  • 0

I was reading Google C++ Style Guide, and got confused in the Doing Work in Constructors part. One of the cons of doing heavy work in constructor is:

If the work calls virtual functions,
these calls will not get dispatched to
the subclass implementations. Future
modification to your class can quietly
introduce this problem even if your
class is not currently subclassed,
causing much confusion.

I didn’t understand what it means. Could someone provide an explanation and why this may be considered a disadvantage?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:39:48+00:00Added an answer on May 15, 2026 at 11:39 pm

    I’m blatantly ripping off some example code from the Wikipedia Virtual function page:

    #include <iostream>
    #include <vector>
    
    class Animal {
        public:
            virtual void eat() const { 
                std::cout << "I eat like a generic Animal." << std::endl; 
            }
            virtual ~Animal() { 
            }
    };
    
    class Wolf : public Animal {
        public:
            void eat() const { 
                std::cout << "I eat like a wolf!" << std::endl; 
            }
    };
    
    class Fish : public Animal {
        public:
            void eat() const { 
                std::cout << "I eat like a fish!" << std::endl; 
            }
    };
    

    If you call eat() inside the Animal constructor, it will call the Animal eat() function every time. Even when you create a Wolf or a Fish object, since the Animal constructor will complete before the subclass object is initialized, the overriding eat functions won’t exist yet.

    This is a disadvantage because it can cause confusion between what’s expected and what actually happens. If I override eat then create an object of my subclass, I expect my overridden function to be called, even from an Animal reference. I expect it because that’s what happens when the call is made explicitly by code outside the constructor. The behavior is different inside the constructor, causing me to scratch my head in bewilderment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading Google C++ Style Guide, and got confused in the Exceptions part.
I have been reading about making ajax heavy applications more search engine friendly: https://developers.google.com/webmasters/ajax-crawling/docs/getting-started
Hey folks, thanks for reading I am currently attempting to do a Google-style calculator.
I'm reading this google guide and using this sample code provided by google ,
I remember reading a style guide explaining what the proper line-height should be for
I have been reading Google's android tutorial and I got a problem... In the
I previously posted this question ( Reading Google docs spreadsheet ) and got a
I was reading the Writing Style Guidelines provided by Google until I saw this:
when reading a python ndb api of google app engine, I cam across this
After reading this post on Stackoverflow Google plus popup box when hovering over thumbnail?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.