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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:50:12+00:00 2026-05-14T14:50:12+00:00

What i think about virtual class is, if a derived class has a public

  • 0

What i think about virtual class is, if a derived class has a public base, let’s say, class base, then a pointer to derived can be assigned to a variable of type pointer to base without use of any explicit type conversion. But what if, we are inside of base class then how can we call derived class’s functions. I will give an example:

class Graph{
  public:
    Graph(string);
    virtual bool addEdge(string,string);
}
class Direct:public Graph{
  public:  
    Direct(string);
    bool addEdge(string,string);
}
Direct::Direct(string filename):Graph(filename){};

When i call constructor of Direct class then it calls Graph. Now lets think Graph function calls addedge.

Graph(string str){
    addedge(str,str);
}

When it calls addedge, even if the function is virtual, it calls Graph::edge. What i want is, to call Direct::addedge. How can it be done?

  • 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-14T14:50:12+00:00Added an answer on May 14, 2026 at 2:50 pm

    It can’t be done. Virtual functions cannot be called whithin constructors — at least they cannot be called with virtual behavior. The problem is that the derived class constructor is responsible for setting up the vtbl to point to it’s particular instance of the virtual functions. The base class’ constructor is executed first, before the derived constructor, so a direct solution to this is not possible.

    You can work around this using either some form of “init” function on your base class, or you can use a factory method.

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

Sidebar

Related Questions

public class City { virtual public long Id { get; set; } virtual public
What do you think about this build tool ? I'm thinking of migrating from
When you think about it, doesn't the REST paradigm of being resource-oriented boil down
What do you guys think about this for a generic singleton? using System; using
I need to think about performance limitations of 100 mbps ethernet (including scenarios with
I'd like know what people think about using RAISERROR in stored procedures to pass
I needed to re-install my computer but I didn't think about exporting the data
Are there any good online resources for how to create, maintain and think about
I'm used to doing Java programming, where you never really have to think about
Do you think that writing about software (i.e. having a blog) and speaking on

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.