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 6706431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:31:11+00:00 2026-05-26T07:31:11+00:00

I was curious since I thought of the idea as a solution to improve

  • 0

I was curious since I thought of the idea as a solution to improve the readability of my code for my assignment. When I create an object of a class, is there a way to call one of the object/class’ functions inside the constructor? If I’m not explaining myself clearly enough just ask and I’ll try to expound on it.

  • 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-26T07:31:11+00:00Added an answer on May 26, 2026 at 7:31 am

    Of course you can…

    … But beware of virtual methods

    There is only one catch, though: Avoid calling virtual methods as you won’t call the most derived method, only the most derived for that class. For example:

    class A
    {
       public:
          A() { std::cout << "A::A()" << std::endl ; }
          virtual void foo() { std::cout << "A::foo()" << std::endl ; }
    } ;
    
    class B : public A
    {
       public:
          B() { std::cout << "B::B()" << std::endl ; this->foo() ; }
          virtual void foo() { std::cout << "B::foo()" << std::endl ; }
    } ;
    
    class C : public B
    {
       public:
          C() { std::cout << "C::C()" << std::endl ; }
          virtual void foo() { std::cout << "C::foo()" << std::endl ; }
    } ;
    
    int main(int argc, char * argv[])
    {
       C c ;
       return 0 ;
    }
    

    The output will be:

    A::A()
    B::B()
    B::foo()
    C::C()
    

    When you create a C object, the constructors will be chained: First A(), then B(), then C(). And as we know by reading the code, the B() constructor calls foo(), which is a virtual method.

    In C++, the method called by B() will be the foo() method which is:

    • the most derived possible
    • and still defined in B or one of B’s base classes

    And for the current code, the right method is B::foo().

    But why?

    When you are executing the B constructor of the C object, only the A and B parts of C have been constructed. The C part isn’t constructed yet, so trying to access C’s data from B’s constructor would probably crash your code, and is a semantic error anyway.

    So we must avoid that. The only normal way to access C’s data from one of the B’s methods is using virtual methods which are overridden by C.

    So, to avoid that, virtual methods are not "fully resolved" in constructors and destructors (which suffer from the same problem, for the same underlying reasons).

    Note that this C++ behavior is different from Java and C#’s.

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

Sidebar

Related Questions

I suppose this isn't a huge deal, since there are other way around this
I was curious since i read it in a doc. Does writing select *
Ever since I started programming this has been something I have been curious about.
I'm curious if this is possible at all since a select * from html
I am curious if I can get some help with Open Graph since I
Curious whether folks have setup 2 way transactional replication on the tables ASP.NET uses
Since we can run JavaScript using various interpreters, like V8 or Rhino, I thought
I am curious why I am getting the following behaviour in my code. #include
I'd like to refactor some old C code of mine, and I was curious
class Some(object): tokens = [ ... list of strings ... ] untokenized = [tokens.index(a)

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.