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

  • Home
  • SEARCH
  • 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 706945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:13:21+00:00 2026-05-14T04:13:21+00:00

I have just created 2 pointers which has undefined behavior and try to invoke

  • 0

I have just created 2 pointers which has undefined behavior and try to invoke a class member function which has no object created ?

I don’t understand this?

#include<iostream>

using namespace std;

class Animal
{
public:
  void talk()
  {
    cout<<"I am an animal"<<endl; 
  }
};

class Dog : public Animal
{  
public:
  void talk()
  {
    cout<<"bark"<<endl; 
  }
};

int main()
{
  Animal * a;
  Dog * d;

  d->talk();
  a->talk();  
} 
  • 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-14T04:13:21+00:00Added an answer on May 14, 2026 at 4:13 am

    A) It’s undefined behavior. Any behavior may happen.

    B) Since you’re not calling a virtual method, it’s pretty easy to explain why the undefined behavior actually does this (and I’ve tested this under just about every compiler I could find).

    In C++, calling a member method is equivalent (in practice if not in definition) of calling a member with a hidden ‘this’ variable. If the method is virtual, it has to go through the vftable, but not for a non-virtual method.

    So

    Foo::Bar(){}
    

    is the rough equivalent of

    Foo_Bar(Foo *this){}
    

    and in the calling code

    Foo *foo = new Foo();
    foo->bar();
    

    the second line is roughly the moral equivalent of

    Foo_Bar(foo);
    

    Now, there’s some simplification going on here, and as I said, some of this may be implementation detail rather than specification. However, the behavior holds true (though relying upon it is an error).

    But, given the preceding, look at an implementation:

    void Foo::Bar(){printf("Hello, world!\n");}
    

    and calling code:

    Foo *foo = 0;
    foo->Bar();
    

    As we’ve said, this is roughly equivalent (since we’re non-virtual) to:

    Foo *foo = 0;
    Foo::Bar(foo);
    

    Which means that we’re calling the equivalent of the following method:

    void Foo_Bar(Foo* this)
    { printf("Hello, world\n"); }
    

    Now, given this method, we’re not actually dereffing the this pointer! So, it’s pretty clear why, in this case, the method will work and not fail.

    The practical upshot of this is that calling non-virtual methods on a null pointer, where the method doesn’t deref an a member, will generally result in this observed behavior. But, relying upon any undefined behavior is, basically, evil.

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

Sidebar

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I found a hint at this on another site. The… May 16, 2026 at 5:16 pm
  • Editorial Team
    Editorial Team added an answer If you want to show the result in the same… May 16, 2026 at 5:16 pm
  • Editorial Team
    Editorial Team added an answer Use M-x browse-url-of-file It's generally not bound, so I use...… May 16, 2026 at 5:16 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have some MSVC++ compiled DLL's for which I have created COM-like (lite) interfaces
Here's my situation: I have 2 person objects, person1 and person2, which were created
I have created a dynamic typing system in C in order to create a
I have two Java class hierarchies that share a common ancestor and implement a
In one of my projects, I have some classes that represent entities that cannot
I've just started building a prototype application in Django. I started out by working
I have a bit of code I'm running to test multithreading in MATLAB mex
My problem is this: I am trying to implement a C++ library which can
I'm new to C++, and I have a question that should be easy, but
I'm using OpenGL ES on Android to draw some shapes, so I have some

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.