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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:27:34+00:00 2026-06-11T10:27:34+00:00

conv.h class Base { public: void foo(); }; class Derived: public Base { public:

  • 0

conv.h

class Base 
{
public:
    void foo();
};

class Derived: public Base 
{
public:
    void bar();
};

class A {};
class B 
{
public:
    void koko();
};

conv.cpp

void Base::foo()
{
    cout<<"stamm";
}

void Derived::bar()
{
    cout<<"bar shoudn't work"<<endl;
}
void B::koko()
{
    cout<<"koko shoudn't work"<<endl;
}

main.cpp

#include "conv.h"
#include <iostream>
int main()
{   
    Base * a = new Base;
    Derived * b = static_cast<Derived*>(a);
    b->bar();

    Derived * c = reinterpret_cast<Derived*>(a);
    c->bar();

    A* s1 = new A;
    B* s2 = reinterpret_cast<B*>(s1);
    s2->koko();
}

output:

bar shoudn't work
bar shoudn't work
koko shoudn't work

How come the method bar is succeeded to be called in run time despite that I have created a Base class not derived?? it works even with two types of conversions (static and reinterpret cast).

same question as above but with unrelated classes (A & B) ??

  • 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-06-11T10:27:36+00:00Added an answer on June 11, 2026 at 10:27 am

    It’s working (read: “compiling and not crashing”) ’cause you never use the this pointer in your nominally “member” functions. If you tried to print out a member variable, for example, you’d get the garbage output or crashes you expect – but these functions as they are now don’t depend on anything in the classes they’re supposedly part of. this could even be NULL for all they care.

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

Sidebar

Related Questions

public void log(String msg, Color c = Color.black) { loggerText.ForeColor = c; loggerText.AppendText(\n +
I've got a framework which consists of many base classes that can be derived
I am trying to call a constructor of a Base Class in a function
I recently noticed a class in C++0x that calls for an explicit default constructor.
Using XScalaWT , this compiled under Scala 2.7: class NodeView(parent: Composite) extends Composite(parent) {
In code: //file main.cpp LINT a = 12; LINT b = 3; a =
I have a Function (within the class Database) function locTo (){//return the location of
I am converting double values to string like this: std::string conv(double x) { char
class Message has_many :threads, :class_name=>Message, :conditions => `#{Message.table_name}`.conversation_id = #{self.send(:conversation_id)} end m = Message.first
I'm playing with the gtkd wrapper generator, and running into an std.conv exception for

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.